Access control
Table of contents
- Introduction
- How a request is checked
- Choosing the roles
- Gating a screen
- Hiding a link
- Writing an access policy
- Deciding who reads a private file
- Advanced
- Reference
- See also
Introduction
A feature keeps its screens for the right people with role_required, and hides the links to them with has_role. When another feature brings finer rules for those screens, it registers an access policy, and the gate and the links both follow it.
from splent_framework.decorators.decorators import role_required
from splent_framework.roles import PANEL_ROLES
@notes_bp.route("/admin/notes")
@role_required(*PANEL_ROLES)
def admin_notes():
...
How a request is checked
Choosing the roles
The framework reads the role attribute of the signed-in account, a column the feature that owns the accounts gives its user model. An account without one gets past no gate.
| Constant | Roles | Use it for |
|---|---|---|
PANEL_ROLES |
("admin", "staff") |
The site’s content, such as posts, events or the inbox of a form |
ADMIN_ROLES |
("admin",) |
The site itself, such as its accounts, its keys and its settings |
Import them from splent_framework.roles, so every gate and every link reads the same tuple. To add a role, use the new name in your gates and your seeders.
Gating a screen
Put role_required under the route, as in the introduction. It asks for a login too, so it replaces login_required. An anonymous visitor goes to the login page, and an account outside the roles gets 403.
Hiding a link
from flask import render_template
from splent_framework.decorators.decorators import has_role
from splent_framework.roles import PANEL_ROLES
def notes_sidebar_link():
if not has_role(*PANEL_ROLES, endpoint="notes.admin_notes"):
return ""
return render_template("hooks/sidebar_notes.html")
Pass the endpoint the link opens, so the link follows the screen’s policies too. Templates have has_role as well.
{% if has_role('admin', 'staff', endpoint='notes.admin_notes') %}
<a href="{{ url_for('notes.admin_notes') }}">Notes</a>
{% endif %}
A menu entry needs none of this. The menu reads the roles role_required left on the view, through required_roles(view), and asks for you.
Writing an access policy
A policy lets your feature decide who opens another feature’s screens. Say your feature names the staff of each collection, whose accounts have the plain user role, and lets them into the shop’s admin.
from splent_framework.decorators.decorators import register_access_policy
DECIDED = {"shop.admin_index", "shop.admin_edit"}
def staff_of_some_collection(user, endpoint, view_args):
if user.role == "admin":
return None # the roles decide, before any query
if endpoint not in DECIDED:
return None # a screen this feature has not decided
return bool(staff_service.collections_for(user))
def init_feature(app):
register_access_policy("shop.admin", staff_of_some_collection)
The policy answers for every endpoint that starts with the prefix. The prefix is the blueprint’s name, a dot and anything after, such as shop.admin, or shop. for the whole blueprint.
| The policy returns | The account |
|---|---|
True |
Gets in, whatever the screen’s roles |
False |
Gets 403, whatever its role |
None |
Is decided by the next shorter prefix, or by the screen’s roles when none is left |
Register policies from init_feature, never while serving.
- Pass the accounts the roles already handle, before any query. A policy that raises refuses, so a failing query would lock the admin out.
- Answer only for the screens you have decided. A screen added later keeps its roles.
- Return
Falsefor an account you must keep out. Its link then disappears too.
Deciding who reads a private file
A file served behind a check lives in a feature’s private storage. The feature that stores it records on each item its access, and for a restricted one the owner_feature that decides. That owner registers a resolver from init_feature.
from splent_framework.services.file_access import register_file_access_resolver
def init_feature(app):
register_file_access_resolver("splent_feature_reports", ReportAccessPolicy().resolve)
resolver(item, user) answers whether the user, anonymous included, may read the item. The route asks resolve_file_access(item, user), then sends the file with send_protected_file.
| The item | resolve_file_access |
|---|---|
access = "public" |
Allows |
Restricted, and the resolver of its owner_feature answers truthy |
Allows |
Restricted, with no owner_feature, no resolver for it, or a resolver that raises |
Denies, and logs a raising resolver |
No access attribute, such as a dict |
Denies, and logs a warning |
A later resolver for the same owner replaces the earlier one.
send_protected_file shows PDFs and PNG, JPEG, GIF and WebP images inline. Anything else goes out as an application/octet-stream attachment, because an uploaded HTML or SVG file would run in your site. With PROTECTED_FILES_ACCEL_PREFIX set, such as /_protected, nginx sends the bytes through X-Accel-Redirect.
Advanced
To test your feature’s own gate without the policies other features registered, take the
no_access_policiesfixture, or usewith isolated_access_policies(). Both put the policies back when the test ends.
Reference
| Case | What happens |
|---|---|
| Inactive account | 403, even when a policy would answer True |
| Several prefixes match | Asked from the longest to the shortest. The first answer that is not None decides |
| The same prefix registered twice | The later policy wins, with a warning in the log |
| A prefix without the blueprint and a dot | register_access_policy raises ValueError |
| A policy raises | 403, and the error is logged |
A view with only login_required |
No policy is asked. Gate it with role_required |
Two role_required on one view |
Both are asked, so only the roles they share get in. required_roles(view) returns those |
role_required() with no roles |
Only an account a policy lets in gets through |
See also
- Extension points
- Signals, to veto an action rather than a screen.