minauthator/lib/kernel/src/models/user.rs

62 lines
1.5 KiB
Rust
Raw Normal View History

2024-10-21 00:05:20 +02:00
use fully_pub::fully_pub;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::types::Json;
use utils::get_random_human_token;
use uuid::Uuid;
2024-10-21 00:05:20 +02:00
#[derive(sqlx::Type, Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(strum_macros::Display)]
#[fully_pub]
enum UserStatus {
Disabled,
Invited,
Active
2024-10-21 00:05:20 +02:00
}
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
2024-10-21 00:05:20 +02:00
#[fully_pub]
struct User {
/// uuid
id: String,
handle: String,
full_name: Option<String>,
email: Option<String>,
website: Option<String>,
2024-12-04 18:25:56 +01:00
avatar_asset_id: Option<String>,
2024-10-21 00:05:20 +02:00
password_hash: Option<String>, // argon2 password hash
status: UserStatus,
roles: Json<Vec<String>>,
reset_password_token: Option<String>,
2024-10-21 00:05:20 +02:00
last_login_at: Option<DateTime<Utc>>,
created_at: DateTime<Utc>
}
impl User {
pub fn new(
handle: String
) -> User {
User {
id: Uuid::new_v4().to_string(),
handle,
full_name: None,
email: None,
website: None,
2024-12-04 18:25:56 +01:00
avatar_asset_id: None,
password_hash: None,
status: UserStatus::Disabled,
roles: Json(Vec::new()),
reset_password_token: None,
last_login_at: None,
created_at: Utc::now()
}
}
pub fn invite(self: &mut Self) {
self.reset_password_token = Some(get_random_human_token());
self.status = UserStatus::Invited;
}
}