Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
owenlxu committed May 30, 2024
2 parents bd0616f + 8e56ff8 commit 3671dc4
Show file tree
Hide file tree
Showing 34 changed files with 827 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import com.tencent.bkrepo.auth.pojo.permission.UpdatePermissionUserRequest
import com.tencent.bkrepo.auth.controller.OpenResource
import com.tencent.bkrepo.auth.pojo.enums.AuthPermissionType
import com.tencent.bkrepo.auth.pojo.permission.UpdatePermissionDeployInRepoRequest
import com.tencent.bkrepo.auth.pojo.role.ExternalRoleResult
import com.tencent.bkrepo.auth.pojo.role.RoleSource
import com.tencent.bkrepo.auth.service.PermissionService
import com.tencent.bkrepo.common.api.pojo.Response
import com.tencent.bkrepo.common.service.util.ResponseBuilder
Expand Down Expand Up @@ -182,4 +184,14 @@ class PermissionController @Autowired constructor(
preCheckUserInProject(AuthPermissionType.REPO, projectId, repoName)
return ResponseBuilder.success(permissionService.getOrCreatePersonalPath(projectId, repoName))
}

@ApiOperation("查询外部用户组")
@GetMapping("/external/group/{projectId}/{source}")
fun getExternalRole(
@PathVariable projectId: String,
@PathVariable source: RoleSource
): Response<List<ExternalRoleResult>> {
preCheckProjectAdmin(projectId)
return ResponseBuilder.success(permissionService.listExternalRoleByProject(projectId, source))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ package com.tencent.bkrepo.auth.dao.repository

import com.tencent.bkrepo.auth.model.TRole
import com.tencent.bkrepo.auth.pojo.enums.RoleType
import com.tencent.bkrepo.auth.pojo.role.RoleSource
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository

Expand All @@ -49,23 +50,38 @@ interface RoleRepository : MongoRepository<TRole, String> {
): List<TRole>

fun findByTypeAndProjectIdAndAdmin(type: RoleType, projectId: String, admin: Boolean): List<TRole>
fun findByProjectIdAndRepoNameAndType(projectId: String, repoName: String, type: RoleType): List<TRole>
fun findByTypeAndProjectIdAndRepoName(type: RoleType, projectId: String, repoName: String): List<TRole>
fun findFirstByRoleIdAndProjectId(roleId: String, projectId: String): TRole?
fun findFirstByProjectIdAndTypeAndName(projectId: String, type: RoleType, name: String): TRole?
fun findFirstByRoleIdAndProjectIdAndRepoName(roleId: String, projectId: String, repoName: String): TRole?
fun findFirstByTypeAndProjectIdAndName(type: RoleType, projectId: String, name: String): TRole?
fun findFirstByTypeAndRoleIdAndProjectIdAndRepoName(
type: RoleType,
roleId: String,
projectId: String,
repoName: String
): TRole?

fun findFirstByTypeAndRoleIdAndProjectIdAndSource(
type: RoleType,
roleId: String,
projectId: String,
source: RoleSource
): TRole?

fun findByProjectIdAndTypeAndAdminAndIdIn(
projectId: String,
type: RoleType,
admin: Boolean,
ids: List<String>
): List<TRole>

fun findByProjectIdAndTypeAndAdminAndRepoNameAndIdIn(
projectId: String,
fun findByTypeAndProjectIdAndAdminAndRepoNameAndIdIn(
type: RoleType,
projectId: String,
admin: Boolean,
repoName: String,
ids: List<String>
): List<TRole>

fun findBySource(source: RoleSource): List<TRole>

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class PermissionHelper constructor(
}
if (queryRoles.isEmpty()) return false

val result = roleRepository.findByProjectIdAndTypeAndAdminAndRepoNameAndIdIn(
val result = roleRepository.findByTypeAndProjectIdAndAdminAndRepoNameAndIdIn(
projectId = request.projectId!!,
type = RoleType.REPO,
repoName = request.repoName!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ package com.tencent.bkrepo.auth.helper
import com.tencent.bkrepo.auth.dao.UserDao
import com.tencent.bkrepo.auth.dao.repository.RoleRepository
import com.tencent.bkrepo.auth.message.AuthMessageCode
import com.tencent.bkrepo.auth.model.TRole
import com.tencent.bkrepo.auth.model.TUser
import com.tencent.bkrepo.auth.pojo.enums.RoleType
import com.tencent.bkrepo.auth.pojo.role.CreateRoleRequest
Expand Down Expand Up @@ -106,18 +105,33 @@ class UserHelper constructor(

fun createRoleCommon(request: CreateRoleRequest): String? {
logger.info("create role request:[$request] ")
val role: TRole? = if (request.type == RoleType.REPO) {
roleRepository.findFirstByRoleIdAndProjectIdAndRepoName(
request.roleId!!,
request.projectId,
request.repoName!!
)
} else {
roleRepository.findFirstByProjectIdAndTypeAndName(
projectId = request.projectId,
type = RoleType.PROJECT,
name = request.name
)
val role = when (request.type) {
RoleType.REPO -> {
require(request.roleId != null)
roleRepository.findFirstByTypeAndRoleIdAndProjectIdAndRepoName(
type = RoleType.REPO,
roleId = request.roleId,
projectId = request.projectId,
repoName = request.repoName!!
)
}
RoleType.PROJECT -> {
if (request.source == null) {
roleRepository.findFirstByTypeAndProjectIdAndName(
type = RoleType.PROJECT,
projectId = request.projectId,
name = request.name
)
} else {
require(request.roleId != null)
roleRepository.findFirstByTypeAndRoleIdAndProjectIdAndSource(
type = RoleType.PROJECT,
roleId = request.roleId,
projectId = request.projectId,
source = request.source
)
}
}
}

role?.let {
Expand Down Expand Up @@ -151,13 +165,17 @@ class UserHelper constructor(
}

private fun findUsableProjectTypeRoleId(roleId: String?, projectId: String): String {
var tempRoleId = roleId ?: "${projectId}_role_${IDUtil.shortUUID()}"
var tempRoleId = roleId ?: buildProjectRoleId(projectId)
while (true) {
val role = roleRepository.findFirstByRoleIdAndProjectId(tempRoleId, projectId)
if (role == null) return tempRoleId else tempRoleId = "${projectId}_role_${IDUtil.shortUUID()}"
if (role == null) return tempRoleId else tempRoleId = buildProjectRoleId(projectId)
}
}

private fun buildProjectRoleId(projectId: String): String {
return "${projectId}_role_${IDUtil.shortUUID()}"
}

companion object {
private val logger = LoggerFactory.getLogger(UserHelper::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.auth.job

import com.tencent.bkrepo.auth.pojo.role.RoleSource
import com.tencent.bkrepo.auth.pojo.role.UpdateRoleRequest
import com.tencent.bkrepo.auth.service.PermissionService
import com.tencent.bkrepo.auth.service.RoleService
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class ExternalGroupSyncJob(
private val roleService: RoleService,
private val permissionService: PermissionService
) {

@Scheduled(cron = "0 */10 * * * ? ")
@SchedulerLock(name = "DevopsUserGroupSync", lockAtMostFor = "PT10M")
fun runDevopsUserGroupSync() {
logger.info("start to update external role")
val roleList = roleService.listRoleBySource(RoleSource.DEVOPS)
val projectIdSet = mutableSetOf<String>()
val indexIdMap = mutableMapOf<String, String>()
val projectIdMap = mutableMapOf<String, String>()
roleList.forEach {
projectIdSet.add(it.projectId)
indexIdMap[it.roleId] = it.id!!
projectIdMap[it.roleId] = it.projectId
}

projectIdSet.forEach { project ->
val roleUserMap = mutableMapOf<String, List<String>>()
permissionService.listExternalRoleByProject(project, RoleSource.DEVOPS).forEach { externalRole ->
roleUserMap[externalRole.roleId] = externalRole.userList
}
roleList.forEach { role ->
if (projectIdMap[role.roleId] == project) {
val updateRequest = UpdateRoleRequest(
userIds = roleUserMap[role.roleId]!!.toSet(),
description = null,
name = null
)
logger.info("to update external role [${role.roleId}] ")
roleService.updateRoleInfo(indexIdMap[role.roleId]!!, updateRequest)
}
}
}
}

companion object {
private val logger = LoggerFactory.getLogger(ExternalGroupSyncJob::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package com.tencent.bkrepo.auth.model

import com.tencent.bkrepo.auth.pojo.enums.RoleType
import com.tencent.bkrepo.auth.pojo.role.RoleSource
import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.index.CompoundIndexes
import org.springframework.data.mongodb.core.mapping.Document
Expand All @@ -44,7 +45,8 @@ import org.springframework.data.mongodb.core.mapping.Document
CompoundIndex(name = "roleId_idx", def = "{'roleId': 1}", background = true),
CompoundIndex(name = "type_idx", def = "{'type': 1}", background = true),
CompoundIndex(name = "projectId_idx", def = "{'projectId': 1}", background = true),
CompoundIndex(name = "repoName_idx", def = "{'repoName': 1}", background = true)
CompoundIndex(name = "repoName_idx", def = "{'repoName': 1}", background = true),
CompoundIndex(name = "source_idx", def = "{'source': 1}", background = true)
)
data class TRole(
val id: String? = null,
Expand All @@ -54,5 +56,6 @@ data class TRole(
val projectId: String,
val repoName: String? = null,
val admin: Boolean = false,
var description: String? = null
var description: String? = null,
var source: RoleSource? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.tencent.bkrepo.auth.pojo

data class BkciRoleListResponse(
val status: Int,
val data: List<BkciRoleResult>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.tencent.bkrepo.auth.pojo

import com.fasterxml.jackson.annotation.JsonProperty

data class BkciRoleResult(
@JsonProperty("display_name")
val displayName: String,
@JsonProperty("role_id")
val roleId: Long,
@JsonProperty("role_name")
val roleName: String,
@JsonProperty("user_id_list")
val userIdList: List<String>,
@JsonProperty("type")
val type: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ data class CreateRoleRequest(
@ApiModelProperty("管理员")
val admin: Boolean = false,
@ApiModelProperty("描述信息")
val description: String? = null
val description: String? = null,
@ApiModelProperty("角色来源")
val source: RoleSource? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tencent.bkrepo.auth.pojo.role

data class ExternalRoleResult(
val name: String,
val roleId: String,
val userList: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ data class Role(
@ApiModelProperty("绑定的用户")
val users: List<String> = listOf(),
@ApiModelProperty("描述信息")
val description: String? = null
val description: String? = null,
@ApiModelProperty("角色来源")
val source: RoleSource? = null
)
Loading

0 comments on commit 3671dc4

Please sign in to comment.