|
1 | 1 | //! Contains the definition for what a [`User`] row in the [`Database`](sqlx::Database) is. |
| 2 | +
|
| 3 | +#![allow(clippy::std_instead_of_core)] |
| 4 | + |
| 5 | +mod auth_user; |
| 6 | + |
| 7 | +use sqlx::FromRow; |
| 8 | +use winvoice_schema::Id; |
| 9 | + |
| 10 | +/// Corresponds to the `users` table in the [`winvoice-server`](crate) database. |
| 11 | +#[derive(Clone, Debug, Default, Eq, FromRow, Hash, Ord, PartialEq, PartialOrd)] |
| 12 | +pub struct User |
| 13 | +{ |
| 14 | + /// The [`User`]'s [`Employee`](winvoice_schema::Employee) [`Id`], if they are employed. |
| 15 | + employee_id: Option<Id>, |
| 16 | + |
| 17 | + /// The [`Id`] of the [`User`]. |
| 18 | + id: Id, |
| 19 | + |
| 20 | + /// The role of the [`User`]. Controls permissions. |
| 21 | + role: String, |
| 22 | + |
| 23 | + /// Get the [`User`]'s [`argon2`]-hashed password. |
| 24 | + password: String, |
| 25 | + |
| 26 | + /// Get the [`User`]'s username. |
| 27 | + username: String, |
| 28 | +} |
| 29 | + |
| 30 | +impl User |
| 31 | +{ |
| 32 | + /// Create a new [`User`]. |
| 33 | + pub const fn new( |
| 34 | + employee_id: Option<Id>, |
| 35 | + id: Id, |
| 36 | + role: String, |
| 37 | + password: String, |
| 38 | + username: String, |
| 39 | + ) -> Self |
| 40 | + { |
| 41 | + Self { employee_id, id, role, password, username } |
| 42 | + } |
| 43 | + |
| 44 | + /// The [`User`]'s [`Employee`](winvoice_schema::Employee) [`Id`], if they are employed. |
| 45 | + pub const fn employee_id(&self) -> Option<i64> |
| 46 | + { |
| 47 | + self.employee_id |
| 48 | + } |
| 49 | + |
| 50 | + /// The [`Id`] of the [`User`]. |
| 51 | + pub const fn id(&self) -> i64 |
| 52 | + { |
| 53 | + self.id |
| 54 | + } |
| 55 | + |
| 56 | + /// The role of the [`User`]. Controls permissions. |
| 57 | + pub fn role(&self) -> &str |
| 58 | + { |
| 59 | + self.role.as_ref() |
| 60 | + } |
| 61 | + |
| 62 | + /// Get the [`User`]'s [`argon2`]-hashed password. |
| 63 | + pub fn password(&self) -> &str |
| 64 | + { |
| 65 | + self.password.as_ref() |
| 66 | + } |
| 67 | + |
| 68 | + /// Get the [`User`]'s username. |
| 69 | + pub fn username(&self) -> &str |
| 70 | + { |
| 71 | + self.username.as_ref() |
| 72 | + } |
| 73 | +} |
0 commit comments