Problem statement
In this Django project, express the hardware shop's access policy as permission classes. Set the project-wide default in the REST_FRAMEWORK dict in _project/api_settings.py_ and declare the catalog's own policy on CatalogView in app/views.py. Today nothing says who is allowed in: no view declares a permission class and the project-wide configuration names no default, so DRF falls back to AllowAny everywhere - the account endpoint hands out an account's own details to a client with no account at all, and the public catalog lets that same anonymous client post a new product into the shop.
Requirements
Implement:
- the default permission class in REST_FRAMEWORK in project/api_settings.py
- the permission policy on CatalogView in app/views.py
The following files are already implemented and provided: app/models.py, app/serializers.py, app/urls.py, app/migrations/, the accounts app and project/settings.py. Product holds name, price and in_stock, and Django builds app.add_product, app.change_product, app.delete_product and app.view_product for it as it does for every model. The accounts user model identifies accounts by email, has no username field, and carries PermissionsMixin, so it already understands groups and model permissions. app/migrations/ ships a group named catalog-editors holding the add, change and delete permissions on Product; the group exists in every database the project builds, the test database included, and putting an account into it is an administrative act rather than a code change. app/urls.py routes /api/account/ to AccountView under the name account and /api/catalog/ to CatalogView under the name catalog-list, so both class names stay as they are. project/api_settings.py exports the REST_FRAMEWORK dict that project/settings.py imports, and it already names DEFAULT_AUTHENTICATION_CLASSES: this project authenticates with sessions only, and that part stays exactly as it is. AccountView is an APIView whose get returns the signed-in account's email; CatalogView is a ListCreateAPIView over Product with queryset and serializer_class already declared.
Every refusal in this project arrives as 403, for a client with no account and for an account missing a permission alike. Sessions are the only authentication class and a session cookie is not something a server can challenge for, so DRF has nothing to put in a WWW-Authenticate header and downgrades the 401 it would otherwise raise.
1. An authenticated-only project default
- DEFAULT_PERMISSION_CLASSES in REST_FRAMEWORK admits signed-in accounts only, so a view that declares nothing of its own is closed to strangers.
- GET /api/account/ then answers 403 to an anonymous client and 200 to a signed-in one, with that account's email under the email key.
- Do not add permission_classes to AccountView to achieve this: the point of the default is that the view needs no line.
2. Anonymous reads on the public catalog
- CatalogView declares permission_classes of its own, overriding the project default for that endpoint and for no other.
- GET /api/catalog/ answers 200 with every product to a client with no account at all.
3. Model-permission gating on catalog writes
- POST /api/catalog/ answers 403 unless the requesting account holds app.add_product. An anonymous client is refused, and so is a signed-in account that does not hold the permission; neither one leaves a row behind.
- An account in the catalog-editors group holds that permission through the group, and the same POST then answers 201 with the stored product.
- No code changes when an account joins or leaves the group.
4. Safe methods ask for nothing
- GET, HEAD and OPTIONS on /api/catalog/ all answer 200 for a signed-in account that holds no permission on Product at all, in the same breath as that account's POST being refused with 403.
- The read side of the catalog never consults a model permission.
Notes
- A permission class answers one question before the view body runs: may this request go on? DRF asks every class in permission_classes and the request continues only if all of them say yes, so a correctly permissioned view needs no if of its own.
- Where the list comes from matters as much as what is in it. DEFAULT_PERMISSION_CLASSES is the floor and applies to every view that does not declare permission_classes itself; a view that does declare it replaces the default entirely for itself and changes nothing for any other view.
- DRF's model-permission family works off Django's own per-model permissions, mapping the request method to a codename on the view's queryset model: GET, HEAD and OPTIONS ask for nothing, POST asks for add_<model>, PUT and PATCH for change_<model>, DELETE for delete_<model>. DjangoModelPermissions is the strict member and requires an authenticated account for every method, safe ones included; DjangoModelPermissionsOrAnonReadOnly is the same mapping with that one requirement dropped.
- The permission itself is never written in code. user.has_perm("app.add_product") is true when the account holds it directly or when any group it belongs to holds it, which is the point of gating on a model permission rather than on an account flag: who may write becomes data.
- When DRF refuses, it raises NotAuthenticated for a client with no account and PermissionDenied for an account lacking the permission. NotAuthenticated only becomes a 401 if some authentication class can offer a WWW-Authenticate challenge with it. A project that lists a scheme which can challenge, token authentication being the usual one, answers that same anonymous request 401; this one does not, so the number that comes back tells you which authentication classes the project configured rather than that something is broken.
- Only _project/api_settings.py_ and app/views.py change. Keep queryset and serializer_class on CatalogView, and leave DEFAULT_AUTHENTICATION_CLASSES alone.
- Do not check request.user by hand in a view and do not write an if that returns 403 itself: the permission classes answer before the handler runs.
- Do not touch the group in app/migrations/, and do not grant permissions from code: membership is data.
- Do not reach for object-level permissions, has_object_permission or a filtered get_queryset - this question is about who may call the endpoint, not about which rows they see - and do not add pagination, filtering or throttling.
