Add user model

This commit is contained in:
D. Scott Boggs 2023-06-26 07:55:12 -04:00
parent a390f79a75
commit 0a197db93f
9 changed files with 154 additions and 2 deletions

View file

@ -6,3 +6,4 @@ pub mod groups;
pub mod ticks;
pub mod track2_groups;
pub mod tracks;
pub mod user;

View file

@ -4,3 +4,4 @@ pub use super::groups::Entity as Groups;
pub use super::ticks::Entity as Ticks;
pub use super::track2_groups::Entity as Track2Groups;
pub use super::tracks::Entity as Tracks;
pub use super::user::Entity as User;

View file

@ -0,0 +1,53 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
use std::default::default;
use bcrypt::*;
use either::Either::{self, Left, Right};
use rocket::response::status::Unauthorized;
use sea_orm::entity::prelude::*;
use crate::{
api::ErrorResponder,
error::{self, Error},
};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "user")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub password_hash: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
impl ActiveModel {
pub fn new(name: String, password: String) -> error::Result<Self> {
use sea_orm::ActiveValue::Set;
let name = Set(name);
let password_hash = Set(hash(password, DEFAULT_COST + 2)?);
Ok(Self {
name,
password_hash,
..default()
})
}
}
impl Model {
pub fn check_password(
self,
password: String,
) -> std::result::Result<Self, Either<Unauthorized<()>, ErrorResponder>> {
match verify(password, &self.password_hash) {
Ok(true) => Ok(self),
Ok(false) => Err(Left(Unauthorized(None))),
Err(err) => Err(Right(Error::from(err).into())),
}
}
}