Skip to content

Commit

Permalink
Merge pull request #707 from RWDai/chores(basic)-simplify-domain-code
Browse files Browse the repository at this point in the history
chores(basic):simplify domain code
  • Loading branch information
gudaoxuri authored Apr 24, 2024
2 parents 5ef8305 + 17ad462 commit 5d9724e
Show file tree
Hide file tree
Showing 43 changed files with 333 additions and 1,649 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ run_script = { version = "0.10" }
testcontainers-modules = { version = "0.3", features = ["redis"] }
strum = { version = "0.26", features = ["derive"] }
# tardis
tardis = { version = "0.1.0-rc.11" }
# tardis = { version = "0.1.0-rc.11" }
# tardis = { path = "../tardis/tardis" }
# tardis = { git = "https://github.com/ideal-world/tardis.git", rev = "191f3ec" }
tardis = { git = "https://github.com/ideal-world/tardis.git", rev = "694ff92" }
#spacegate

# spacegate-kernel = { git = "https://github.com/ideal-world/spacegate.git", rev = "f37a81a", features = [
Expand Down
8 changes: 3 additions & 5 deletions backend/basic/src/helper/request_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ pub async fn try_set_real_ip_from_req_to_ctx(request: &Request, ctx: &TardisCont
pub fn parse_forwarded_ip(forwarded_value: &str) -> Option<IpAddr> {
forwarded_value
.strip_prefix("Forwarded: ")
.map(|forwarded_value| {
.and_then(|forwarded_value| {
forwarded_value
.split(";")
.split(';')
.find(|part| part.trim().starts_with("for="))
.map(|part| part.trim()[4..].split(",").next().map(|ip_str| IpAddr::from_str(ip_str).ok()).flatten())
.flatten()
.and_then(|part| part.trim()[4..].split(',').next().and_then(|ip_str| IpAddr::from_str(ip_str).ok()))
})
.flatten()
}

/// Try to get real ip from request
Expand Down
76 changes: 9 additions & 67 deletions backend/basic/src/rbum/domain/rbum_cert.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use tardis::basic::dto::TardisContext;
use tardis::chrono::{self, Utc};
use tardis::db::reldb_client::TardisActiveModel;
use tardis::db::sea_orm;
use tardis::db::sea_orm::prelude::*;
use tardis::db::sea_orm::sea_query::{ColumnDef, IndexCreateStatement, Table, TableCreateStatement};
use tardis::db::sea_orm::*;
use tardis::TardisCreateIndex;
use tardis::{TardisCreateEntity, TardisEmptyBehavior, TardisEmptyRelation};

/// Certification model
///
Expand All @@ -20,8 +17,9 @@ use tardis::TardisCreateIndex;
/// you can use this model directly without associating the credential configuration.
/// For example, data connection credentials, depending on business requirements, may not require credential configuration.
///
///
/// NOTE: 如果不需要对凭证作统一的校验处理,可以直接使用此模型,不用关联凭证配置。比如数据连接凭证,视业务需求也可以不需要凭证配置。
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateIndex)]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateEntity, TardisEmptyBehavior, TardisEmptyRelation)]
#[sea_orm(table_name = "rbum_cert")]
pub struct Model {
/// Certification id
Expand Down Expand Up @@ -138,72 +136,16 @@ pub struct Model {
#[index(index_id = "id")]
pub rel_rbum_id: String,

#[fill_ctx(fill = "own_paths")]
pub own_paths: String,
#[fill_ctx]
pub owner: String,
#[sea_orm(extra = "DEFAULT CURRENT_TIMESTAMP")]
pub create_time: chrono::DateTime<Utc>,
#[sea_orm(extra = "DEFAULT CURRENT_TIMESTAMP")]
pub update_time: chrono::DateTime<Utc>,
#[fill_ctx]
pub create_by: String,
#[fill_ctx(insert_only = false)]
pub update_by: String,
}

impl TardisActiveModel for ActiveModel {
fn fill_ctx(&mut self, ctx: &TardisContext, is_insert: bool) {
if is_insert {
self.own_paths = Set(ctx.own_paths.to_string());
self.owner = Set(ctx.owner.to_string());
self.create_by = Set(ctx.owner.to_string());
}
self.update_by = Set(ctx.owner.to_string());
}

fn create_table_statement(db: DbBackend) -> TableCreateStatement {
let mut builder = Table::create();
builder
.table(Entity.table_ref())
.if_not_exists()
.col(ColumnDef::new(Column::Id).not_null().string().primary_key())
// Specific
.col(ColumnDef::new(Column::Kind).not_null().string())
.col(ColumnDef::new(Column::Supplier).not_null().string())
.col(ColumnDef::new(Column::Ak).not_null().string())
.col(ColumnDef::new(Column::Sk).not_null().string())
.col(ColumnDef::new(Column::SkInvisible).not_null().boolean().default(false))
.col(ColumnDef::new(Column::Ext).not_null().string())
.col(ColumnDef::new(Column::ConnUri).not_null().string())
.col(ColumnDef::new(Column::RelRbumCertConfId).not_null().string())
.col(ColumnDef::new(Column::RelRbumKind).not_null().small_integer())
.col(ColumnDef::new(Column::RelRbumId).not_null().string())
// Basic
.col(ColumnDef::new(Column::OwnPaths).not_null().string())
.col(ColumnDef::new(Column::Owner).not_null().string())
.col(ColumnDef::new(Column::Status).not_null().small_integer())
.col(ColumnDef::new(Column::CreateBy).not_null().string())
.col(ColumnDef::new(Column::UpdateBy).not_null().string());
if db == DatabaseBackend::Postgres {
builder
.col(ColumnDef::new(Column::StartTime).not_null().timestamp_with_time_zone())
.col(ColumnDef::new(Column::EndTime).not_null().timestamp_with_time_zone())
.col(ColumnDef::new(Column::CreateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone())
.col(ColumnDef::new(Column::UpdateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone());
} else {
builder
.engine("InnoDB")
.character_set("utf8mb4")
.collate("utf8mb4_0900_as_cs")
.col(ColumnDef::new(Column::StartTime).not_null().date_time())
.col(ColumnDef::new(Column::EndTime).not_null().date_time())
.col(ColumnDef::new(Column::CreateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp())
.col(ColumnDef::new(Column::UpdateTime).extra("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP".to_string()).timestamp());
}
builder.to_owned()
}

fn create_index_statement() -> Vec<IndexCreateStatement> {
tardis_create_index_statement()
}
}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
87 changes: 11 additions & 76 deletions backend/basic/src/rbum/domain/rbum_cert_conf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use tardis::basic::dto::TardisContext;
use tardis::chrono::{self, Utc};
use tardis::db::reldb_client::TardisActiveModel;
use tardis::db::sea_orm;
use tardis::db::sea_orm::prelude::*;
use tardis::db::sea_orm::sea_query::{ColumnDef, IndexCreateStatement, Table, TableCreateStatement};
use tardis::db::sea_orm::*;
use tardis::TardisCreateIndex;
use tardis::{TardisCreateEntity, TardisEmptyBehavior, TardisEmptyRelation};

/// Certificate configuration model
///
Expand All @@ -20,7 +17,7 @@ use tardis::TardisCreateIndex;
/// It is required that the same binding subject has the same certificate configuration type [`Self::kind`] and certificate configuration supplier [`Self::supplier`] unique.
///
/// 凭证配置绑定的主体可以是某一资源域[`Self::rel_rbum_domain_id`]或资源项[`Self::rel_rbum_item_id`]。要求同一绑定主体下同一凭证配置类型[`Self::kind`]及凭证配置供应商[`Self::supplier`]唯一。
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateIndex)]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateEntity, TardisEmptyBehavior, TardisEmptyRelation)]
#[sea_orm(table_name = "rbum_cert_conf")]
pub struct Model {
/// Certificate configuration id
Expand All @@ -35,6 +32,7 @@ pub struct Model {
/// Used for the classification of certificates, such as: ldap, userpwd, token, oauth2, etc.
///
/// 用于凭证的分类,比如:ldap、userpwd、token、oauth2等。
#[tardis_entity(custom_len = "127")]
#[index(index_id = "id_2", unique)]
pub kind: String,
/// Certificate configuration supplier
Expand All @@ -45,6 +43,7 @@ pub struct Model {
/// For example, the certificate of type oauth2 can be further refined into WeChat oauth2, QQ oauth2, Weibo oauth2, etc.
///
/// 一种凭证类型可以有多个供应商。比如 oauth2 类型的凭证,可以进一步细化成 微信oauth2、QQ oauth2、微博 oauth2 等。
#[tardis_entity(custom_len = "127")]
#[index(index_id = "id_2", unique)]
pub supplier: String,
/// Certificate configuration name
Expand Down Expand Up @@ -100,6 +99,7 @@ pub struct Model {
/// Such as database connection pool configuration.
///
/// 比如数据库连接池配置。
#[tardis_entity(custom_type = "text")]
pub ext: String,
/// Whether sk is required
///
Expand Down Expand Up @@ -225,81 +225,16 @@ pub struct Model {
pub rel_rbum_item_id: String,

#[index()]
#[fill_ctx(fill = "own_paths")]
pub own_paths: String,
#[fill_ctx]
pub owner: String,
#[sea_orm(extra = "DEFAULT CURRENT_TIMESTAMP")]
pub create_time: chrono::DateTime<Utc>,
#[sea_orm(extra = "DEFAULT CURRENT_TIMESTAMP")]
pub update_time: chrono::DateTime<Utc>,
#[fill_ctx]
pub create_by: String,
#[fill_ctx(insert_only = false)]
pub update_by: String,
}

impl TardisActiveModel for ActiveModel {
fn fill_ctx(&mut self, ctx: &TardisContext, is_insert: bool) {
if is_insert {
self.own_paths = Set(ctx.own_paths.to_string());
self.owner = Set(ctx.owner.to_string());
self.create_by = Set(ctx.owner.to_string());
}
self.update_by = Set(ctx.owner.to_string());
}

fn create_table_statement(db: DbBackend) -> TableCreateStatement {
let mut builder = Table::create();
builder
.table(Entity.table_ref())
.if_not_exists()
.col(ColumnDef::new(Column::Id).not_null().string().primary_key())
// Specific
.col(ColumnDef::new(Column::Kind).not_null().string_len(127))
.col(ColumnDef::new(Column::Supplier).not_null().string_len(127))
.col(ColumnDef::new(Column::Name).not_null().string())
.col(ColumnDef::new(Column::Note).not_null().string())
.col(ColumnDef::new(Column::AkNote).not_null().string())
.col(ColumnDef::new(Column::AkRule).not_null().string())
.col(ColumnDef::new(Column::SkNote).not_null().string())
.col(ColumnDef::new(Column::SkRule).not_null().string())
.col(ColumnDef::new(Column::Ext).text())
.col(ColumnDef::new(Column::SkNeed).not_null().boolean())
.col(ColumnDef::new(Column::SkDynamic).not_null().boolean())
.col(ColumnDef::new(Column::SkEncrypted).not_null().boolean())
.col(ColumnDef::new(Column::Repeatable).not_null().boolean())
.col(ColumnDef::new(Column::IsBasic).not_null().boolean())
.col(ColumnDef::new(Column::RestByKinds).not_null().string())
.col(ColumnDef::new(Column::ExpireSec).not_null().big_integer())
.col(ColumnDef::new(Column::SkLockCycleSec).not_null().integer())
.col(ColumnDef::new(Column::SkLockErrTimes).not_null().small_integer())
.col(ColumnDef::new(Column::SkLockDurationSec).not_null().integer())
.col(ColumnDef::new(Column::CoexistNum).not_null().small_integer())
.col(ColumnDef::new(Column::ConnUri).not_null().string())
.col(ColumnDef::new(Column::RelRbumDomainId).not_null().string())
.col(ColumnDef::new(Column::RelRbumItemId).not_null().string())
.col(ColumnDef::new(Column::Status).not_null().small_integer())
// Basic
.col(ColumnDef::new(Column::OwnPaths).not_null().string())
.col(ColumnDef::new(Column::Owner).not_null().string())
.col(ColumnDef::new(Column::CreateBy).not_null().string())
.col(ColumnDef::new(Column::UpdateBy).not_null().string());
if db == DatabaseBackend::Postgres {
builder
.col(ColumnDef::new(Column::CreateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone())
.col(ColumnDef::new(Column::UpdateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone());
} else {
builder
.engine("InnoDB")
.character_set("utf8mb4")
.collate("utf8mb4_0900_as_cs")
.col(ColumnDef::new(Column::CreateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp())
.col(ColumnDef::new(Column::UpdateTime).extra("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP".to_string()).timestamp());
}
builder.to_owned()
}

fn create_index_statement() -> Vec<IndexCreateStatement> {
tardis_create_index_statement()
}
}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
67 changes: 8 additions & 59 deletions backend/basic/src/rbum/domain/rbum_domain.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use tardis::basic::dto::TardisContext;
use tardis::chrono::{self, Utc};
use tardis::db::reldb_client::TardisActiveModel;
use tardis::db::sea_orm;
use tardis::db::sea_orm::prelude::*;
use tardis::db::sea_orm::sea_query::{ColumnDef, IndexCreateStatement, Table, TableCreateStatement};
use tardis::db::sea_orm::*;
use tardis::TardisCreateIndex;
use tardis::{TardisCreateEntity, TardisEmptyBehavior, TardisEmptyRelation};

/// Resource domain model
///
Expand All @@ -20,7 +17,7 @@ use tardis::TardisCreateIndex;
/// IAM components and CMDB components are resource domains.
///
/// 例如:所有菜单资源由IAM组件提供,所有IaaS资源由CMDB组件提供。IAM组件和CMDB组件是资源域。
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateIndex)]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, TardisCreateEntity, TardisEmptyBehavior, TardisEmptyRelation)]
#[sea_orm(table_name = "rbum_domain")]
pub struct Model {
/// Resource domain id
Expand Down Expand Up @@ -61,64 +58,16 @@ pub struct Model {
pub scope_level: i16,

#[index]
#[fill_ctx(fill = "own_paths")]
pub own_paths: String,
#[fill_ctx]
pub owner: String,
#[sea_orm(extra = "DEFAULT CURRENT_TIMESTAMP")]
pub create_time: chrono::DateTime<Utc>,
#[sea_orm(extra = "DEFAULT CURRENT_TIMESTAMP")]
pub update_time: chrono::DateTime<Utc>,
#[fill_ctx]
pub create_by: String,
#[fill_ctx(insert_only = false)]
pub update_by: String,
}

impl TardisActiveModel for ActiveModel {
fn fill_ctx(&mut self, ctx: &TardisContext, is_insert: bool) {
if is_insert {
self.own_paths = Set(ctx.own_paths.to_string());
self.owner = Set(ctx.owner.to_string());
self.create_by = Set(ctx.owner.to_string());
}
self.update_by = Set(ctx.owner.to_string());
}

fn create_table_statement(db: DbBackend) -> TableCreateStatement {
let mut builder = Table::create();
builder
.table(Entity.table_ref())
.if_not_exists()
.col(ColumnDef::new(Column::Id).not_null().string().primary_key())
// Specific
.col(ColumnDef::new(Column::Code).not_null().string())
.col(ColumnDef::new(Column::Name).not_null().string())
.col(ColumnDef::new(Column::Note).not_null().string())
.col(ColumnDef::new(Column::Icon).not_null().string())
.col(ColumnDef::new(Column::Sort).not_null().big_integer())
// Basic
.col(ColumnDef::new(Column::OwnPaths).not_null().string())
.col(ColumnDef::new(Column::Owner).not_null().string())
// With Scope
.col(ColumnDef::new(Column::ScopeLevel).not_null().small_integer())
.col(ColumnDef::new(Column::CreateBy).not_null().string())
.col(ColumnDef::new(Column::UpdateBy).not_null().string());
if db == DatabaseBackend::Postgres {
builder
.col(ColumnDef::new(Column::CreateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone())
.col(ColumnDef::new(Column::UpdateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp_with_time_zone());
} else {
builder
.engine("InnoDB")
.character_set("utf8mb4")
.collate("utf8mb4_0900_as_cs")
.col(ColumnDef::new(Column::CreateTime).extra("DEFAULT CURRENT_TIMESTAMP".to_string()).timestamp())
.col(ColumnDef::new(Column::UpdateTime).extra("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP".to_string()).timestamp());
}
builder.to_owned()
}

fn create_index_statement() -> Vec<IndexCreateStatement> {
tardis_create_index_statement()
}
}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
Loading

0 comments on commit 5d9724e

Please sign in to comment.