...
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use chrono::{DateTime, Utc};
|
||||
use bcrypt::{hash, verify, DEFAULT_COST};
|
||||
|
||||
/// Represents a user in the system
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -10,6 +11,9 @@ pub struct User {
|
||||
pub name: String,
|
||||
/// User's email address
|
||||
pub email: String,
|
||||
/// User's hashed password
|
||||
#[serde(skip_serializing)]
|
||||
pub password_hash: Option<String>,
|
||||
/// User's role in the system
|
||||
pub role: UserRole,
|
||||
/// When the user was created
|
||||
@@ -34,24 +38,56 @@ impl User {
|
||||
id: None,
|
||||
name,
|
||||
email,
|
||||
password_hash: None,
|
||||
role: UserRole::User,
|
||||
created_at: Some(Utc::now()),
|
||||
updated_at: Some(Utc::now()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new user with a password
|
||||
pub fn new_with_password(name: String, email: String, password: &str) -> Result<Self, bcrypt::BcryptError> {
|
||||
let password_hash = hash(password, DEFAULT_COST)?;
|
||||
|
||||
Ok(Self {
|
||||
id: None,
|
||||
name,
|
||||
email,
|
||||
password_hash: Some(password_hash),
|
||||
role: UserRole::User,
|
||||
created_at: Some(Utc::now()),
|
||||
updated_at: Some(Utc::now()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a new admin user
|
||||
pub fn new_admin(name: String, email: String) -> Self {
|
||||
Self {
|
||||
id: None,
|
||||
name,
|
||||
email,
|
||||
password_hash: None,
|
||||
role: UserRole::Admin,
|
||||
created_at: Some(Utc::now()),
|
||||
updated_at: Some(Utc::now()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new admin user with a password
|
||||
pub fn new_admin_with_password(name: String, email: String, password: &str) -> Result<Self, bcrypt::BcryptError> {
|
||||
let password_hash = hash(password, DEFAULT_COST)?;
|
||||
|
||||
Ok(Self {
|
||||
id: None,
|
||||
name,
|
||||
email,
|
||||
password_hash: Some(password_hash),
|
||||
role: UserRole::Admin,
|
||||
created_at: Some(Utc::now()),
|
||||
updated_at: Some(Utc::now()),
|
||||
})
|
||||
}
|
||||
|
||||
/// Checks if the user is an admin
|
||||
pub fn is_admin(&self) -> bool {
|
||||
self.role == UserRole::Admin
|
||||
@@ -69,6 +105,38 @@ impl User {
|
||||
|
||||
self.updated_at = Some(Utc::now());
|
||||
}
|
||||
|
||||
/// Sets or updates the user's password
|
||||
pub fn set_password(&mut self, password: &str) -> Result<(), bcrypt::BcryptError> {
|
||||
let password_hash = hash(password, DEFAULT_COST)?;
|
||||
self.password_hash = Some(password_hash);
|
||||
self.updated_at = Some(Utc::now());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Verifies if the provided password matches the stored hash
|
||||
pub fn verify_password(&self, password: &str) -> Result<bool, bcrypt::BcryptError> {
|
||||
match &self.password_hash {
|
||||
Some(hash) => verify(password, hash),
|
||||
None => Ok(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents user login credentials
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct LoginCredentials {
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
/// Represents user registration data
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct RegistrationData {
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
pub password_confirmation: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
Reference in New Issue
Block a user