-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from L2-Technology/kill-root-node
remove root node as concept, restructure auth/admin
- Loading branch information
Showing
40 changed files
with
722 additions
and
599 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use sea_orm::{entity::prelude::*, ActiveValue}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::seconds_since_epoch; | ||
|
||
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)] | ||
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")] | ||
pub enum UserRole { | ||
#[sea_orm(num_value = 0)] | ||
Default, | ||
} | ||
|
||
impl From<UserRole> for i16 { | ||
fn from(role: UserRole) -> i16 { | ||
match role { | ||
UserRole::Default => 0, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] | ||
pub struct Entity; | ||
|
||
impl EntityName for Entity { | ||
fn table_name(&self) -> &str { | ||
"user" | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Serialize, Deserialize)] | ||
pub struct Model { | ||
pub id: String, | ||
pub role: i16, | ||
pub username: String, | ||
pub hashed_password: String, | ||
pub created_at: i64, | ||
pub updated_at: i64, | ||
} | ||
|
||
impl Model { | ||
pub fn get_role(&self) -> UserRole { | ||
match self.role { | ||
0 => UserRole::Default, | ||
_ => panic!("invalid role"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] | ||
pub enum Column { | ||
Id, | ||
Role, | ||
Username, | ||
HashedPassword, | ||
CreatedAt, | ||
UpdatedAt, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] | ||
pub enum PrimaryKey { | ||
Id, | ||
} | ||
|
||
impl PrimaryKeyTrait for PrimaryKey { | ||
type ValueType = String; | ||
fn auto_increment() -> bool { | ||
false | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter)] | ||
pub enum Relation {} | ||
|
||
impl ColumnTrait for Column { | ||
type EntityName = Entity; | ||
fn def(&self) -> ColumnDef { | ||
match self { | ||
Self::Id => ColumnType::String(None).def().unique(), | ||
Self::Role => ColumnType::SmallInteger.def(), | ||
Self::Username => ColumnType::String(None).def().unique(), | ||
Self::HashedPassword => ColumnType::String(None).def(), | ||
Self::CreatedAt => ColumnType::BigInteger.def(), | ||
Self::UpdatedAt => ColumnType::BigInteger.def(), | ||
} | ||
} | ||
} | ||
|
||
impl RelationTrait for Relation { | ||
fn def(&self) -> RelationDef { | ||
panic!("No RelationDef") | ||
} | ||
} | ||
|
||
impl ActiveModelBehavior for ActiveModel { | ||
fn new() -> Self { | ||
Self { | ||
id: ActiveValue::Set(Uuid::new_v4().to_string()), | ||
role: ActiveValue::Set(UserRole::Default.into()), | ||
..<Self as ActiveModelTrait>::default() | ||
} | ||
} | ||
|
||
fn before_save(mut self, insert: bool) -> Result<Self, DbErr> { | ||
let now: i64 = seconds_since_epoch(); | ||
self.updated_at = ActiveValue::Set(now); | ||
if insert { | ||
self.created_at = ActiveValue::Set(now); | ||
} | ||
Ok(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use sea_schema::migration::prelude::*; | ||
pub struct Migration; | ||
|
||
impl MigrationName for Migration { | ||
fn name(&self) -> &str { | ||
"m20220808_000001_create_users_table" | ||
} | ||
} | ||
|
||
#[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).string().not_null().primary_key()) | ||
.col(ColumnDef::new(User::Role).small_integer().not_null()) | ||
.col( | ||
ColumnDef::new(User::Username) | ||
.string() | ||
.unique_key() | ||
.not_null(), | ||
) | ||
.col(ColumnDef::new(User::HashedPassword).string().not_null()) | ||
.col(ColumnDef::new(User::CreatedAt).big_integer().not_null()) | ||
.col(ColumnDef::new(User::UpdatedAt).big_integer().not_null()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
let mut stmt = Table::drop(); | ||
stmt.table(User::Table); | ||
manager.drop_table(stmt).await | ||
} | ||
} | ||
|
||
#[derive(Iden)] | ||
enum User { | ||
Table, | ||
Id, | ||
Role, | ||
Username, | ||
HashedPassword, | ||
CreatedAt, | ||
UpdatedAt, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.