refactor(系统设置): 项目不能删除最后一个管理员

This commit is contained in:
wxg0103 2024-02-06 18:44:04 +08:00 committed by Craftsman
parent 1ad018b614
commit 152ea72a4c
13 changed files with 86 additions and 99 deletions

View File

@ -39,6 +39,8 @@ not_authorized=未经授权
user_apikey_limit=最多能有5个Api key
please_logout_current_user=请先登出当前用户
resource.name=资源
keep_at_least_one_administrator=至少保留一个管理员
project.member_count.not_blank=成员数量不能为空
#load test
edit_load_test_not_found=无法编辑测试,未找到测试:

View File

@ -39,7 +39,8 @@ login_fail=Login fail
user_apikey_limit=Can have up to 5 api keys
please_logout_current_user=Please logout current user first
resource.name=Resource
keep_at_least_one_administrator=Keep at least one administrator
project.member_count.not_blank=The number of members cannot be empty
#load test
edit_load_test_not_found=Cannot edit test, test not found=
run_load_test_not_found=Cannot run test, test not found=

View File

@ -39,6 +39,8 @@ not_authorized=未经授权
user_apikey_limit=最多能有5个Api key
please_logout_current_user=请先登出当前用户
resource.name=资源
keep_at_least_one_administrator=至少保留一个管理员
project.member_count.not_blank=成员数量不能为空
#load test
edit_load_test_not_found=无法编辑测试,未找到测试:

View File

@ -38,6 +38,8 @@ user_expires=用戶過期
not_authorized=未經授權
user_apikey_limit=最多能有5個Api key
please_logout_current_user=請先登出當前用戶
keep_at_least_one_administrator=至少保留一個管理員
project.member_count.not_blank=成員數不能為空
#load test
edit_load_test_not_found=無法編輯測試,未找到測試:
run_load_test_not_found=無法運行測試,未找到測試:

View File

@ -213,9 +213,7 @@ public class EnvironmentGroupService {
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(userId).andRoleIdEqualTo(InternalUserRole.ADMIN.name());
if (userRoleRelationMapper.countByExample(userRoleRelationExample) > 0) {
ProjectExample example = new ProjectExample();
example.createCriteria().andEnableEqualTo(true);
projects = projectMapper.selectByExample(example);
projects = projectMapper.selectByExample(new ProjectExample());
} else {
projects = extProjectMapper.getProject(userId);
}

View File

@ -60,7 +60,7 @@ import java.util.stream.Collectors;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class FileManagementControllerTests extends BaseTest {
@ -109,9 +109,11 @@ public class FileManagementControllerTests extends BaseTest {
initProject.setName("文件管理专用项目");
initProject.setDescription("建国创建的文件管理专用项目");
initProject.setEnable(true);
initProject.setUserIds(List.of("admin"));
project = commonProjectService.add(initProject, "admin", "/organization-project/add", OperationLogModule.SETTING_ORGANIZATION_PROJECT);
}
}
@Test
@Order(1)
public void emptyDataTest() throws Exception {
@ -2331,6 +2333,7 @@ public class FileManagementControllerTests extends BaseTest {
request.setDropPosition(4);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().is5xxServerError());
}
@Test
@Order(90)
public void deleteModuleTestSuccess() throws Exception {

View File

@ -79,6 +79,7 @@ public class FileRepositoryControllerTest extends BaseTest {
initProject.setName("文件管理存储库专用项目");
initProject.setDescription("建国创建的文件管理存储库专用项目");
initProject.setEnable(true);
initProject.setUserIds(List.of("admin"));
project = commonProjectService.add(initProject, "admin", "/organization-project/add", OperationLogModule.SETTING_ORGANIZATION_PROJECT);
}
}

View File

@ -4,6 +4,7 @@ import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -34,7 +35,8 @@ public class ProjectBaseRequest {
@Schema(description = "模块设置", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private List<String> moduleIds;
@Schema(description = "成员数", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@Schema(description = "成员数", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "{project.member_count.not_blank}", groups = {Created.class, Updated.class})
private List<String> userIds;
@Schema(description = "资源池", requiredMode = Schema.RequiredMode.NOT_REQUIRED)

View File

@ -132,11 +132,8 @@ public class CommonProjectService {
ProjectAddMemberBatchRequest memberRequest = new ProjectAddMemberBatchRequest();
memberRequest.setProjectIds(List.of(project.getId()));
if (CollectionUtils.isEmpty(addProjectDTO.getUserIds())) {
memberRequest.setUserIds(List.of(createUser));
} else {
memberRequest.setUserIds(addProjectDTO.getUserIds());
} //资源池
//资源池
if (CollectionUtils.isNotEmpty(addProjectDTO.getResourcePoolIds())) {
checkResourcePoolExist(addProjectDTO.getResourcePoolIds());
List<ProjectTestResourcePool> projectTestResourcePools = new ArrayList<>();
@ -315,7 +312,7 @@ public class CommonProjectService {
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(example);
List<String> orgUserIds = userRoleRelations.stream().map(UserRoleRelation::getUserId).toList();
List<LogDTO> logDTOList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(updateProjectDto.getUserIds())) {
//updateProjectDto.getUserIds() 为前端传过来的用户id 与数据库中的用户id做对比 如果数据库中的用户id不在前端传过来的用户id中 则删除
List<String> deleteIds = orgUserIds.stream()
.filter(item -> !updateProjectDto.getUserIds().contains(item))
@ -345,19 +342,6 @@ public class CommonProjectService {
this.addProjectAdmin(memberRequest, updateUser, path, OperationLogType.ADD.name(),
Translator.get("add"), module);
}
} else {
if (CollectionUtils.isNotEmpty(orgUserIds)) {
//如果前端传过来的用户id为空 则删除项目所有管理员
UserRoleRelationExample deleteExample = new UserRoleRelationExample();
deleteExample.createCriteria().andSourceIdEqualTo(project.getId()).andUserIdIn(orgUserIds).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelationMapper.selectByExample(deleteExample).forEach(userRoleRelation -> {
User user = userMapper.selectByPrimaryKey(userRoleRelation.getUserId());
LogDTO logDTO = new LogDTO(OperationLogConstants.SYSTEM, project.getOrganizationId(), userRoleRelation.getId(), updateUser, OperationLogType.DELETE.name(), module, Translator.get("delete") + Translator.get("project_admin") + ": " + user.getName());
setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList);
});
userRoleRelationMapper.deleteByExample(deleteExample);
}
}
if (CollectionUtils.isNotEmpty(logDTOList)) {
operationLogService.batchAdd(logDTOList);
}
@ -506,11 +490,19 @@ public class CommonProjectService {
UserExample userExample = new UserExample();
userExample.createCriteria().andIdEqualTo(userId).andDeletedEqualTo(false);
List<User> users = userMapper.selectByExample(userExample);
User user = CollectionUtils.isNotEmpty(users) ? users.get(0) : null;
User user = CollectionUtils.isNotEmpty(users) ? users.getFirst() : null;
if (user == null) {
throw new MSException(Translator.get("user_not_exist"));
}
//判断用户是不是最后一个管理员 如果是 就报错
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdNotEqualTo(userId)
.andSourceIdEqualTo(projectId)
.andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
if (userRoleRelationMapper.countByExample(userRoleRelationExample) == 0) {
throw new MSException(Translator.get("keep_at_least_one_administrator"));
}
userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(userId)
.andSourceIdEqualTo(projectId);
if (StringUtils.equals(projectId, user.getLastProjectId())) {

View File

@ -21,6 +21,8 @@ import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.List;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -50,6 +52,7 @@ public class LoginControllerTests extends BaseTest {
project.setOrganizationId(DEFAULT_ORGANIZATION_ID);
project.setName("test-login-projectName");
project.setEnable(true);
project.setUserIds(List.of("test.login"));
ProjectDTO add = systemProjectService.add(project, "test.login");

View File

@ -356,7 +356,7 @@ public class OrganizationProjectControllerTests extends BaseTest {
checkLog(projectId, OperationLogType.ADD);
//userId为空的时候
project = this.generatorAdd("organizationId", "organization-userIdIsNull", "description", true, new ArrayList<>());
project = this.generatorAdd("organizationId", "organization-userIdIsNull", "description", true, List.of("admin"));
mvcResult = this.responsePost(addProject, project);
result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
projectExample = new ProjectExample();
@ -368,14 +368,6 @@ public class OrganizationProjectControllerTests extends BaseTest {
checkLog(projectId, OperationLogType.ADD);
compareProjectDTO(projects.get(0), result);
userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo(projectId).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().contains("admin"));
userRoleRelationExample.createCriteria().andSourceIdEqualTo("organizationId").andRoleIdEqualTo(InternalUserRole.ORG_MEMBER.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().contains("admin"));
projectExtend = projectMapper.selectByPrimaryKey(projectId);
Assertions.assertNull(projectExtend.getModuleSetting());
//设置了模块模版
@ -456,6 +448,9 @@ public class OrganizationProjectControllerTests extends BaseTest {
project = this.generatorAdd("organizationId", "org-pool-error", null, true, List.of("admin"));
project.setResourcePoolIds(List.of("resourcePoolId3"));
this.requestPost(addProject, project, ERROR_REQUEST_MATCHER);
//成员为空
project = this.generatorAdd("organizationId", "org-pool-error", null, true, null);
this.requestPost(addProject, project, BAD_REQUEST_MATCHER);
}
@ -601,7 +596,7 @@ public class OrganizationProjectControllerTests extends BaseTest {
@Test
@Order(7)
public void testUpdateProject() throws Exception {
UpdateProjectRequest project = this.generatorUpdate("organizationId", projectId, "organization-TestName", "Edit name", true, List.of("admin1"));
UpdateProjectRequest project = this.generatorUpdate("organizationId", projectId, "organization-TestName", "Edit name", true, List.of("admin", "admin1"));
Project projectExtend = projectMapper.selectByPrimaryKey(projectId);
List<String> moduleIds = new ArrayList<>();
if (StringUtils.isNotBlank(projectExtend.getModuleSetting())) {
@ -626,22 +621,17 @@ public class OrganizationProjectControllerTests extends BaseTest {
// 校验日志
checkLog(projectId, OperationLogType.ADD);
//用户id为空
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "organization-TestNameUserIdIsNull", "Edit name", true, new ArrayList<>());
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "organization-TestNameUserIdIsNull", "Edit name", true, List.of("admin1"));
mvcResult = this.responsePost(updateProject, project);
result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
currentProject = projectMapper.selectByPrimaryKey(project.getId());
compareProjectDTO(currentProject, result);
userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo("projectId2").andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
//断言userRoleRelations是空的
Assertions.assertTrue(userRoleRelations.isEmpty());
//断言模块设置
projectExtend = projectMapper.selectByPrimaryKey("projectId2");
Assertions.assertEquals(projectExtend.getModuleSetting(), CollectionUtils.isEmpty(project.getModuleIds()) ? null : JSON.toJSONString(project.getModuleIds()));
// 修改模块设置
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "org-Module", "Edit name", true, new ArrayList<>());
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "org-Module", "Edit name", true, List.of("admin1"));
moduleIds = new ArrayList<>();
moduleIds.add("apiTest");
moduleIds.add("uiTest");
@ -650,17 +640,12 @@ public class OrganizationProjectControllerTests extends BaseTest {
result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
currentProject = projectMapper.selectByPrimaryKey(project.getId());
compareProjectDTO(currentProject, result);
userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo("projectId2").andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
//断言userRoleRelations是空的
Assertions.assertTrue(userRoleRelations.isEmpty());
//断言模块设置
projectExtend = projectMapper.selectByPrimaryKey("projectId2");
Assertions.assertEquals(projectExtend.getModuleSetting(), JSON.toJSONString(moduleIds));
//设置资源池
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId3", "org-updatePools", "org-updatePools", true, new ArrayList<>());
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId3", "org-updatePools", "org-updatePools", true, List.of("admin1"));
project.setResourcePoolIds(List.of("resourcePoolId", "resourcePoolId1"));
mvcResult = this.responsePost(updateProject, project);
result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
@ -701,6 +686,8 @@ public class OrganizationProjectControllerTests extends BaseTest {
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId", "org-Module-pool", null, true, List.of("admin"));
project.setResourcePoolIds(List.of("resourcePoolId3"));
this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER);
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId", "org-Module-pool", null, true, null);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER);
}
@ -897,6 +884,7 @@ public class OrganizationProjectControllerTests extends BaseTest {
projectId = "projectId111";
userId = "admin1";
this.responseGet(removeProjectMember + projectId + "/" + userId, ERROR_REQUEST_MATCHER);
this.responseGet(removeProjectMember + "projectId3" + "/" + "admin1", ERROR_REQUEST_MATCHER);
}
@Test

View File

@ -349,7 +349,7 @@ public class SystemProjectControllerTests extends BaseTest {
assertionsRefStatusFlowSetting(projectId);
//userId为空的时候
project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "userIdIsNull", "description", true, new ArrayList<>());
project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "userIdNotNull", "description", true, List.of("admin"));
// 开启项目模板
changeOrgTemplateEnable(false);
mvcResult = this.responsePost(addProject, project);
@ -626,6 +626,9 @@ public class SystemProjectControllerTests extends BaseTest {
project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "pool", null, true, List.of("admin"));
project.setResourcePoolIds(List.of("resourcePoolId3"));
this.requestPost(addProject, project, ERROR_REQUEST_MATCHER);
//成员为空
project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "name", null, true, null);
this.requestPost(addProject, project, BAD_REQUEST_MATCHER);
}
@ -779,22 +782,8 @@ public class SystemProjectControllerTests extends BaseTest {
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().containsAll(List.of("admin", "admin1")));
//用户id为空
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId1", "TestNameUserIdIsNull", "Edit name", true, new ArrayList<>());
Project projectExtend = projectMapper.selectByPrimaryKey("projectId1");
projectExtend.setModuleSetting(null);
mvcResult = this.responsePost(updateProject, project);
result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
currentProject = projectMapper.selectByPrimaryKey(project.getId());
compareProjectDTO(currentProject, result);
userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo("projectId1").andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
//断言userRoleRelations是空的
Assertions.assertTrue(userRoleRelations.isEmpty());
// 修改模块设置
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId1", "Module", "Edit name", true, new ArrayList<>());
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId1", "TestName2", "Edit name", true, List.of("admin"));
List<String> moduleIds = new ArrayList<>();
moduleIds.add("apiTest");
moduleIds.add("uiTest");
@ -804,17 +793,12 @@ public class SystemProjectControllerTests extends BaseTest {
result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
currentProject = projectMapper.selectByPrimaryKey(project.getId());
compareProjectDTO(currentProject, result);
userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo("projectId1").andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
//断言userRoleRelations是空的
Assertions.assertTrue(userRoleRelations.isEmpty());
//断言模块设置
projectExtend = projectMapper.selectByPrimaryKey("projectId1");
Project projectExtend = projectMapper.selectByPrimaryKey("projectId1");
Assertions.assertEquals(projectExtend.getModuleSetting(), JSON.toJSONString(moduleIds));
//设置资源池
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId5", "updatePools", "updatePools", true, new ArrayList<>());
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId5", "updatePools", "updatePools", true, List.of("admin"));
project.setResourcePoolIds(List.of("resourcePoolId", "resourcePoolId1"));
mvcResult = this.responsePost(updateProject, project);
result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class);
@ -854,6 +838,9 @@ public class SystemProjectControllerTests extends BaseTest {
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "pool-edit", "description", true, List.of("admin"));
project.setResourcePoolIds(List.of("resourcePoolId3"));
this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER);
//成员为空
project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "name", null, true, null);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER);
}
@ -1043,6 +1030,10 @@ public class SystemProjectControllerTests extends BaseTest {
projectId = "projectId111";
userId = "admin1";
this.responseGet(removeProjectMember + projectId + "/" + userId, ERROR_REQUEST_MATCHER);
//用户id为最后一个管理员
projectId = "projectId5";
userId = "admin";
this.responseGet(removeProjectMember + projectId + "/" + userId, ERROR_REQUEST_MATCHER);
}
@Test

View File

@ -126,6 +126,7 @@ public class TestPlanTests extends BaseTest {
private static TestPlan repeatCaseTestPlan;
private static final String[] PROJECT_MODULE = new String[]{"workstation", "testPlan", "bugManagement", "caseManagement", "apiTest", "uiTest", "loadTest"};
@BeforeEach
public void initTestData() {
//文件管理专用项目
@ -135,6 +136,7 @@ public class TestPlanTests extends BaseTest {
initProject.setName("文件管理专用项目");
initProject.setDescription("建国创建的文件管理专用项目");
initProject.setEnable(true);
initProject.setUserIds(List.of("admin"));
project = commonProjectService.add(initProject, "admin", "/organization-project/add", OperationLogModule.SETTING_ORGANIZATION_PROJECT);