use crate::sql::Auto;
use crate::Model;
#[derive(Model, Clone)]
#[rustango(
table = "rustango_admin_users",
access = "admin_only",
app = "admin",
display = "username",
admin(
list_display = "username, is_superuser, active, created_at",
search_fields = "username",
ordering = "username",
readonly_fields = "created_at, sessions_revoked_at",
formfield_overrides = "password_hash: password",
)
)]
pub struct AdminUser {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(max_length = 150, unique)]
#[rustango(help_text = "Login for the admin, e.g. `admin`. Unique.")]
#[rustango(verbose_name = "Username")]
pub username: String,
#[rustango(max_length = 200)]
#[rustango(
help_text = "Argon2id hash of the password; the password itself is never stored. Set with `create-admin` or the password pages."
)]
#[rustango(verbose_name = "Password hash")]
pub password_hash: String,
#[cfg(feature = "admin-sso")]
#[rustango(max_length = 254, unique)]
#[rustango(
help_text = "Address that links an admin SSO sign-in to this account: the IdP's verified email must match. Empty means no SSO. Unique."
)]
#[rustango(verbose_name = "Email")]
pub email: Option<String>,
#[rustango(default = "false")]
#[rustango(
help_text = "Security: full admin access. The admin lets only superusers in by default, so a non-superuser gets a 403."
)]
#[rustango(verbose_name = "Superuser")]
pub is_superuser: bool,
#[rustango(default = "true")]
#[rustango(help_text = "Off: the account cannot sign in and its sessions end. The row stays.")]
#[rustango(verbose_name = "Active")]
pub active: bool,
#[rustango(help_text = "When the row was created. Set by the database.")]
#[rustango(verbose_name = "Created at")]
pub created_at: chrono::DateTime<chrono::Utc>,
#[rustango(
help_text = "Set on logout. Security: sessions issued at or before it are refused on every device."
)]
#[rustango(verbose_name = "Sessions revoked at")]
pub sessions_revoked_at: Option<chrono::DateTime<chrono::Utc>>,
}
redacted_debug!(AdminUser { id, username, is_superuser, active, created_at, sessions_revoked_at; password_hash });
fn admin_hash_password<'a>(
values: &'a mut Vec<(&'static str, crate::core::SqlValue)>,
before: Option<&'a serde_json::Value>,
) -> super::derived_fields::DeriveFuture<'a> {
Box::pin(async move {
let Some(plain) =
super::derived_fields::take_new_password(values, before, "password_hash")?
else {
return Ok(());
};
let hash = crate::passwords::hash_async(&plain)
.await
.map_err(|e| e.to_string())?;
values.push(("password_hash", crate::core::SqlValue::String(hash)));
Ok::<(), String>(())
})
}
inventory::submit! {
super::derived_fields::AdminDerivedField {
table: "rustango_admin_users",
derive: admin_hash_password,
}
}
inventory::submit! {
super::derived_fields::AdminDerivedField {
table: "rustango_admin_users",
derive: super::derived_fields::revoke_on_deactivate,
}
}
super::object_permissions::lock_user_credentials!(AdminUser, "rustango_admin_users");
#[cfg(feature = "admin-sso")]
crate::register_admin_superuser_fields!(AdminUser, Add, [email]);
#[cfg(feature = "admin-sso")]
crate::register_admin_superuser_fields!(AdminUser, Change, [email]);
impl AdminUser {
#[allow(clippy::disallowed_methods)]
pub fn new_with_password(
username: impl Into<String>,
password: &str,
is_superuser: bool,
) -> Result<Self, crate::passwords::PasswordError> {
Ok(Self {
id: Auto::Unset,
username: username.into(),
password_hash: crate::passwords::hash(password)?,
#[cfg(feature = "admin-sso")]
email: None,
is_superuser,
active: true,
created_at: chrono::Utc::now(),
sessions_revoked_at: None,
})
}
}