From d07611d044c701d3560d427c455b24ad766190f5 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Wed, 20 Apr 2022 00:21:49 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1ade=20las=20entidades=20asociadas=20a=20?= =?UTF-8?q?roles=20y=20usuarios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop-user/src/entity/mod.rs | 3 ++ pagetop-user/src/entity/role.rs | 41 ++++++++++++++++++++ pagetop-user/src/entity/role_permission.rs | 35 +++++++++++++++++ pagetop-user/src/entity/user.rs | 43 +++++++++++++++------ pagetop-user/src/entity/user_role.rs | 45 ++++++++++++++++++++++ 5 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 pagetop-user/src/entity/role.rs create mode 100644 pagetop-user/src/entity/role_permission.rs create mode 100644 pagetop-user/src/entity/user_role.rs diff --git a/pagetop-user/src/entity/mod.rs b/pagetop-user/src/entity/mod.rs index 22d12a38..a8287bfe 100644 --- a/pagetop-user/src/entity/mod.rs +++ b/pagetop-user/src/entity/mod.rs @@ -1 +1,4 @@ +pub mod role; +pub mod role_permission; pub mod user; +pub mod user_role; diff --git a/pagetop-user/src/entity/role.rs b/pagetop-user/src/entity/role.rs new file mode 100644 index 00000000..2520ebe8 --- /dev/null +++ b/pagetop-user/src/entity/role.rs @@ -0,0 +1,41 @@ +use pagetop::db::entity::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "role")] +pub struct Model { + #[sea_orm(primary_key)] + pub rid : u32, + #[sea_orm(unique)] + pub name : String, + #[sea_orm(default_value = 0)] + pub weight: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + RolePermission, + UserRole, +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::RolePermission => Entity::has_many(super::role_permission::Entity).into(), + Self::UserRole => Entity::has_many(super::user_role::Entity).into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::RolePermission.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::UserRole.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/pagetop-user/src/entity/role_permission.rs b/pagetop-user/src/entity/role_permission.rs new file mode 100644 index 00000000..2c2859d1 --- /dev/null +++ b/pagetop-user/src/entity/role_permission.rs @@ -0,0 +1,35 @@ +use pagetop::db::entity::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "role_permission")] +pub struct Model { + #[sea_orm(primary_key)] + pub rid : u32, + #[sea_orm(primary_key)] + pub permission: String, + pub module : String, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Role, +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Role => Entity::belongs_to(super::role::Entity) + .from(Column::Rid) + .to(super::role::Column::Rid) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Role.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/pagetop-user/src/entity/user.rs b/pagetop-user/src/entity/user.rs index d0db4a98..8bec2f1e 100644 --- a/pagetop-user/src/entity/user.rs +++ b/pagetop-user/src/entity/user.rs @@ -1,18 +1,39 @@ -use crate::db::entity::*; -use serde::{Deserialize, Serialize}; +use pagetop::db::entity::*; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[sea_orm(table_name = "user")] pub struct Model { - #[sea_orm(primary_key)] - #[serde(skip_deserializing)] - pub id: i32, - pub title: String, - #[sea_orm(column_type = "Text")] - pub text: String, + #[sea_orm(primary_key, auto_increment = false)] + pub uid : u32, + #[sea_orm(unique)] + pub name : String, + pub pass : String, + pub mail : Option, + pub created : DateTimeUtc, + pub changed : DateTimeUtc, + pub access : DateTimeUtc, + pub login : DateTimeUtc, + pub status : bool, + pub timezone: Option, } -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + UserRole, +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::UserRole => Entity::has_many(super::user_role::Entity).into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::UserRole.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/pagetop-user/src/entity/user_role.rs b/pagetop-user/src/entity/user_role.rs new file mode 100644 index 00000000..23a6af8f --- /dev/null +++ b/pagetop-user/src/entity/user_role.rs @@ -0,0 +1,45 @@ +use pagetop::db::entity::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "user_role")] +pub struct Model { + #[sea_orm(primary_key)] + pub uid: u32, + #[sea_orm(primary_key)] + pub rid: u32, +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + User, + Role, +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::User => Entity::belongs_to(super::user::Entity) + .from(Column::Uid) + .to(super::user::Column::Uid) + .into(), + Self::Role => Entity::belongs_to(super::role::Entity) + .from(Column::Rid) + .to(super::role::Column::Rid) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Role.def() + } +} + +impl ActiveModelBehavior for ActiveModel {}