forked from TWS/kalkutago
Add user model
This commit is contained in:
parent
e25301655b
commit
60d7ce4664
9 changed files with 154 additions and 2 deletions
|
|
@ -2,11 +2,11 @@ use crate::error::Error;
|
|||
|
||||
#[derive(Responder)]
|
||||
#[response(status = 500, content_type = "json")]
|
||||
pub(crate) struct ErrorResponder {
|
||||
pub struct ErrorResponder {
|
||||
message: String,
|
||||
}
|
||||
|
||||
pub(crate) type ApiResult<T> = Result<T, ErrorResponder>;
|
||||
pub type ApiResult<T> = Result<T, ErrorResponder>;
|
||||
|
||||
// The following impl's are for easy conversion of error types.
|
||||
|
||||
|
|
|
|||
|
|
@ -6,3 +6,4 @@ pub mod groups;
|
|||
pub mod ticks;
|
||||
pub mod track2_groups;
|
||||
pub mod tracks;
|
||||
pub mod user;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
53
server/src/entities/user.rs
Normal file
53
server/src/entities/user.rs
Normal 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())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
use std::string;
|
||||
|
||||
use bcrypt::BcryptError;
|
||||
use derive_builder::UninitializedFieldError;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
|
@ -18,6 +19,8 @@ pub enum Error {
|
|||
Utf8(#[from] string::FromUtf8Error),
|
||||
#[error(transparent)]
|
||||
ChannelSendError(#[from] tokio::sync::broadcast::error::SendError<crate::api::update::Update>),
|
||||
#[error(transparent)]
|
||||
Bcrypt(#[from] BcryptError),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
|
|
|||
42
server/src/migrator/m20230626_083036_create_users_table.rs
Normal file
42
server/src/migrator/m20230626_083036_create_users_table.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(User::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(User::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(User::Name).string().not_null())
|
||||
.col(ColumnDef::new(User::PasswordHash).string().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(User::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
/// Learn more at https://docs.rs/sea-query#iden
|
||||
#[derive(Iden)]
|
||||
enum User {
|
||||
Table,
|
||||
Id,
|
||||
Name,
|
||||
PasswordHash,
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ mod m20230606_000001_create_tracks_table;
|
|||
mod m20230606_000002_create_ticks_table;
|
||||
mod m20230606_000003_create_groups_table;
|
||||
mod m20230606_000004_create_track2groups_table;
|
||||
mod m20230626_083036_create_users_table;
|
||||
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
|
|
@ -15,6 +16,7 @@ impl MigratorTrait for Migrator {
|
|||
Box::new(m20230606_000002_create_ticks_table::Migration),
|
||||
Box::new(m20230606_000003_create_groups_table::Migration),
|
||||
Box::new(m20230606_000004_create_track2groups_table::Migration),
|
||||
Box::new(m20230626_083036_create_users_table::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue