Skip to content

Commit

Permalink
feat:同步并分表存储资源组权限数据 TencentBlueKing#10964
Browse files Browse the repository at this point in the history
  • Loading branch information
fcfang123 committed Sep 24, 2024
1 parent f48d00c commit ffaceff
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ import com.tencent.devops.auth.provider.rbac.service.RbacPermissionApplyService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionAuthMonitorSpaceService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionAuthorizationScopesService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionExtService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionFacadeServiceImpl
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionItsmCallbackService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionProjectService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceCallbackService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceGroupFacadeServiceImpl
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceGroupPermissionService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceGroupService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceGroupSyncService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceMemberFacadeServiceImpl
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceMemberService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceService
import com.tencent.devops.auth.provider.rbac.service.RbacPermissionResourceValidateService
Expand All @@ -91,6 +92,7 @@ import com.tencent.devops.auth.service.PermissionAuthorizationService
import com.tencent.devops.auth.service.ResourceService
import com.tencent.devops.auth.service.SuperManagerService
import com.tencent.devops.auth.service.iam.MigrateCreatorFixService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupFacadeService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupPermissionService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupSyncService
Expand Down Expand Up @@ -191,7 +193,7 @@ class RbacAuthConfiguration {
)

@Bean
fun permissionFacadeService(
fun permissionResourceGroupFacadeService(
permissionResourceGroupService: PermissionResourceGroupService,
groupPermissionService: PermissionResourceGroupPermissionService,
permissionResourceMemberService: PermissionResourceMemberService,
Expand All @@ -201,7 +203,7 @@ class RbacAuthConfiguration {
deptService: DeptService,
iamV2ManagerService: V2ManagerService,
rbacCacheService: RbacCacheService
) = RbacPermissionFacadeServiceImpl(
) = RbacPermissionResourceGroupFacadeServiceImpl(
permissionResourceGroupService = permissionResourceGroupService,
groupPermissionService = groupPermissionService,
permissionResourceMemberService = permissionResourceMemberService,
Expand Down Expand Up @@ -252,11 +254,25 @@ class RbacAuthConfiguration {
authResourceGroupMemberDao = authResourceGroupMemberDao,
dslContext = dslContext,
deptService = deptService,
rbacCacheService = rbacCacheService,
permissionAuthorizationService = permissionAuthorizationService,
syncIamGroupMemberService = syncIamGroupMemberService
)

@Bean
fun permissionResourceMemberFacadeService(
permissionResourceGroupFacadeService: PermissionResourceGroupFacadeService,
permissionResourceMemberService: PermissionResourceMemberService,
authResourceGroupDao: AuthResourceGroupDao,
authResourceGroupMemberDao: AuthResourceGroupMemberDao,
dslContext: DSLContext
) = RbacPermissionResourceMemberFacadeServiceImpl(
permissionResourceGroupFacadeService = permissionResourceGroupFacadeService,
permissionResourceMemberService = permissionResourceMemberService,
authResourceGroupDao = authResourceGroupDao,
authResourceGroupMemberDao = authResourceGroupMemberDao,
dslContext = dslContext
)

@Bean
@Primary
fun rbacPermissionExtService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.tencent.devops.auth.pojo.enum.RemoveMemberButtonControl
import com.tencent.devops.auth.pojo.vo.GroupDetailsInfoVo
import com.tencent.devops.auth.pojo.vo.MemberGroupCountWithPermissionsVo
import com.tencent.devops.auth.service.DeptService
import com.tencent.devops.auth.service.iam.PermissionFacadeService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupFacadeService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupPermissionService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupService
import com.tencent.devops.auth.service.iam.PermissionResourceMemberService
Expand All @@ -27,7 +27,7 @@ import org.jooq.DSLContext
import org.slf4j.LoggerFactory
import java.util.concurrent.TimeUnit

class RbacPermissionFacadeServiceImpl(
class RbacPermissionResourceGroupFacadeServiceImpl(
private val permissionResourceGroupService: PermissionResourceGroupService,
private val groupPermissionService: PermissionResourceGroupPermissionService,
private val permissionResourceMemberService: PermissionResourceMemberService,
Expand All @@ -37,7 +37,7 @@ class RbacPermissionFacadeServiceImpl(
private val deptService: DeptService,
private val iamV2ManagerService: V2ManagerService,
private val rbacCacheService: RbacCacheService
) : PermissionFacadeService {
) : PermissionResourceGroupFacadeService {
override fun getMemberGroupsDetails(
projectId: String,
memberId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.tencent.devops.auth.provider.rbac.service

import com.tencent.devops.auth.dao.AuthResourceGroupDao
import com.tencent.devops.auth.dao.AuthResourceGroupMemberDao
import com.tencent.devops.auth.pojo.ResourceMemberInfo
import com.tencent.devops.auth.pojo.dto.IamGroupIdsQueryConditionDTO
import com.tencent.devops.auth.pojo.dto.ProjectMembersQueryConditionDTO
import com.tencent.devops.auth.pojo.request.ProjectMembersQueryConditionReq
import com.tencent.devops.auth.service.iam.PermissionResourceGroupFacadeService
import com.tencent.devops.auth.service.iam.PermissionResourceMemberFacadeService
import com.tencent.devops.auth.service.iam.PermissionResourceMemberService
import com.tencent.devops.common.api.model.SQLPage
import org.jooq.DSLContext
import org.slf4j.LoggerFactory

class RbacPermissionResourceMemberFacadeServiceImpl(
private val permissionResourceGroupFacadeService: PermissionResourceGroupFacadeService,
private val permissionResourceMemberService: PermissionResourceMemberService,
private val authResourceGroupDao: AuthResourceGroupDao,
private val authResourceGroupMemberDao: AuthResourceGroupMemberDao,
private val dslContext: DSLContext
) : PermissionResourceMemberFacadeService {
override fun listProjectMembersByComplexConditions(
conditionReq: ProjectMembersQueryConditionReq
): SQLPage<ResourceMemberInfo> {
logger.info("list project members by complex conditions: $conditionReq")
// 不允许同时查询部门名称和用户名称
if (conditionReq.userName != null && conditionReq.deptName != null) {
return SQLPage(count = 0, records = emptyList())
}

// 简单查询直接返回结果
if (!conditionReq.isComplexQuery()) {
return permissionResourceMemberService.listProjectMembers(
projectCode = conditionReq.projectCode,
memberType = conditionReq.memberType,
userName = conditionReq.userName,
deptName = conditionReq.deptName,
departedFlag = conditionReq.departedFlag,
page = conditionReq.page,
pageSize = conditionReq.pageSize
)
}

// 处理复杂查询条件
val iamGroupIdsByCondition = if (conditionReq.isNeedToQueryIamGroups()) {
permissionResourceGroupFacadeService.listIamGroupIdsByConditions(
condition = IamGroupIdsQueryConditionDTO(
projectCode = conditionReq.projectCode,
groupName = conditionReq.groupName,
relatedResourceType = conditionReq.relatedResourceType,
relatedResourceCode = conditionReq.relatedResourceCode,
action = conditionReq.action
)
)
} else {
emptyList()
}.toMutableList()

// 查询不到用户组,直接返回空
if (conditionReq.isNeedToQueryIamGroups() && iamGroupIdsByCondition.isEmpty()) {
return SQLPage(0, emptyList())
}

val conditionDTO = ProjectMembersQueryConditionDTO.build(conditionReq, iamGroupIdsByCondition)

if (iamGroupIdsByCondition.isNotEmpty()) {
logger.debug("iamGroupIdsByCondition :$iamGroupIdsByCondition")
// 根据用户组Id查询出对应用户组中的人员模板成员
val iamTemplateIds = authResourceGroupMemberDao.listProjectMembersByComplexConditions(
dslContext = dslContext,
conditionDTO = ProjectMembersQueryConditionDTO(
projectCode = conditionDTO.projectCode,
queryTemplate = true,
iamGroupIds = conditionDTO.iamGroupIds
)
)
if (iamTemplateIds.isNotEmpty()) {
// 根据查询出的人员模板ID,查询出对应的组ID
val iamGroupIdsFromTemplate = authResourceGroupDao.listIamGroupIdsByConditions(
dslContext = dslContext,
projectCode = conditionDTO.projectCode,
iamTemplateIds = iamTemplateIds.map { it.id.toInt() }
)
iamGroupIdsByCondition.addAll(iamGroupIdsFromTemplate)
logger.debug("iamGroupIdsByCondition and template :$iamGroupIdsByCondition")
}
}

val records = authResourceGroupMemberDao.listProjectMembersByComplexConditions(
dslContext = dslContext,
conditionDTO = conditionDTO
)
logger.debug("listProjectMembersByComplexConditions :$records")

val count = authResourceGroupMemberDao.countProjectMembersByComplexConditions(
dslContext = dslContext,
conditionDTO = conditionDTO
)
logger.debug("listProjectMembersByComplexConditions :$count")
// 添加离职标志
return if (conditionDTO.departedFlag == false) {
SQLPage(count, records)
} else {
SQLPage(count, permissionResourceMemberService.addDepartedFlagToMembers(records))
}
}

companion object {
private val logger = LoggerFactory.getLogger(RbacPermissionResourceMemberService::class.java)

// 永久过期时间
private const val PERMANENT_EXPIRED_TIME = 4102444800000L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@ import com.tencent.devops.auth.dao.AuthResourceGroupMemberDao
import com.tencent.devops.auth.pojo.AuthResourceGroupMember
import com.tencent.devops.auth.pojo.ResourceMemberInfo
import com.tencent.devops.auth.pojo.dto.GroupMemberRenewalDTO
import com.tencent.devops.auth.pojo.dto.IamGroupIdsQueryConditionDTO
import com.tencent.devops.auth.pojo.dto.ProjectMembersQueryConditionDTO
import com.tencent.devops.auth.pojo.enum.BatchOperateType
import com.tencent.devops.auth.pojo.request.GroupMemberCommonConditionReq
import com.tencent.devops.auth.pojo.request.GroupMemberHandoverConditionReq
import com.tencent.devops.auth.pojo.request.GroupMemberRenewalConditionReq
import com.tencent.devops.auth.pojo.request.GroupMemberSingleRenewalReq
import com.tencent.devops.auth.pojo.request.ProjectMembersQueryConditionReq
import com.tencent.devops.auth.pojo.request.RemoveMemberFromProjectReq
import com.tencent.devops.auth.pojo.vo.BatchOperateGroupMemberCheckVo
import com.tencent.devops.auth.pojo.vo.ResourceMemberCountVO
import com.tencent.devops.auth.service.DeptService
import com.tencent.devops.auth.service.PermissionAuthorizationService
import com.tencent.devops.auth.service.iam.PermissionFacadeService
import com.tencent.devops.auth.service.iam.PermissionResourceGroupSyncService
import com.tencent.devops.auth.service.iam.PermissionResourceMemberService
import com.tencent.devops.common.api.exception.ErrorCodeException
Expand Down Expand Up @@ -60,8 +56,7 @@ class RbacPermissionResourceMemberService constructor(
private val dslContext: DSLContext,
private val deptService: DeptService,
private val permissionAuthorizationService: PermissionAuthorizationService,
private val syncIamGroupMemberService: PermissionResourceGroupSyncService,
private val permissionFacadeService: PermissionFacadeService
private val syncIamGroupMemberService: PermissionResourceGroupSyncService
) : PermissionResourceMemberService {
override fun getResourceGroupMembers(
projectCode: String,
Expand Down Expand Up @@ -179,7 +174,7 @@ class RbacPermissionResourceMemberService constructor(
return SQLPage(count = count, records = addDepartedFlagToMembers(records))
}

private fun addDepartedFlagToMembers(records: List<ResourceMemberInfo>): List<ResourceMemberInfo> {
override fun addDepartedFlagToMembers(records: List<ResourceMemberInfo>): List<ResourceMemberInfo> {
val userMembers = records.filter {
it.type == ManagerScopesEnum.getType(ManagerScopesEnum.USER)
}.map { it.id }
Expand All @@ -199,89 +194,6 @@ class RbacPermissionResourceMemberService constructor(
}
}

override fun listProjectMembersByComplexConditions(
conditionReq: ProjectMembersQueryConditionReq
): SQLPage<ResourceMemberInfo> {
logger.info("list project members by complex conditions: $conditionReq")
// 不允许同时查询部门名称和用户名称
if (conditionReq.userName != null && conditionReq.deptName != null) {
return SQLPage(count = 0, records = emptyList())
}

// 简单查询直接返回结果
if (!conditionReq.isComplexQuery()) {
return listProjectMembers(
projectCode = conditionReq.projectCode,
memberType = conditionReq.memberType,
userName = conditionReq.userName,
deptName = conditionReq.deptName,
departedFlag = conditionReq.departedFlag,
page = conditionReq.page,
pageSize = conditionReq.pageSize
)
}

// 处理复杂查询条件
val iamGroupIdsByCondition = if (conditionReq.isNeedToQueryIamGroups()) {
permissionFacadeService.listIamGroupIdsByConditions(
condition = IamGroupIdsQueryConditionDTO(
projectCode = conditionReq.projectCode,
groupName = conditionReq.groupName,
relatedResourceType = conditionReq.relatedResourceType,
relatedResourceCode = conditionReq.relatedResourceCode,
action = conditionReq.action
)
)
} else {
emptyList()
}.toMutableList()

// 查询不到用户组,直接返回空
if (conditionReq.isNeedToQueryIamGroups() && iamGroupIdsByCondition.isEmpty()) {
return SQLPage(0, emptyList())
}

val conditionDTO = ProjectMembersQueryConditionDTO.build(conditionReq, iamGroupIdsByCondition)

if (iamGroupIdsByCondition.isNotEmpty()) {
// 根据用户组Id查询出对应用户组中的人员模板成员
val iamTemplateIds = authResourceGroupMemberDao.listProjectMembersByComplexConditions(
dslContext = dslContext,
conditionDTO = ProjectMembersQueryConditionDTO(
projectCode = conditionDTO.projectCode,
queryTemplate = true,
iamGroupIds = conditionDTO.iamGroupIds
)
)
if (iamTemplateIds.isNotEmpty()) {
// 根据查询出的人员模板ID,查询出对应的组ID
val iamGroupIdsFromTemplate = authResourceGroupDao.listIamGroupIdsByConditions(
dslContext = dslContext,
projectCode = conditionDTO.projectCode,
iamTemplateIds = iamTemplateIds.map { it.id.toInt() }
)
iamGroupIdsByCondition.addAll(iamGroupIdsFromTemplate)
}
}

val records = authResourceGroupMemberDao.listProjectMembersByComplexConditions(
dslContext = dslContext,
conditionDTO = conditionDTO
)

val count = authResourceGroupMemberDao.countProjectMembersByComplexConditions(
dslContext = dslContext,
conditionDTO = conditionDTO
)

// 添加离职标志
return if (conditionDTO.departedFlag == false) {
SQLPage(count, records)
} else {
SQLPage(count, addDepartedFlagToMembers(records))
}
}

override fun addGroupMember(
projectCode: String,
memberId: String,
Expand Down
Loading

0 comments on commit ffaceff

Please sign in to comment.