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

Permission Gated Publishing

mediumDjango PermissionsAccess Control100%2 of 2 solved

Problem statement

In this Django project, implement the rule that only editors decide what goes live. Gate publish_article and unpublish_article in app/views.py on the app.publish_article permission, and draw the publish and unpublish controls in app/templates/app/article_detail.html only for a visitor who holds it. This closes two routes whose whole check is "is anybody signed in", and stops the page advertising an action it is going to refuse.

Requirements

Implement:

  • publish_article in app/views.py
  • unpublish_article in app/views.py
  • the controls in app/templates/app/article_detail.html

The following files are already implemented and provided: app/models.py, app/urls.py, the article list template, the login template and the set_published(slug, state) helper, which finds the article, writes is_published and redirects to the article's own page. Article holds title, slug (unique), body, is_published, an author pointing at a user, and a get_absolute_url(). Its Meta declares permissions = [("publish_article", "Can publish article")], so the permission row already exists and the name every check uses is app.publish_article. A read-only data migration, app/migrations/0002_editors_group.py, creates a group named editors and puts that permission on it. You create neither the permission nor the group. LOGIN_URL is /accounts/login/, and perms is in every template context because the auth context processor is installed.

Keep the view names publish_article and unpublish_article that app/urls.py imports, and keep set_published as the thing that writes the state.

1. POST /articles/<slug>/publish/ and POST /articles/<slug>/unpublish/

  • Purpose: Publish a draft article, or pull a live one down.
  • Authentication: Required.
  • Authorization: app.publish_article, held directly or through the editors group.
  • Response:
    • 302 Found: to the article's own page for a permitted user, with is_published changed in the database.
    • 403 Forbidden: for a signed-in user without the permission, with is_published left exactly as it was.
    • 302 Found: to /accounts/login/ with next= carrying the route that was asked for, for an anonymous visitor.
  • A direct grant and a group grant answer identically, because the default authentication backend unions the two sets.
  • Keeping the two denials apart is what the order of the checks is for: the signed-in check runs first and sends a stranger to LOGIN_URL with next=, and only then is a refusal raised rather than redirected.

2. GET /articles/<slug>/ - the controls

  • Purpose: Show the article, with the controls the visitor is allowed to use.
  • Authentication: Not required.
  • Response:
    • 200 OK: the article page for everybody. The Publish form appears on a draft and the Unpublish form on a live article, both only for a visitor holding app.publish_article. A signed-in user without it and an anonymous visitor get the same page with neither control on it.
  • Keep the four control ids publish-form, publish-button, unpublish-form and unpublish-button, and keep article-title on the heading and article-state on the state line: each control is found by its id, and the heading shows that the page itself rendered.

3. Nothing to redeploy

  • Adding somebody to the editors group grants them publishing immediately, with no change to any file, and reveals the controls on their next page load. This follows from the two requirements above rather than being extra work.

Notes

  • Check the permission app.publish_article. Never substitute a username, a group name or user_permissions for it: 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.
  • Hiding a control is not the gate, since a hidden form is still a URL anyone can POST to, but leaving it visible invites a request you are going to refuse.
  • Permissions are cached per user instance, so a grant made during a request is visible after the user is re-read.

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 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 solvedmediumMembers Area Access MatrixThe members area has a public page, member pages, permission-gated actions and a staff-only section, and today every one of them is reachable by everyone. Enforce the whole access matrix across the view and template layers, with a login redirect for anonymous visitors and a 403 for signed-in visitors who are not allowed.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.