Skip to content

Commit

Permalink
iam:fix role add async task.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed Nov 17, 2023
1 parent 981d234 commit 8918bd3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
8 changes: 6 additions & 2 deletions support/iam/src/console_app/api/iam_ca_role_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl IamCaRoleApi {

/// Delete Role By Role Id
#[oai(path = "/:id", method = "delete")]
async fn delete(&self, id: Path<String>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Void> {
async fn delete(&self, id: Path<String>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Option<String>> {
add_remote_ip(request, &ctx.0).await?;
let mut funs = iam_constants::get_tardis_inst();
funs.begin().await?;
Expand Down Expand Up @@ -155,7 +155,11 @@ impl IamCaRoleApi {
IamRoleServ::delete_item_with_all_rels(&id.0, &funs, &ctx.0).await?;
funs.commit().await?;
ctx.0.execute_task().await?;
TardisResp::ok(Void {})
if let Some(task_id) = TaskProcessor::get_task_id_with_ctx(&ctx.0).await? {
TardisResp::accepted(Some(task_id))
} else {
TardisResp::ok(None)
}
}

/// Add Role Rel Account
Expand Down
40 changes: 27 additions & 13 deletions support/iam/src/console_interface/api/iam_ci_role_api.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use tardis::tokio;
use crate::basic::serv::iam_app_serv::IamAppServ;
use crate::basic::serv::iam_cert_serv::IamCertServ;
use crate::basic::serv::iam_role_serv::IamRoleServ;
use crate::iam_constants::{self, RBUM_SCOPE_LEVEL_APP};
use bios_basic::helper::request_helper::add_remote_ip;
use tardis::web::context_extractor::TardisContextExtractor;
use tardis::web::poem::Request;
use tardis::web::poem::{Request, RequestBody};
use tardis::web::poem_openapi;
use tardis::web::poem_openapi::param::{Path, Query};
use tardis::web::web_resp::{TardisApiResult, TardisResp, Void};
use bios_basic::process::task_processor::TaskProcessor;

#[derive(Clone, Default)]
pub struct IamCiRoleApi;

Expand Down Expand Up @@ -69,23 +72,34 @@ impl IamCiRoleApi {
account_ids: Query<String>,
ctx: TardisContextExtractor,
request: &Request,
) -> TardisApiResult<Void> {
) -> TardisApiResult<Option<String>> {
add_remote_ip(request, &ctx.0).await?;
let ctx = ctx.0;
let mut funs = iam_constants::get_tardis_inst();
funs.begin().await?;
let apps_split: Vec<&str> = app_ids.0.split(',').collect::<Vec<_>>();
let account_split: Vec<&str> = account_ids.0.split(',').collect::<Vec<_>>();
for app_id in apps_split {
let mock_app_ctx = IamCertServ::try_use_app_ctx(ctx.clone(), Some(app_id.to_string()))?;
for account_id in account_split.clone() {
IamAppServ::add_rel_account(app_id, account_id, true, &funs, &mock_app_ctx).await?;
IamRoleServ::add_rel_account(&id.0, account_id, Some(RBUM_SCOPE_LEVEL_APP), &funs, &mock_app_ctx).await?;
}
}
funs.commit().await?;
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();
for app_id in apps_split {
let mock_app_ctx = IamCertServ::try_use_app_ctx(ctx_clone.clone(), Some(app_id.to_string())).unwrap_or(ctx_clone.clone());
for account_id in account_split.clone() {
let _ = IamAppServ::add_rel_account(app_id, account_id, true, &funs, &mock_app_ctx).await;
let _ = IamRoleServ::add_rel_account(&id.0, account_id, Some(RBUM_SCOPE_LEVEL_APP), &funs, &mock_app_ctx).await;
}
}
});
task_handle.await.unwrap();
Ok(())
})
})).await?;
ctx.execute_task().await?;
TardisResp::ok(Void {})
if let Some(task_id) = TaskProcessor::get_task_id_with_ctx(&ctx).await? {
TardisResp::accepted(Some(task_id))
} else {
TardisResp::ok(None)
}
}

/// Add Role Rel Account
Expand Down
8 changes: 6 additions & 2 deletions support/iam/src/console_system/api/iam_cs_role_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,19 @@ impl IamCsRoleApi {

/// Delete Role By Role Id
#[oai(path = "/:id", method = "delete")]
async fn delete(&self, id: Path<String>, tenant_id: Query<Option<String>>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Void> {
async fn delete(&self, id: Path<String>, tenant_id: Query<Option<String>>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Option<String>> {
let ctx = IamCertServ::try_use_tenant_ctx(ctx.0, tenant_id.0)?;
add_remote_ip(request, &ctx).await?;
let mut funs = iam_constants::get_tardis_inst();
funs.begin().await?;
IamRoleServ::delete_item_with_all_rels(&id.0, &funs, &ctx).await?;
funs.commit().await?;
ctx.execute_task().await?;
TardisResp::ok(Void {})
if let Some(task_id) = TaskProcessor::get_task_id_with_ctx(&ctx).await? {
TardisResp::accepted(Some(task_id))
} else {
TardisResp::ok(None)
}
}

/// Add Role Rel Account
Expand Down
8 changes: 6 additions & 2 deletions support/iam/src/console_tenant/api/iam_ct_role_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,18 @@ impl IamCtRoleApi {

/// Delete Role By Role Id
#[oai(path = "/:id", method = "delete")]
async fn delete(&self, id: Path<String>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Void> {
async fn delete(&self, id: Path<String>, ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<Option<String>> {
add_remote_ip(request, &ctx.0).await?;
let mut funs = iam_constants::get_tardis_inst();
funs.begin().await?;
IamRoleServ::delete_item_with_all_rels(&id.0, &funs, &ctx.0).await?;
funs.commit().await?;
ctx.0.execute_task().await?;
TardisResp::ok(Void {})
if let Some(task_id) = TaskProcessor::get_task_id_with_ctx(&ctx.0).await? {
TardisResp::accepted(Some(task_id))
} else {
TardisResp::ok(None)
}
}

/// Add Role Rel Account
Expand Down

0 comments on commit 8918bd3

Please sign in to comment.