users: migrations, queries, users crate: create user, get first admin user

This commit is contained in:
Philippe Loctaux 2023-03-05 23:31:10 +01:00
parent 8af226cd05
commit 3e168c19bc
13 changed files with 283 additions and 1 deletions

View file

@ -0,0 +1,45 @@
use crate::error::Error;
use crate::User;
use database::sqlx::SqliteExecutor;
use database::Users as DatabaseUsers;
use hash::Password;
use id::UserID;
impl From<DatabaseUsers> for User {
fn from(db: DatabaseUsers) -> Self {
Self {
id: UserID(db.id),
created_at: db.created_at,
updated_at: db.updated_at,
is_admin: db.is_admin,
username: db.username,
name: db.name,
email: db.email,
password: db.password,
password_recover: db.password_recover,
paper_key: db.paper_key,
is_archived: db.is_archived,
}
}
}
impl User {
pub async fn get_initial_admin(conn: impl SqliteExecutor<'_>) -> Result<Option<Self>, Error> {
Ok(DatabaseUsers::get_initial_admin(conn)
.await?
.map(Self::from))
}
pub async fn insert(
conn: impl SqliteExecutor<'_>,
id: &UserID,
is_admin: bool,
username: &str,
password: Option<&Password>,
) -> Result<Option<()>, Error> {
Ok(
DatabaseUsers::insert(conn, &id.0, is_admin, username, password.map(|p| p.hash()))
.await?,
)
}
}