Skip to content

Commit

Permalink
iam: add kv client (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzIsGod1019 authored Jun 25, 2024
1 parent 8e16eb8 commit f260c85
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions backend/supports/iam/src/basic/serv/clients.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod iam_kv_client;
pub mod iam_log_client;
pub mod iam_search_client;
pub mod mail_client;
Expand Down
66 changes: 66 additions & 0 deletions backend/supports/iam/src/basic/serv/clients/iam_kv_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use bios_basic::rbum::rbum_enumeration::RbumScopeLevelKind;
use bios_sdk_invoke::clients::spi_kv_client::SpiKvClient;
use tardis::tokio;
use tardis::{
basic::{dto::TardisContext, result::TardisResult},
serde_json::Value,
TardisFunsInst,
};

use crate::iam_constants;

pub struct IamKvClient;

impl IamKvClient {
pub async fn async_add_or_modify_item(
key: String,
value: Value,
info: Option<String>,
scope_level: Option<RbumScopeLevelKind>,
_funs: &TardisFunsInst,
ctx: &TardisContext,
) -> TardisResult<()> {
let ctx_clone = ctx.clone();
ctx.add_async_task(Box::new(|| {
Box::pin(async move {
let task_handle = tokio::spawn(async move {
let funs = iam_constants::get_tardis_inst();
let _ = Self::add_or_modify_item(&key, &value, info, scope_level, &funs, &ctx_clone).await;
});
task_handle.await.unwrap();
Ok(())
})
}))
.await
}

pub async fn async_delete_item(key: String, _funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
let ctx_clone = ctx.clone();
ctx.add_async_task(Box::new(|| {
Box::pin(async move {
let task_handle = tokio::spawn(async move {
let funs = iam_constants::get_tardis_inst();
let _ = Self::delete_item(&key, &funs, &ctx_clone).await;
});
task_handle.await.unwrap();
Ok(())
})
}))
.await
}

pub async fn add_or_modify_item(
key: &str,
value: &Value,
info: Option<String>,
scope_level: Option<RbumScopeLevelKind>,
funs: &TardisFunsInst,
ctx: &TardisContext,
) -> TardisResult<()> {
SpiKvClient::add_or_modify_item(key, value, info, scope_level.map(|kind| kind.to_int()), funs, ctx).await
}

pub async fn delete_item(key: &str, funs: &TardisFunsInst, ctx: &TardisContext) -> TardisResult<()> {
SpiKvClient::delete_item(key, funs, ctx).await
}
}
7 changes: 6 additions & 1 deletion backend/supports/iam/src/basic/serv/iam_role_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tardis::db::sea_orm::prelude::Expr;
use tardis::db::sea_orm::sea_query::SelectStatement;
use tardis::db::sea_orm::*;
use tardis::log::info;
use tardis::serde_json::json;
use tardis::web::web_resp::TardisPage;
use tardis::{tokio, TardisFuns, TardisFunsInst};

Expand All @@ -35,6 +36,7 @@ use crate::iam_constants::{RBUM_SCOPE_LEVEL_APP, RBUM_SCOPE_LEVEL_TENANT};
use crate::iam_enumeration::{IamRelKind, IamRoleKind};
use crate::iam_initializer::{default_iam_send_avatar, ws_iam_send_client};

use super::clients::iam_kv_client::IamKvClient;
use super::clients::iam_log_client::{IamLogClient, LogParamTag};
use super::clients::iam_search_client::IamSearchClient;
use super::iam_cert_serv::IamCertServ;
Expand Down Expand Up @@ -116,6 +118,7 @@ impl RbumItemCrudOperation<iam_role::ActiveModel, IamRoleAddReq, IamRoleModifyRe
ctx,
)
.await;
IamKvClient::async_add_or_modify_item(id.to_string(), json!(role.name.clone()), None, Some(role.scope_level.clone()), funs, ctx).await?;

Ok(())
}
Expand Down Expand Up @@ -206,7 +209,7 @@ impl RbumItemCrudOperation<iam_role::ActiveModel, IamRoleAddReq, IamRoleModifyRe
let mut op_describe = String::new();
let mut op_kind = String::new();
if modify_req.name.is_some() {
if Self::is_custom_role(role.kind, role.scope_level) {
if Self::is_custom_role(role.kind, role.scope_level.clone()) {
op_describe = format!("编辑自定义角色名称为{}", modify_req.name.as_ref().unwrap_or(&TrimString::from("")));
op_kind = "ModifyCustomizeRoleName".to_string();
} else {
Expand All @@ -218,6 +221,7 @@ impl RbumItemCrudOperation<iam_role::ActiveModel, IamRoleAddReq, IamRoleModifyRe
if !op_describe.is_empty() {
let _ = IamLogClient::add_ctx_task(LogParamTag::IamRole, Some(id.to_string()), op_describe, Some(op_kind), ctx).await;
}
IamKvClient::async_add_or_modify_item(id.to_string(), json!(role.name.clone()), None, Some(role.scope_level.clone()), funs, ctx).await?;

Ok(())
}
Expand Down Expand Up @@ -322,6 +326,7 @@ impl RbumItemCrudOperation<iam_role::ActiveModel, IamRoleAddReq, IamRoleModifyRe
ctx,
)
.await;
IamKvClient::async_delete_item(id.to_string(), funs, ctx).await?;

Ok(())
}
Expand Down

0 comments on commit f260c85

Please sign in to comment.