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

11
crates/users/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "users"
version = "0.0.0"
edition = "2021"
[dependencies]
database = { path = "../database" }
hash = { path = "../hash" }
id = { path = "../id" }
thiserror = { workspace = true }
chrono = { workspace = true }

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?,
)
}
}

View file

@ -0,0 +1,8 @@
// error
#[derive(thiserror::Error)]
// the rest
#[derive(Debug)]
pub enum Error {
#[error("Database: {0}")]
Database(#[from] database::Error),
}

22
crates/users/src/lib.rs Normal file
View file

@ -0,0 +1,22 @@
mod database;
mod error;
use chrono::{DateTime, Utc};
use id::UserID;
pub use crate::error::Error;
#[derive(Debug)]
pub struct User {
id: UserID,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
is_admin: bool,
username: String,
name: Option<String>,
email: Option<String>,
password: Option<String>,
password_recover: Option<String>,
paper_key: Option<String>,
is_archived: bool,
}