bios_iam/console_interface/api/
iam_ci_tenant_api.rs

1use bios_basic::helper::request_helper::try_set_real_ip_from_req_to_ctx;
2use bios_basic::rbum::dto::rbum_filer_dto::{RbumBasicFilterReq, RbumSetTreeFilterReq};
3use bios_basic::rbum::dto::rbum_set_dto::RbumSetTreeNodeResp;
4use bios_basic::rbum::helper::rbum_scope_helper::check_without_owner_and_unsafe_fill_ctx;
5use bios_basic::rbum::rbum_enumeration::RbumSetCateLevelQueryKind;
6
7use bios_basic::rbum::serv::rbum_item_serv::RbumItemCrudOperation;
8use tardis::web::poem::web::Query;
9use tardis::web::poem_openapi;
10
11use tardis::web::{
12    context_extractor::TardisContextExtractor,
13    poem::Request,
14    web_resp::{TardisApiResult, TardisResp},
15};
16
17use crate::basic::dto::iam_tenant_dto::IamTenantBoneResp;
18use crate::basic::serv::iam_cert_serv::IamCertServ;
19use crate::basic::serv::iam_set_serv::IamSetServ;
20use crate::iam_enumeration::IamSetKind;
21use crate::{
22    basic::{
23        dto::{iam_filer_dto::IamTenantFilterReq, iam_tenant_dto::IamTenantAggDetailResp},
24        serv::iam_tenant_serv::IamTenantServ,
25    },
26    iam_constants,
27};
28
29#[derive(Clone, Default)]
30pub struct IamCiTenantApi;
31
32/// # Interface Console Tenant API
33/// 接口控制台租户API
34#[poem_openapi::OpenApi(prefix_path = "/ci/tenant", tag = "bios_basic::ApiTag::Tenant")]
35impl IamCiTenantApi {
36    /// Find Tenants
37    /// 查找租户
38    #[oai(path = "/all", method = "get")]
39    async fn find(&self) -> TardisApiResult<Vec<IamTenantBoneResp>> {
40        let funs = iam_constants::get_tardis_inst();
41        let result = IamTenantServ::find_items(
42            &IamTenantFilterReq {
43                basic: RbumBasicFilterReq {
44                    ignore_scope: true,
45                    own_paths: Some("".to_string()),
46                    with_sub_own_paths: true,
47                    enabled: Some(true),
48                    ..Default::default()
49                },
50                ..Default::default()
51            },
52            None,
53            None,
54            &funs,
55            &IamCertServ::get_anonymous_context(),
56        )
57        .await?;
58        let result = result
59            .into_iter()
60            .map(|i| IamTenantBoneResp {
61                id: i.id,
62                name: i.name,
63                icon: i.icon,
64            })
65            .collect();
66        TardisResp::ok(result)
67    }
68
69    /// Get Current Tenant
70    /// 获取当前租户
71    #[oai(path = "/", method = "get")]
72    async fn get(&self, mut ctx: TardisContextExtractor, request: &Request) -> TardisApiResult<IamTenantAggDetailResp> {
73        let funs = iam_constants::get_tardis_inst();
74        check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
75        try_set_real_ip_from_req_to_ctx(request, &ctx.0).await?;
76        let result = IamTenantServ::get_tenant_agg(&IamTenantServ::get_id_by_ctx(&ctx.0, &funs)?, &IamTenantFilterReq::default(), &funs, &ctx.0).await?;
77        ctx.0.execute_task().await?;
78        TardisResp::ok(result)
79    }
80
81    /// Find Org Tree By Current Tenant
82    ///
83    /// * Without parameters: Query the whole tree
84    /// * ``parent_sys_code=true`` : query only the next level. This can be used to query level by level when the tree is too large
85    ///
86    /// 通过当前租户查找组织树
87    ///
88    /// * 无参数:查询整个树
89    /// * ``parent_sys_code=true``:仅查询下一级。当树太大时,可以逐级查询
90    #[oai(path = "/orgs", method = "get")]
91    async fn get_orgs(
92        &self,
93        parent_sys_code: Query<Option<String>>,
94        set_id: Query<Option<String>>,
95        mut ctx: TardisContextExtractor,
96        request: &Request,
97    ) -> TardisApiResult<Vec<RbumSetTreeNodeResp>> {
98        let funs = iam_constants::get_tardis_inst();
99        check_without_owner_and_unsafe_fill_ctx(request, &funs, &mut ctx.0)?;
100        let ctx = IamSetServ::try_get_rel_ctx_by_set_id(set_id.0, &funs, ctx.0).await?;
101        try_set_real_ip_from_req_to_ctx(request, &ctx).await?;
102        let set_id = IamSetServ::get_default_set_id_by_ctx(&IamSetKind::Org, &funs, &ctx).await?;
103        let result = IamSetServ::get_tree(
104            &set_id,
105            &mut RbumSetTreeFilterReq {
106                fetch_cate_item: true,
107                sys_codes: parent_sys_code.0.map(|parent_sys_code| vec![parent_sys_code]),
108                sys_code_query_kind: Some(RbumSetCateLevelQueryKind::Sub),
109                sys_code_query_depth: Some(1),
110                ..Default::default()
111            },
112            &funs,
113            &ctx,
114        )
115        .await?;
116        ctx.execute_task().await?;
117        TardisResp::ok(result.main)
118    }
119}