Skip to main content

PasswordAccount

Trait PasswordAccount 

Source
pub trait PasswordAccount:
    Model<UpdateQuery: From<Query<List<Self>>> + IntoStatement<Returning = ()>>
    + IntoExpr<Self>
    + Send
    + Sync
    + 'static {
    type User: PanelUser;

    // Required methods
    fn name() -> Lens<Self, String>;
    fn email() -> Lens<Self, String>;
    fn password_hash() -> Lens<Self, String>;
    fn email_verified_at() -> Lens<Self, Option<Timestamp>>;
    fn account(user: &Self::User) -> &Self;
    fn sign_in(
        self,
        cx: &Cx,
        ex: &mut dyn Executor,
    ) -> impl Future<Output = Result<Option<Self::User>>> + Send;

    // Provided methods
    fn profile_errors(
        user: &Self::User,
        profile: &Profile,
    ) -> FieldErrors<Profile> { ... }
    fn store_locale(
        cx: &Cx,
        user: &Self::User,
        locale: &Locale,
    ) -> impl Future<Output = Result<()>> + Send { ... }
}
Expand description

A user model PasswordAuth signs in by email and password, and whose own account it edits, resets and verifies.

The app names four fields of its model through lens!, and says what the panel signs in: the model itself, or a value loaded beside it, such as the user with their tenants. AdminUser is one.

use tablo_core::{Lens, Membership, PanelUser, PasswordAccount, lens};

#[derive(Debug, Clone, toasty::Model)]
struct User {
    #[key]
    #[auto]
    id: uuid::Uuid,
    name: String,
    #[unique]
    email: String,
    password: String,
    email_verified_at: Option<Timestamp>,
}

/// The user and the tenants they belong to.
struct SignedIn {
    user: User,
    tenants: Vec<Membership>,
}

impl PanelUser for SignedIn {
    fn user_id(&self) -> String {
        self.user.id.to_string()
    }
    fn display_name(&self) -> &str {
        &self.user.name
    }
    fn email(&self) -> Option<&str> {
        Some(&self.user.email)
    }
    fn can_access_panel(&self, _panel: &tablo_core::PanelId) -> bool {
        true
    }
    fn tenants(&self) -> &[Membership] {
        &self.tenants
    }
}

impl PasswordAccount for User {
    type User = SignedIn;

    fn name() -> Lens<Self, String> {
        lens!(User.name)
    }
    fn email() -> Lens<Self, String> {
        lens!(User.email)
    }
    fn password_hash() -> Lens<Self, String> {
        lens!(User.password)
    }
    fn email_verified_at() -> Lens<Self, Option<Timestamp>> {
        lens!(User.email_verified_at)
    }
    fn account(user: &SignedIn) -> &User {
        &user.user
    }
    async fn sign_in(
        self,
        _cx: &Cx,
        _ex: &mut dyn toasty::Executor,
    ) -> topcoat::Result<Option<SignedIn>> {
        // Load the user's memberships through `_ex` here.
        Ok(Some(SignedIn {
            user: self,
            tenants: Vec::new(),
        }))
    }
}

PasswordAuth stores an email trimmed and lowercased, as normalize_email spells it, and looks it up the same way, so a resource or a sign-up that writes the email stores it so too. The model’s primary key is one field, and the User’s PanelUser::user_id spells it as its Display does, such as self.id.to_string(): a sign-in whose id spells another key is refused as a declaration error.

A panel’s edit form over the model that changes an account’s email or password hash ends its sessions, pending sign-ins, reset links and pending email change on every panel that signs in Self::User, as it does an AdminUser’s; a custom action calls revoke_sessions_for_user itself.

Required Associated Types§

Source

type User: PanelUser

What the panel signs in: Self, or a value holding it.

Required Methods§

Source

fn name() -> Lens<Self, String>

The display name, which the profile page edits.

Source

fn email() -> Lens<Self, String>

The address the account signs in with, unique across the table.

Source

fn password_hash() -> Lens<Self, String>

The password’s Argon2id hash in PHC string format, as hash_password writes it.

Source

fn email_verified_at() -> Lens<Self, Option<Timestamp>>

When the account proved it reads its email; None until it does, and again after the email changes. Every email change writes it; only a panel with Auth::email_verification reads it.

Source

fn account(user: &Self::User) -> &Self

The account user signs in as.

Source

fn sign_in( self, cx: &Cx, ex: &mut dyn Executor, ) -> impl Future<Output = Result<Option<Self::User>>> + Send

The account as the panel signs it in, loaded through ex, or None when it no longer signs in, such as a deactivated account: its login fails as a wrong password does, its sessions end at their next request, and it gets no reset link.

It runs on every request of a signed-in session, so a removed membership or a deactivation takes effect at once. It reads through ex, the connection the caller already holds.

Provided Methods§

Source

fn profile_errors(user: &Self::User, profile: &Profile) -> FieldErrors<Profile>

Refuses a profile change the app does not allow user, such as a new email on an account whose sign-in details are locked. Defaults to none. It runs before the current password is checked, and the page renders each error under its field’s control. profile.email is as typed: compare it through normalize_email, as the stored email is spelled.

Source

fn store_locale( cx: &Cx, user: &Self::User, locale: &Locale, ) -> impl Future<Output = Result<()>> + Send

Stores locale as user’s, as Authenticator::store_locale describes. Defaults to storing nothing.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl PasswordAccount for AdminUser

The shipped account PasswordAuth signs in by default. An account whose active is false signs in nowhere and gets no reset link.