Skip to content

Commit

Permalink
feat: 创建项目时,增加是否创建本地权限参数 TencentBlueKing#879 (TencentBlueKing#882)
Browse files Browse the repository at this point in the history
* feat: 创建项目时,增加是否创建本地权限参数 TencentBlueKing#879

* feat: 创建项目时,增加是否创建本地权限参数 TencentBlueKing#879

* feat: 创建项目时,增加是否创建本地权限参数 TencentBlueKing#879

* feat: 创建项目时,增加是否创建本地权限参数 TencentBlueKing#879
  • Loading branch information
yaoxuwan authored Aug 7, 2023
1 parent 1f7e652 commit de48968
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import com.tencent.bkrepo.common.artifact.event.base.EventType
*/
data class ProjectCreatedEvent(
override val projectId: String,
override val userId: String
override val userId: String,
val createPermission: Boolean
) : ArtifactEvent(
type = EventType.PROJECT_CREATED,
projectId = projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,13 @@ class EdgePullReplicaExecutor(
localProjectId: String,
localRepoName: String,
): RepositoryDetail {
val projectCreateRequest = ProjectCreateRequest(localProjectId, localProjectId, null, centerRepo.createdBy)
val projectCreateRequest = ProjectCreateRequest(
name = localProjectId,
displayName = localProjectId,
description = null,
createPermission = true,
operator = centerRepo.createdBy
)
try {
projectClient.createProject(projectCreateRequest)
} catch (e: RemoteErrorCodeException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ data class ProjectCreateRequest(
val displayName: String,
@ApiModelProperty("描述", required = true)
val description: String? = null,
@ApiModelProperty("创建权限", required = false)
val createPermission: Boolean = true,
@ApiModelProperty("操作用户", required = false)
val operator: String = SYSTEM_USER
) : ProjectRequest
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ data class UserProjectCreateRequest(
@ApiModelProperty("显示名")
val displayName: String,
@ApiModelProperty("描述")
val description: String
val description: String,
@ApiModelProperty("创建本地权限")
val createPermission: Boolean = true
) : ProjectRequest
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class UserProjectController(
name = name,
displayName = displayName,
description = description,
operator = userId
operator = userId,
createPermission = createPermission
)
}
projectService.createProject(createRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class ResourcePermissionListener(
*/
fun handle(event: ProjectCreatedEvent) {
with(event) {
if (!createPermission) {
return
}
if (isAuthedNormalUser(userId) && isNeedLocalPermission(projectId)) {
val projectManagerRoleId = roleResource.createProjectManage(projectId).data!!
userResource.addUserRole(userId, projectManagerRoleId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ object ProjectEventFactory {
with(request) {
return ProjectCreatedEvent(
projectId = name,
userId = operator
userId = operator,
createPermission = createPermission
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,63 +70,63 @@ class ProjectServiceTest @Autowired constructor(
@Test
@DisplayName("测试创建项目")
fun `test create project`() {
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
}

@Test
@DisplayName("测试创建同名项目")
fun `should throw exception when project exists`() {
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
assertThrows<ErrorCodeException> { projectService.createProject(request) }
}

@Test
@DisplayName("测试非法项目名称")
fun `should throw exception with illegal name`() {
var request = ProjectCreateRequest("1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
var request = ProjectCreateRequest("1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest("11", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("11", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest("a".repeat(33), UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("a".repeat(33), UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest("test_1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("test_1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("test-1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("test-1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("_prebuild", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("_prebuild", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("CODECC_a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("CODECC_a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
}

@Test
@DisplayName("测试非法项目显示名")
fun `should throw exception with illegal display name`() {
var request = ProjectCreateRequest(UT_PROJECT_ID, "", UT_PROJECT_DESC, UT_USER)
var request = ProjectCreateRequest(UT_PROJECT_ID, "", UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(33), UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(33), UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(1), UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(1), UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(32), UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(32), UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

removeAllProject()
request = ProjectCreateRequest(UT_PROJECT_ID, "123-abc", UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "123-abc", UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ class RepositoryServiceTest @Autowired constructor(
fun beforeAll() {
initMock()
if (!projectService.checkExist(UT_PROJECT_ID)) {
val projectCreateRequest = ProjectCreateRequest(UT_PROJECT_ID, UT_REPO_NAME, UT_REPO_DISPLAY, UT_USER)
val projectCreateRequest = ProjectCreateRequest(
name = UT_PROJECT_ID,
displayName = UT_REPO_NAME,
description = UT_REPO_DISPLAY,
createPermission = true,
operator = UT_USER
)
projectService.createProject(projectCreateRequest)
}
val request = StorageCredentialsCreateRequest(UT_STORAGE_CREDENTIALS_KEY, storageCredentials, UT_REGION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ open class ServiceBaseTest {
credentialsKey: String? = null
) {
if (!projectService.checkExist(UT_PROJECT_ID)) {
val projectCreateRequest = ProjectCreateRequest(UT_PROJECT_ID, UT_REPO_NAME, UT_REPO_DISPLAY, UT_USER)
val projectCreateRequest = ProjectCreateRequest(
name = UT_PROJECT_ID,
displayName = UT_REPO_NAME,
description = UT_REPO_DISPLAY,
createPermission = true,
operator = UT_USER
)
projectService.createProject(projectCreateRequest)
}
if (!repositoryService.checkExist(UT_PROJECT_ID, UT_REPO_NAME)) {
Expand All @@ -180,7 +186,7 @@ open class ServiceBaseTest {
projectService: ProjectService,
projectId: String = UT_PROJECT_ID
): ProjectInfo {
val projectCreateRequest = ProjectCreateRequest(projectId, UT_REPO_NAME, UT_REPO_DISPLAY, UT_USER)
val projectCreateRequest = ProjectCreateRequest(projectId, UT_REPO_NAME, UT_REPO_DISPLAY, true, UT_USER)
return projectService.createProject(projectCreateRequest)
}

Expand Down

0 comments on commit de48968

Please sign in to comment.