Expand description
Authentication & authorization.
Three pieces:
users.rs— user records, password hashing, login/logoutsessions.rs— DB-backed sessions with expiry cleanuppermissions.rs— granular permissions + groups + theauthorize!check used throughout the admin
A user belongs to zero or more groups. Permissions come from two
sources: (a) direct assignments on the user, (b) inherited from
the user’s groups. The permission string is
"<app>.<action>_<model>" — e.g. "posts.change_post".
Structs§
- Identity
- The identity attached to a request by the auth middleware. Kept cheap to clone because we pass it into handler bodies.
- Permission
- Stored
User - Superuser
- Marker type used by the authorize! macro for fast-paths on admins.
Enums§
Constants§
- SESSION_
COOKIE - The cookie name we look for and set. Constant so middleware and handlers stay in sync.
Functions§
- add_
user_ to_ group - bootstrap_
default_ groups - Insert each default group with
ON CONFLICT (name) DO NOTHING. Idempotent across restarts and against admin-created groups — duplicates by name simply skip without bumping any state. - bootstrap_
demo_ users - Phase 7a/0.5/d — gated by
RUSTIO_DEMO_MODE=1. Inserts the five demo users keyed offbranding.domain(e.g.staff@rustio.local) and attaches each to the matching default groups (which must already exist; callbootstrap_default_groups+lazy_attach_*first). Idempotent via the demo-count gate: re-running on a DB that already has demo users is a no-op. Real users coexist — the gate counts onlyis_demo = TRUErows. - check_
permission - Ask “does this identity have permission X?”.
- create_
group - create_
session - create_
user - delete_
session - find_
user_ by_ email - grant_
to_ group - grant_
to_ user - hash_
password - identity_
from_ session - init_
permission_ tables - init_
session_ tables - init_
tables - Initialise every auth-related table. Safe to call on every boot.
- init_
user_ tables - lazy_
attach_ permissions - For each default group, attach the permissions it can resolve from
the currently-registered models.
All(&[…])becomes one perm per non-core entry;Specific(&[…])becomes one perm per matching entry (skipping un-registered models silently). - login
- Verify credentials and create a session. Returns the session token to set in the cookie. A deliberately vague error on failure — we don’t want to leak whether the email was valid.
- migrate_
user_ schema - Idempotent schema upgrade for the 5-tier role hierarchy + demo flag.
- permissions_
for_ user - All permission names belonging to the given user — direct + via groups — unioned into one set. Cached for 60s.
- purge_
expired_ sessions - Delete all expired sessions. Intended to be called periodically
from a background task (see
background::spawn_session_sweeper). - register_
model_ permissions - For an admin model named
posts, register the canonical four permissions: add_post, change_post, delete_post, view_post. Idempotent. - remove_
user_ from_ group - session_
token_ from_ cookie - set_
password - update_
user_ role - verify_
password - would_
orphan_ developers - Phase 7a/0.5/f — would the proposed change leave the system with zero active Developers?