goathacks-backend/src/models/users.rs
Cara Salter 0ccf714d73 aaaa
2025-03-20 14:39:43 -04:00

62 lines
1.8 KiB
Rust

/*! Users are entities that interact with the system.
*
* They are notably separate from any API tokens.
*/
use std::fmt::{Debug, Display};
use argon2::{password_hash::{rand_core::OsRng, SaltString}, Argon2};
use chrono::{DateTime, Utc};
use crate::errors::{EmptyResult, Result};
/** A single User with access to the system
*
* The specific User object contains profile and biographic data that's either useful to ACM or
* required by external partners (see: MLH).
*/
#[derive(Clone)]
struct User {
/// Unique user identifier, a V4 UUID
id: uuid::Uuid,
/// The user's name. Not guaranteed to be anything in particular, should be
/// treated as a standard UTF8 string
name: String,
/// User's (hashed) password
password_hash: String,
/// User's email. This must be validated for correct format!
email: String,
/// Whether the user is allowed to log in.
is_active: bool,
/// UTC date/time of creation
created: chrono::DateTime<Utc>,
/// UTC date/time of last login
last_login: Option<DateTime<Utc>>,
}
impl Display for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.email, self.id)
}
}
impl Debug for User {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.email, self.id)
}
}
impl User {
pub fn new_user() -> Self {
Self { id: uuid::Uuid::new_v4(), name: String::new(), password_hash: String::new(), email: String::new(), is_active: true, created: Utc::now(), last_login: None }
}
pub fn set_password(new_pw: String) -> EmptyResult {
let argon = Argon2::default();
let salt = SaltString::generate(&mut OsRng);
let mut out: Vec<u8> = Vec::new();
Ok(())
}
}