fix: delete project member when no projectIds

This commit is contained in:
guoyuqi 2023-08-08 11:09:04 +08:00 committed by f2c-ci-robot[bot]
parent e3171c2b12
commit e58f383a4d
4 changed files with 73 additions and 23 deletions

View File

@ -61,14 +61,14 @@ public class OrganizationController {
@PostMapping("/update-member") @PostMapping("/update-member")
@Operation(summary = "更新用户") @Operation(summary = "更新用户")
@RequiresPermissions(PermissionConstants.ORGANIZATION_MEMBER_UPDATE) @RequiresPermissions(value = {PermissionConstants.ORGANIZATION_MEMBER_UPDATE, PermissionConstants.PROJECT_USER_READ_ADD, PermissionConstants.PROJECT_USER_READ_DELETE})
public void updateMember(@Validated @RequestBody OrganizationMemberUpdateRequest organizationMemberExtendRequest) { public void updateMember(@Validated @RequestBody OrganizationMemberUpdateRequest organizationMemberExtendRequest) {
organizationService.updateMember(organizationMemberExtendRequest, SessionUtils.getUserId()); organizationService.updateMember(organizationMemberExtendRequest, SessionUtils.getUserId());
} }
@PostMapping("/project/add-member") @PostMapping("/project/add-member")
@Operation(summary = "添加组织成员至项目") @Operation(summary = "添加组织成员至项目")
@RequiresPermissions(PermissionConstants.ORGANIZATION_MEMBER_UPDATE) @RequiresPermissions(value = {PermissionConstants.ORGANIZATION_MEMBER_UPDATE, PermissionConstants.PROJECT_USER_READ_ADD})
public void addMemberToProject(@Validated @RequestBody OrgMemberExtendProjectRequest orgMemberExtendProjectRequest) { public void addMemberToProject(@Validated @RequestBody OrgMemberExtendProjectRequest orgMemberExtendProjectRequest) {
organizationService.addMemberToProject(orgMemberExtendProjectRequest, SessionUtils.getUserId()); organizationService.addMemberToProject(orgMemberExtendProjectRequest, SessionUtils.getUserId());
} }

View File

@ -497,7 +497,32 @@ public class OrganizationService {
List<String> projectIds = organizationMemberUpdateRequest.getProjectIds(); List<String> projectIds = organizationMemberUpdateRequest.getProjectIds();
if (CollectionUtils.isNotEmpty(projectIds)) { if (CollectionUtils.isNotEmpty(projectIds)) {
updateProjectUserRelation(createUserId, organizationId, memberId, projectIds, sqlSession, logDTOList); updateProjectUserRelation(createUserId, organizationId, memberId, projectIds, sqlSession, logDTOList);
} else {
ProjectExample projectExample = new ProjectExample();
projectExample.createCriteria().andOrganizationIdEqualTo(organizationId);
List<Project> projects = projectMapper.selectByExample(projectExample);
if (CollectionUtils.isNotEmpty(projects)) {
List<String> projectInDBInOrgIds = projects.stream().map(Project::getId).collect(Collectors.toList());
userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(memberId).andSourceIdIn(projectInDBInOrgIds);
userRoleRelationMapper.deleteByExample(userRoleRelationExample);
//add Log
for (String projectInDBInOrgId : projectInDBInOrgIds) {
String path = "/organization/update-member";
LogDTO dto = new LogDTO(
projectInDBInOrgId,
organizationId,
memberId,
createUserId,
OperationLogType.UPDATE.name(),
OperationLogModule.ORGANIZATION_MEMBER,
"成员");
setLog(dto, path, logDTOList, "");
}
}
} }
sqlSession.flushStatements(); sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
//写入操作日志 //写入操作日志

View File

@ -124,7 +124,25 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(3) @Order(4)
public void updateOrgMemberSuccessWithNoSysProjectIds() throws Exception {
OrganizationMemberExtendRequest organizationMemberRequest = new OrganizationMemberExtendRequest();
organizationMemberRequest.setOrganizationId("sys_default_organization_7");
organizationMemberRequest.setMemberIds(Arrays.asList("sys_default_user", "sys_default_user2"));
organizationMemberRequest.setUserRoleIds(Arrays.asList("sys_default_org_role_id_7", "sys_default_project_role_id_8"));
this.requestPost(ORGANIZATION_LIST_ADD_MEMBER, organizationMemberRequest, status().isOk());
OrganizationMemberUpdateRequest organizationMemberUpdateRequest = new OrganizationMemberUpdateRequest();
organizationMemberUpdateRequest.setOrganizationId("sys_default_organization_7");
organizationMemberUpdateRequest.setMemberId("sys_default_user2");
organizationMemberUpdateRequest.setUserRoleIds(List.of("sys_default_org_role_id_7"));
this.requestPost(ORGANIZATION_UPDATE_MEMBER, organizationMemberUpdateRequest, status().isOk());
// 批量添加成员成功后, 验证是否添加成功
listByKeyWord("testUserTwo", "sys_default_organization_7", true, "sys_default_org_role_id_7", null, false, null, null);
}
@Test
@Order(5)
public void updateOrgMemberSuccessWithPartRoles() throws Exception { public void updateOrgMemberSuccessWithPartRoles() throws Exception {
OrganizationMemberUpdateRequest organizationMemberUpdateRequest = new OrganizationMemberUpdateRequest(); OrganizationMemberUpdateRequest organizationMemberUpdateRequest = new OrganizationMemberUpdateRequest();
organizationMemberUpdateRequest.setOrganizationId("sys_default_organization_3"); organizationMemberUpdateRequest.setOrganizationId("sys_default_organization_3");
@ -138,7 +156,7 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(4) @Order(6)
public void updateOrgMemberError() throws Exception { public void updateOrgMemberError() throws Exception {
//组织ID正确 //组织ID正确
// 成员为空 // 成员为空
@ -159,7 +177,7 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(5) @Order(7)
public void updateOrgMemberToRoleSuccess() throws Exception { public void updateOrgMemberToRoleSuccess() throws Exception {
OrganizationMemberExtendRequest organizationMemberRequest = new OrganizationMemberExtendRequest(); OrganizationMemberExtendRequest organizationMemberRequest = new OrganizationMemberExtendRequest();
organizationMemberRequest.setOrganizationId("sys_default_organization_3"); organizationMemberRequest.setOrganizationId("sys_default_organization_3");
@ -171,7 +189,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(6) @Order(8)
public void updateOrgMemberToRoleError() throws Exception { public void updateOrgMemberToRoleError() throws Exception {
//组织ID正确 //组织ID正确
// 成员选择为空 // 成员选择为空
@ -200,7 +218,7 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(7) @Order(9)
public void addOrgMemberToProjectSuccess() throws Exception { public void addOrgMemberToProjectSuccess() throws Exception {
OrgMemberExtendProjectRequest organizationMemberRequest = new OrgMemberExtendProjectRequest(); OrgMemberExtendProjectRequest organizationMemberRequest = new OrgMemberExtendProjectRequest();
organizationMemberRequest.setOrganizationId("sys_default_organization_3"); organizationMemberRequest.setOrganizationId("sys_default_organization_3");
@ -212,7 +230,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(8) @Order(10)
public void addOrgMemberToProjectError() throws Exception { public void addOrgMemberToProjectError() throws Exception {
// 成员选择为空 // 成员选择为空
addOrUpdateOrganizationProjectMemberError(ORGANIZATION_PROJECT_ADD_MEMBER, "sys_default_organization_3", Collections.emptyList(), Arrays.asList("projectId1", "projectId2"), status().isBadRequest()); addOrUpdateOrganizationProjectMemberError(ORGANIZATION_PROJECT_ADD_MEMBER, "sys_default_organization_3", Collections.emptyList(), Arrays.asList("projectId1", "projectId2"), status().isBadRequest());
@ -225,13 +243,13 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(9) @Order(11)
public void getOrgMemberListSuccess() throws Exception { public void getOrgMemberListSuccess() throws Exception {
listByKeyWord("testUserOne", "sys_default_organization_3", false, null, null, false, null, null); listByKeyWord("testUserOne", "sys_default_organization_3", false, null, null, false, null, null);
} }
@Test @Test
@Order(7) @Order(12)
public void getOrgMemberListSuccessWidthEmpty() throws Exception { public void getOrgMemberListSuccessWidthEmpty() throws Exception {
OrganizationRequest organizationRequest = new OrganizationRequest(); OrganizationRequest organizationRequest = new OrganizationRequest();
organizationRequest.setCurrent(1); organizationRequest.setCurrent(1);
@ -258,7 +276,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(8) @Order(13)
public void getOrgMemberListError() throws Exception { public void getOrgMemberListError() throws Exception {
// 页码有误 // 页码有误
OrganizationRequest organizationRequest = new OrganizationRequest(); OrganizationRequest organizationRequest = new OrganizationRequest();
@ -278,26 +296,26 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(11) @Order(14)
public void removeOrgMemberSuccess() throws Exception { public void removeOrgMemberSuccess() throws Exception {
this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/sys_default_organization_6/sys_default_user", status().isOk()); this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/sys_default_organization_6/sys_default_user", status().isOk());
} }
@Test @Test
@Order(12) @Order(15)
public void removeOrgMemberSuccessWithNoProject() throws Exception { public void removeOrgMemberSuccessWithNoProject() throws Exception {
this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/sys_default_organization_3/sys_default_user", status().isOk()); this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/sys_default_organization_3/sys_default_user", status().isOk());
} }
@Test @Test
@Order(13) @Order(16)
public void removeOrgMemberError() throws Exception { public void removeOrgMemberError() throws Exception {
// 项目不存在 // 项目不存在
this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin-x", status().is5xxServerError()); this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin-x", status().is5xxServerError());
} }
@Test @Test
@Order(14) @Order(17)
public void getProjectListByOrgSuccess() throws Exception { public void getProjectListByOrgSuccess() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/project/list/sys_default_organization_3") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/project/list/sys_default_organization_3")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -316,7 +334,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(15) @Order(18)
public void getProjectEmptyListByOrg() throws Exception { public void getProjectEmptyListByOrg() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/project/list/sys_default_organization_5") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/project/list/sys_default_organization_5")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -332,7 +350,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(16) @Order(19)
public void getProjectListByOrgError() throws Exception { public void getProjectListByOrgError() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/organization/project/list/sys_default_organization_x") mockMvc.perform(MockMvcRequestBuilders.get("/organization/project/list/sys_default_organization_x")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -344,7 +362,7 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(17) @Order(20)
public void getUserRoleListByOrgSuccess() throws Exception { public void getUserRoleListByOrgSuccess() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/user/role/list/sys_default_organization_3") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/user/role/list/sys_default_organization_3")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -363,7 +381,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(18) @Order(21)
public void getUserRoleEmptyListByOrg() throws Exception { public void getUserRoleEmptyListByOrg() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/user/role/list/sys_default_organization_6") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/user/role/list/sys_default_organization_6")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -385,7 +403,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(19) @Order(22)
public void getUserRoleListByOrgError() throws Exception { public void getUserRoleListByOrgError() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/organization/user/role/list/sys_default_organization_x") mockMvc.perform(MockMvcRequestBuilders.get("/organization/user/role/list/sys_default_organization_x")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -398,7 +416,7 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(20) @Order(23)
public void getNotExistUserListByOrgSuccess() throws Exception { public void getNotExistUserListByOrgSuccess() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/not-exist/user/list/sys_default_organization_3") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/not-exist/user/list/sys_default_organization_3")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -417,7 +435,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(21) @Order(24)
public void getNotExistUserListWithNoRelation() throws Exception { public void getNotExistUserListWithNoRelation() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/not-exist/user/list/sys_default_organization_5") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/organization/not-exist/user/list/sys_default_organization_5")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -433,7 +451,7 @@ public class OrganizationControllerTests extends BaseTest {
} }
@Test @Test
@Order(22) @Order(25)
public void getNotExistUserListByOrgError() throws Exception { public void getNotExistUserListByOrgError() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/organization/not-exist/user/list/sys_default_organization_x") mockMvc.perform(MockMvcRequestBuilders.get("/organization/not-exist/user/list/sys_default_organization_x")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)

View File

@ -11,6 +11,8 @@ INSERT INTO organization(id,num, name, description, create_time, update_time, cr
('sys_default_organization_5',null, 'organization_name_5', 'XXX-5', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); ('sys_default_organization_5',null, 'organization_name_5', 'XXX-5', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null);
INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE
('sys_default_organization_6',null, 'organization_name_6', 'XXX-6', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); ('sys_default_organization_6',null, 'organization_name_6', 'XXX-6', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null);
INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE
('sys_default_organization_7',null, 'organization_name_7', 'XXX-7', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null);
INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) VALUE INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) VALUE
('sys_default_user', 'testUserOne', 'testUserOne@metersphere.io', MD5('calong'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); ('sys_default_user', 'testUserOne', 'testUserOne@metersphere.io', MD5('calong'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin');
@ -41,6 +43,11 @@ INSERT INTO user_role(id, name, description, internal, type, create_time, update
INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUE INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUE
('sys_default_org_role_id_5', 'sys_default_org_role_id_5', 'XXX', FALSE, 'ORGANIZATION', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'sys_default_organization_3'); ('sys_default_org_role_id_5', 'sys_default_org_role_id_5', 'XXX', FALSE, 'ORGANIZATION', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'sys_default_organization_3');
INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUE
('sys_default_org_role_id_7', 'sys_default_org_role_id_7', 'XXX', FALSE, 'ORGANIZATION', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'sys_default_organization_7');
INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUE
('sys_default_org_role_id_8', 'sys_default_org_role_id_8', 'XXX', FALSE, 'ORGANIZATION', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'sys_default_organization_7');
INSERT INTO user_role_relation(id, user_id, role_id, source_id, create_time, create_user) VALUE INSERT INTO user_role_relation(id, user_id, role_id, source_id, create_time, create_user) VALUE
('gyq_user_role_relation_test', 'sys_default_user4', 'sys_default_org_role_id_5', 'sys_default_organization_6', UNIX_TIMESTAMP() * 1000, 'admin'); ('gyq_user_role_relation_test', 'sys_default_user4', 'sys_default_org_role_id_5', 'sys_default_organization_6', UNIX_TIMESTAMP() * 1000, 'admin');