Skip to main content

rustio_admin/auth/
mod.rs

1//! Authentication & authorization.
2//!
3//! Three pieces:
4//! - `users.rs`       — user records, password hashing, login
5//! - `sessions.rs`    — DB-backed sessions with expiry cleanup
6//! - `permissions.rs` — granular permissions + groups
7//!
8//! A user belongs to zero or more groups. Permissions come from two
9//! sources: (a) direct assignments on the user, (b) inherited from
10//! the user's groups. The permission string is
11//! `<app>.<action>_<model>` — e.g. `posts.change_post`.
12
13pub mod guards;
14mod permissions;
15mod role;
16mod sessions;
17mod users;
18
19pub(crate) use permissions::invalidate_user_cache;
20pub use permissions::{
21    add_user_to_group, check_permission, create_group, grant_to_group, grant_to_user,
22    init_permission_tables, permissions_for_user, register_model_permissions,
23    remove_user_from_group, Permission, PermissionError, Superuser,
24};
25pub use role::{protected_roles, Role};
26pub use sessions::{
27    create_session, current_session_id, delete_session, identity_from_session, init_session_tables,
28    invalidate_sessions, list_active_for_user, logout_session, purge_expired_sessions,
29    session_token_from_cookie, InvalidationOutcome, Session, SessionInvalidationReason,
30    SessionTarget, SessionTrust, SESSION_COOKIE,
31};
32#[allow(deprecated)]
33pub use users::would_orphan_developers;
34pub use users::{
35    create_user, find_user_by_email, hash_password, init_user_tables, load_user_profile, login,
36    migrate_user_schema, set_password, update_user_role, verdict_for_orphan_role, verify_password,
37    would_orphan_protected, would_orphan_role, Identity, StoredUser, UserProfile,
38};
39
40use crate::error::Result;
41use crate::orm::Db;
42
43/// Initialise every auth-related table. Safe to call on every boot.
44pub async fn init_tables(db: &Db) -> Result<()> {
45    init_user_tables(db).await?;
46    migrate_user_schema(db).await?;
47    init_session_tables(db).await?;
48    sessions::migrate_session_schema(db).await?;
49    sessions::migrate_session_lifecycle(db).await?;
50    init_permission_tables(db).await?;
51    Ok(())
52}