StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 9 · Authentication & Authorization

Members Area Access Matrix

mediumDjango PermissionsAccess Control100%2 of 2 solved

Problem statement

In this Django project, implement a members area where four tiers of visitor get four different answers. Wire the sign-in and sign-out routes in app/urls.py, gate the five views in app/views.py on login, on the app.approve_document permission and on is_staff, and draw each control in app/templates/app/member_home.html only for the visitor allowed to use it. This closes a site whose access model is written down and enforced nowhere.

Requirements

Implement:

  • the login and logout routes in app/urls.py
  • the gates on member_home, approve_document, revoke_document and staff_console in app/views.py
  • the controls in app/templates/app/member_home.html

The following files are already implemented and provided: app/models.py, project/settings.py, the staff console template, the login template, and in app/views.py the five views (each one ungated) plus a working set_approved(slug, state) helper that writes is_approved and redirects to the members page. app/urls.py wires the five app routes and keeps their names; the two accounts/ routes are missing. app/templates/app/member_home.html renders the signed-in member's email, the documents, a link to the staff console and the approve/revoke forms - all of it to everybody.

User is the project's own user model: AUTH_USER_MODEL names app.User, it identifies people by email with USERNAME_FIELD = "email", and there is no username field anywhere in the project. Document holds title, slug, summary and is_approved, and its Meta declares permissions = [("approve_document", "Can approve document")], so the permission row already exists and the name every check uses is app.approve_document. A read-only data migration, app/migrations/0002_reviewers_group.py, creates a group named reviewers and puts that permission on it. You create neither the permission nor the group.

Four tiers matter, and they are not nested neatly: a staff user is somebody with is_staff set, which in this project grants no permissions at all, and a permitted member is somebody holding app.approve_document (directly or through reviewers) who is not staff. LOGIN_URL is /accounts/login/, LOGIN_REDIRECT_URL is /members/ and LOGOUT_REDIRECT_URL is /, so the redirects are not yours to configure.

1. GET, POST /accounts/login/ and POST /accounts/logout/

  • Purpose: Sign a visitor in and out. Both are Django's built-in views, wired under the names login and logout.
  • Response:
    • 200 OK: the login page for an anonymous visitor. LoginView renders registration/login.html with no configuration, and that template ships ready. Because USERNAME_FIELD is email, AuthenticationForm asks for an email address, though the input is still named username.
    • 302 Found: to /members/ when the credentials are correct and the request carries no next, with a session established.
    • 200 OK: the login page again for wrong credentials, with no session established and this form-wide error on it:
Please enter a correct email and password. Note that both fields may be case-sensitive.
  • 302 Found: to / from a POST of the logout route, with the session ended. A GET of it does not end the session, since LogoutView accepts POST and OPTIONS only.

2. GET /members/

  • Purpose: The members area.
  • Authentication: Required, and nothing more.
  • Response:
    • 200 OK: for every signed-in tier - a plain member, a permitted member and a staff user alike.
    • 302 Found: to /accounts/login/?next=/members/ for an anonymous visitor. Signing in from that redirect lands on /members/ rather than on the default landing page.

3. POST /members/documents/<slug>/approve/ and POST /members/documents/<slug>/revoke/

  • Purpose: Approve a document, or take the approval back.
  • Authentication: Required.
  • Authorization: app.approve_document, held directly or through the reviewers group.
  • Response:
    • 302 Found: to /members/ for a permitted member, with is_approved changed in the database.
    • 403 Forbidden: for a signed-in member without the permission, including a staff user, with is_approved left exactly as it was.
    • 302 Found: to /accounts/login/ with next= carrying the action URL, for an anonymous visitor.

4. GET /staff/

  • Purpose: The staff console.
  • Authentication: Required.
  • Authorization: is_staff.
  • Response:
    • 200 OK: for a staff user, with the console rendered.
    • 403 Forbidden: for a signed-in visitor who is not staff, including a member holding app.approve_document, because the two gates are independent.
    • 302 Found: to /accounts/login/?next=/staff/ for an anonymous visitor.

5. The controls on the members page

  • The approve and revoke forms are drawn only for a visitor holding app.approve_document, and the staff console link only for a staff user.
  • Everybody else still gets the whole page: the heading, their own email address and every document listed, with those controls absent.
  • Keep the ids the template already carries, because each element is found by its id rather than by searching the markup: member-heading, member-email, staff-link, document-list, and per document document-<slug>, title-<slug>, state-<slug>, approve-form-<slug>, approve-button-<slug>, revoke-form-<slug> and revoke-button-<slug>.

6. Nothing to redeploy

  • Adding somebody to the reviewers group grants them the approval actions and reveals the controls immediately, with no change to any file; removing them takes both away again. This follows from requirements 3 and 5 rather than being extra work.

Notes

  • The two denials are different answers to different questions. An anonymous visitor does not know who they are yet, so they are redirected to sign in carrying next=; a signed-in visitor has already answered that and is simply not allowed, so they get 403. Neither answer is a 404, and neither is a silent no-op. The order of the checks is what keeps them apart.
  • GET / is open to every tier and already works.
  • Check the permission app.approve_document. Never check an email address, a group name, or user_permissions directly: a group name lies the moment the group is renamed, and user_permissions counts only a direct grant, so it refuses every member of the group.
  • is_staff grants no permissions - only is_superuser does - and nothing in Django checks it for you outside the admin.
  • Do not reach for staff_member_required or anything else from django.contrib.admin: the admin is not installed.
  • Hiding a control is never the gate, since a hidden form is still a URL anybody can POST to, but drawing a control you are going to refuse invites the request.
  • Use the built-in LoginView and LogoutView, keep set_approved as the thing that writes is_approved, and keep {% csrf_token %} in every POST form.

More in Authentication & Authorization

See the module
Session Access FlowThe site has member pages but no way to sign in, and the protected views are protected by nothing. Wire Django's built-in LoginView and LogoutView with your own login template, gate the member views with login_required, and send a visitor back to the page they originally asked for.100%2 of 2 solvedmediumPermission Gated PublishingPublishing and unpublishing are gated on being signed in and nothing more, so every author can change the state of anyone's article. Gate both actions on the model permission app.publish_article, hide the controls from a visitor who does not hold it, and let the editors group grant it.100%2 of 2 solvedmediumEmail Login Custom UserThe product signs users up by email, but the shipped user model still identifies people by a username. Replace it with a custom user model that authenticates by email, backed by a manager that creates users and superusers correctly.100%2 of 2 solvedmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.