mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Add DjangoModelPermissionsOrAnonReadOnly
This commit is contained in:
parent
8dff8d2fdc
commit
b65b065375
|
@ -96,16 +96,15 @@ This permission class ties into Django's standard `django.contrib.auth` [model p
|
||||||
* `POST` requests require the user to have the `add` permission on the model.
|
* `POST` requests require the user to have the `add` permission on the model.
|
||||||
* `PUT` and `PATCH` requests require the user to have the `change` permission on the model.
|
* `PUT` and `PATCH` requests require the user to have the `change` permission on the model.
|
||||||
* `DELETE` requests require the user to have the `delete` permission on the model.
|
* `DELETE` requests require the user to have the `delete` permission on the model.
|
||||||
|
|
||||||
If you want to use `DjangoModelPermissions` but also allow unauthenticated users to have read permission, override the class and set the `authenticated_users_only` property to `False`. For example:
|
|
||||||
|
|
||||||
class HasModelPermissionsOrReadOnly(DjangoModelPermissions):
|
|
||||||
authenticated_users_only = False
|
|
||||||
|
|
||||||
The default behaviour can also be overridden to support custom model permissions. For example, you might want to include a `view` model permission for `GET` requests.
|
The default behaviour can also be overridden to support custom model permissions. For example, you might want to include a `view` model permission for `GET` requests.
|
||||||
|
|
||||||
To use custom model permissions, override `DjangoModelPermissions` and set the `.perms_map` property. Refer to the source code for details.
|
To use custom model permissions, override `DjangoModelPermissions` and set the `.perms_map` property. Refer to the source code for details.
|
||||||
|
|
||||||
|
## DjangoModelPermissionsOrAnonReadOnly
|
||||||
|
|
||||||
|
Similar to `DjangoModelPermissions`, but also allows unauthenticated users to have read-only access to the API.
|
||||||
|
|
||||||
## TokenHasReadWriteScope
|
## TokenHasReadWriteScope
|
||||||
|
|
||||||
This permission class is intended for use with either of the `OAuthAuthentication` and `OAuth2Authentication` classes, and ties into the scoping that their backends provide.
|
This permission class is intended for use with either of the `OAuthAuthentication` and `OAuth2Authentication` classes, and ties into the scoping that their backends provide.
|
||||||
|
|
|
@ -89,8 +89,8 @@ class DjangoModelPermissions(BasePermission):
|
||||||
It ensures that the user is authenticated, and has the appropriate
|
It ensures that the user is authenticated, and has the appropriate
|
||||||
`add`/`change`/`delete` permissions on the model.
|
`add`/`change`/`delete` permissions on the model.
|
||||||
|
|
||||||
This permission will only be applied against view classes that
|
This permission can only be applied against view classes that
|
||||||
provide a `.model` attribute, such as the generic class-based views.
|
provide a `.model` or `.queryset` attribute.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Map methods into required permission codes.
|
# Map methods into required permission codes.
|
||||||
|
@ -138,6 +138,14 @@ class DjangoModelPermissions(BasePermission):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class DjangoModelPermissionsOrAnonReadOnly(DjangoModelPermissions):
|
||||||
|
"""
|
||||||
|
Similar to DjangoModelPermissions, except that anonymous users are
|
||||||
|
allowed read-only access.
|
||||||
|
"""
|
||||||
|
authenticated_users_only = False
|
||||||
|
|
||||||
|
|
||||||
class TokenHasReadWriteScope(BasePermission):
|
class TokenHasReadWriteScope(BasePermission):
|
||||||
"""
|
"""
|
||||||
The request is authenticated as a user and the token used has the right scope
|
The request is authenticated as a user and the token used has the right scope
|
||||||
|
|
Loading…
Reference in New Issue
Block a user