Reading-Notes

View the Project on GitHub MohammadAl-khatib/Reading-Notes

Permissions & Postgresql

Permissions

Permission checks are always run at the very start of the view, before any other code is allowed to proceed. Permission checks will typically use the authentication information in the request.user and request.auth properties to determine if the incoming request should be permitted.

How permissions are determined

Using a list to check for permissions, Django will check the list one by one, if a permission is denied, a status code of 401 or 403 will be sent.

Object level permissions

This kind of permissions define if the user can change some data, Django uses classes to define models of data, so data is always an instance (object) of class, anyway, users preferably will see objects which they have this permission upon.

API Reference:

  1. AllowAny

    The AllowAny permission class will allow unrestricted access, regardless of if the request was authenticated or unauthenticated.

    This permission is not strictly required, since you can achieve the same result by using an empty list or tuple for the permissions setting, but you may find it useful to specify this class because it makes the intention explicit.

  2. IsAuthenticated

    The IsAuthenticated permission class will deny permission to any unauthenticated user, and allow permission otherwise.

    This permission is suitable if you want your API to only be accessible to registered users.

  3. IsAdminUser

    The IsAdminUser permission class will deny permission to any user, unless user.is_staff is True in which case permission will be allowed.

    This permission is suitable if you want your API to only be accessible to a subset of trusted administrators.

  4. IsAuthenticatedOrReadOnly

    The IsAuthenticatedOrReadOnly will 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.

    This permission is suitable if you want to your API to allow read permissions to anonymous users, and only allow write permissions to authenticated users.

Also, custom permissions can be created