reading-notes

Permissions & Postgresql

DRF Permissions

class ExampleView(APIView): permission_classes = [IsAuthenticated]

def get(self, request, format=None):
    content = {
        'status': 'request was permitted'
    }
    return Response(content) ``` - AllowAny: will allow unrestricted access, regardless of if the request was authenticated or unauthenticated. - IsAuthenticated: deny permission to any unauthenticated user, and allow permission otherwise. - IsAdminUser: deny permission to any user, unless user.is_staff is True in which case permission will be allowed. - IsAuthenticatedOrReadOnly: allow authenticated users to perform any request. Requests for unauthorised users will only be permitted if the request method is one of the "safe" methods; GET, HEAD or OPTIONS.  - Django model permissions:   - 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.   - DELETE requests require the user to have the delete permission on the model. - DjangoModelPermissionsOrAnonReadOnly: Similar to DjangoModelPermissions, but also allows unauthenticated users to have read-only access to the API. - Django object permissions:   - POST requests require the user to have the add permission on the model instance.   - PUT and PATCH requests require the user to have the change permission on the model instance.   - DELETE requests require the user to have the delete permission on the model instance. - test if a request is a read operation or a write operation, you should check the request method against the constant SAFE_METHODS, which is a tuple containing 'GET', 'OPTIONS' and 'HEAD': ``` if request.method in permissions.SAFE_METHODS:
# Check permissions for read-only request else:
# Check permissions for write request ``` - example of a permission class that checks the incoming request's IP address against a blocklist, and denies the request if the IP has been blocked: ``` from rest_framework import permissions

class BlocklistPermission(permissions.BasePermission): “”” Global permission check for blocked IPs. “””

def has_permission(self, request, view):
    ip_addr = request.META['REMOTE_ADDR']
    blocked = Blocklist.objects.filter(ip_addr=ip_addr).exists()
    return not blocked ``` - object-level permissions, that are only run against operations that affect a particular object instance: ``` class IsOwnerOrReadOnly(permissions.BasePermission):
"""
Object-level permission to only allow owners of an object to edit it.
Assumes the model instance has an `owner` attribute.
"""

def has_object_permission(self, request, view, obj):
    # Read permissions are allowed to any request,
    # so we'll always allow GET, HEAD or OPTIONS requests.
    if request.method in permissions.SAFE_METHODS:
        return True

    # Instance must have an attribute named `owner`.
    return obj.owner == request.user ```