feat: add simple cli user management

This commit is contained in:
Jef Roosens 2025-01-12 10:05:38 +01:00
parent 3800534a51
commit cc5f7856f6
Signed by: Jef Roosens
GPG key ID: 21FD3D77D56BAF49
3 changed files with 47 additions and 1 deletions

View file

@ -51,6 +51,22 @@ impl User {
.verify_password(password.as_ref().as_bytes(), &password_hash)
.is_ok()
}
pub fn all(pool: &DbPool) -> Result<Vec<Self>, DbError> {
let conn = pool.get()?;
let mut stmt = conn.prepare("select * from users")?;
let users: Result<Vec<_>, _> = stmt.query_map((), Self::from_row)?.collect();
Ok(users?)
}
pub fn remove_by_username(pool: &DbPool, username: impl AsRef<str>) -> Result<bool, DbError> {
let conn = pool.get()?;
let mut stmt = conn.prepare("delete from users where username = $1")?;
Ok(stmt.execute((username.as_ref(),))? > 0)
}
}
fn hash_password(password: impl AsRef<str>) -> String {