Skip to content
Clone
main
67 lines 2.4 KB Rust
use sea_orm::entity::Set;
use sea_orm::entity::prelude::*;
use uuid::Uuid;
/// 组织内角色。`switch` 列名避开 SQL 保留字,`key` 在组织内唯一。
/// `system = true` 的三个内置角色(owner / admin / member)不可删除、key 不可改。
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "space_roles")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(unique_key = "org_key")]
pub organization_id: Uuid,
#[sea_orm(unique_key = "org_key", column_type = "String(StringLen::N(64))")]
pub key: String,
#[sea_orm(column_type = "String(StringLen::N(64))")]
pub name: String,
#[sea_orm(column_type = "String(StringLen::N(512))")]
pub description: Option<String>,
#[sea_orm(default_value = 0)]
pub rank: i32,
#[sea_orm(default_value = false)]
pub system: bool,
#[sea_orm(default_value = false)]
pub default_role: bool,
/// 允许成员自助加入/退出该角色(旧 `space_groups` 的自助加入语义)。
#[sea_orm(default_value = false)]
pub self_assignable: bool,
#[sea_orm(column_type = "String(StringLen::N(32))")]
pub color: Option<String>,
#[sea_orm(column_type = "String(StringLen::N(32))")]
pub emoji: Option<String>,
#[sea_orm(column_type = "String(StringLen::N(128))")]
pub icon_hash: Option<String>,
#[sea_orm(default_value = 0)]
pub position: i64,
#[sea_orm(default_expr = "Expr::current_timestamp()")]
pub created_at: DateTimeUtc,
#[sea_orm(default_expr = "Expr::current_timestamp()")]
pub updated_at: DateTimeUtc,
#[sea_orm(default_value = 1)]
pub version: i32,
#[sea_orm(belongs_to, from = "organization_id", to = "id", on_delete = "Cascade")]
pub organization: BelongsTo<super::spaces::Entity>,
#[sea_orm(has_many)]
pub permissions: HasMany<super::space_role_permissions::Entity>,
#[sea_orm(has_many)]
pub members: HasMany<super::space_member_roles::Entity>,
}
#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
where
C: ConnectionTrait,
{
if insert && self.id.is_not_set() {
self.id = Set(Uuid::now_v7());
}
self.updated_at = Set(chrono::Utc::now());
Ok(self)
}
}