Skip to main content

reinhardt_admin/server/
user.rs

1//! Admin default user type defined via `#[user]` macro.
2//!
3//! Provides the internal user type used by admin server functions for
4//! authentication and permission checking. This replaces the deprecated
5//! `DefaultUser` from `reinhardt-auth`.
6
7use chrono::{DateTime, Utc};
8use reinhardt_db::orm::Model;
9use reinhardt_macros::user;
10use serde::{Deserialize, Serialize};
11use uuid::Uuid;
12
13/// Internal user type for admin server function authentication.
14///
15/// This struct mirrors the fields of the deprecated `DefaultUser` but is
16/// defined using the `#[user]` macro, which generates the required trait
17/// implementations (`BaseUser`, `FullUser`, `PermissionsMixin`, etc.).
18/// `Model` is implemented manually since admin operates on the same
19/// `auth_user` table as the original `DefaultUser`.
20// `manager = false`: admin user CRUD is delegated to `reinhardt-admin`'s own
21// query layer; the auto-generated in-memory manager would be unused and would
22// require `Default` on `AdminDefaultUser`, which is not currently derivable.
23#[user(hasher = reinhardt_auth::Argon2Hasher, username_field = "username", full = true, manager = false)]
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct AdminDefaultUser {
26	/// Unique identifier.
27	pub id: Uuid,
28	/// Login username.
29	pub username: String,
30	/// Email address.
31	pub email: String,
32	/// First name.
33	pub first_name: String,
34	/// Last name.
35	pub last_name: String,
36	/// Hashed password (None if not set).
37	pub password_hash: Option<String>,
38	/// Timestamp of last login.
39	pub last_login: Option<DateTime<Utc>>,
40	/// Whether the user account is active.
41	pub is_active: bool,
42	/// Whether the user has staff privileges.
43	pub is_staff: bool,
44	/// Whether the user has superuser privileges.
45	pub is_superuser: bool,
46	/// Timestamp when the user joined.
47	pub date_joined: DateTime<Utc>,
48	/// List of permission codenames assigned to this user.
49	#[serde(deserialize_with = "super::serde_helpers::string_or_vec", default)]
50	pub user_permissions: Vec<String>,
51	/// List of group names this user belongs to.
52	#[serde(deserialize_with = "super::serde_helpers::string_or_vec", default)]
53	pub groups: Vec<String>,
54}
55
56/// Fields struct for `AdminDefaultUser` ORM operations.
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct AdminDefaultUserFields {
59	_private: (),
60}
61
62impl AdminDefaultUserFields {
63	fn new() -> Self {
64		Self { _private: () }
65	}
66}
67
68impl reinhardt_db::orm::FieldSelector for AdminDefaultUserFields {
69	fn with_alias(self, _alias: &str) -> Self {
70		self
71	}
72}
73
74impl Model for AdminDefaultUser {
75	type PrimaryKey = Uuid;
76	type Fields = AdminDefaultUserFields;
77	type Objects = reinhardt_db::orm::Manager<Self>;
78
79	fn table_name() -> &'static str {
80		"auth_user"
81	}
82
83	fn new_fields() -> Self::Fields {
84		AdminDefaultUserFields::new()
85	}
86
87	fn primary_key(&self) -> Option<Self::PrimaryKey> {
88		Some(self.id)
89	}
90
91	fn set_primary_key(&mut self, value: Self::PrimaryKey) {
92		self.id = value;
93	}
94
95	fn primary_key_field() -> &'static str {
96		"id"
97	}
98}