Skip to main content

reinhardt_auth/core/
user.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4/// User trait - Core authentication trait
5///
6/// This trait defines the basic interface that all user types must implement
7/// in the Reinhardt authentication system. It provides methods for checking
8/// user status and permissions.
9///
10/// # Examples
11///
12/// ```
13/// use reinhardt_auth::{User, SimpleUser};
14/// use uuid::Uuid;
15///
16/// let user = SimpleUser {
17///     id: Uuid::new_v4(),
18///     username: "alice".to_string(),
19///     email: "alice@example.com".to_string(),
20///     is_active: true,
21///     is_admin: false,
22///     is_staff: false,
23///     is_superuser: false,
24/// };
25///
26/// assert!(user.is_authenticated());
27/// assert!(user.is_active());
28/// assert_eq!(user.username(), "alice");
29/// ```
30#[deprecated(
31	since = "0.1.0-rc.15",
32	note = "Use AuthIdentity + BaseUser/FullUser + PermissionsMixin instead"
33)]
34pub trait User: Send + Sync {
35	/// Returns the unique identifier for this user
36	fn id(&self) -> String;
37
38	/// Returns the username for this user
39	fn username(&self) -> &str;
40
41	/// Returns the username (alias for `username()`)
42	///
43	/// This method exists for Django compatibility.
44	fn get_username(&self) -> &str {
45		self.username()
46	}
47
48	/// Returns whether this user is authenticated
49	///
50	/// For concrete user types, this should always return `true`.
51	/// `AnonymousUser` should return `false`.
52	fn is_authenticated(&self) -> bool;
53
54	/// Returns whether this user account is active
55	///
56	/// Inactive users cannot log in and should be denied access.
57	fn is_active(&self) -> bool;
58
59	/// Returns whether this user is an administrator
60	///
61	/// Admin users typically have elevated privileges in the system.
62	fn is_admin(&self) -> bool;
63
64	/// Returns whether this user is a staff member
65	///
66	/// Staff members typically have access to the admin interface.
67	fn is_staff(&self) -> bool;
68
69	/// Returns whether this user is a superuser
70	///
71	/// Superusers have all permissions without explicit assignment.
72	fn is_superuser(&self) -> bool;
73}
74
75/// Simple user implementation with basic fields
76///
77/// This is a lightweight user struct suitable for most applications.
78///
79/// # Examples
80///
81/// ```
82/// use reinhardt_auth::{User, SimpleUser};
83/// use uuid::Uuid;
84///
85/// let user = SimpleUser {
86///     id: Uuid::new_v4(),
87///     username: "bob".to_string(),
88///     email: "bob@example.com".to_string(),
89///     is_active: true,
90///     is_admin: true,
91///     is_staff: true,
92///     is_superuser: false,
93/// };
94///
95/// assert!(user.is_authenticated());
96/// assert!(user.is_admin());
97/// assert!(user.is_staff());
98/// ```
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100pub struct SimpleUser {
101	/// Unique identifier for the user.
102	pub id: Uuid,
103	/// The user's login name.
104	pub username: String,
105	/// The user's email address.
106	pub email: String,
107	/// Whether the user account is active.
108	pub is_active: bool,
109	/// Whether the user has admin privileges.
110	pub is_admin: bool,
111	/// Whether the user is a staff member.
112	pub is_staff: bool,
113	/// Whether the user has superuser privileges.
114	pub is_superuser: bool,
115}
116
117#[allow(deprecated)] // Implementing deprecated User trait for backward compatibility
118impl User for SimpleUser {
119	fn id(&self) -> String {
120		self.id.to_string()
121	}
122
123	fn username(&self) -> &str {
124		&self.username
125	}
126
127	fn is_authenticated(&self) -> bool {
128		true
129	}
130
131	fn is_active(&self) -> bool {
132		self.is_active
133	}
134
135	fn is_admin(&self) -> bool {
136		self.is_admin
137	}
138
139	fn is_staff(&self) -> bool {
140		self.is_staff
141	}
142
143	fn is_superuser(&self) -> bool {
144		self.is_superuser
145	}
146}
147
148/// Anonymous user - represents a non-authenticated visitor
149///
150/// This type is used to represent users who are not logged in.
151/// All permission checks return `false`, and `is_authenticated()` returns `false`.
152///
153/// # Examples
154///
155/// ```
156/// use reinhardt_auth::{User, AnonymousUser};
157///
158/// let anon = AnonymousUser;
159///
160/// assert!(!anon.is_authenticated());
161/// assert!(!anon.is_active());
162/// assert!(!anon.is_admin());
163/// assert_eq!(anon.username(), "");
164/// assert_eq!(anon.id(), "");
165/// ```
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct AnonymousUser;
168
169#[allow(deprecated)] // Implementing deprecated User trait for backward compatibility
170impl User for AnonymousUser {
171	fn id(&self) -> String {
172		String::new()
173	}
174
175	fn username(&self) -> &str {
176		""
177	}
178
179	fn is_authenticated(&self) -> bool {
180		false
181	}
182
183	fn is_active(&self) -> bool {
184		false
185	}
186
187	fn is_admin(&self) -> bool {
188		false
189	}
190
191	fn is_staff(&self) -> bool {
192		false
193	}
194
195	fn is_superuser(&self) -> bool {
196		false
197	}
198}