refactor(系统设置): 项目补充Tag

This commit is contained in:
wxg0103 2023-07-19 11:29:46 +08:00 committed by 刘瑞斌
parent dea6c43a66
commit cd57074414
7 changed files with 109 additions and 26 deletions

View File

@ -3,14 +3,18 @@ package io.metersphere.sdk.controller;
import io.metersphere.sdk.dto.LicenseDTO; import io.metersphere.sdk.dto.LicenseDTO;
import io.metersphere.sdk.service.LicenseService; import io.metersphere.sdk.service.LicenseService;
import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.CommonBeanFactory;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/license") @RequestMapping("/license")
@Tag(name="license校验")
public class LicenseValidateController { public class LicenseValidateController {
@GetMapping("/validate") @GetMapping("/validate")
@Operation(summary = "license校验")
public LicenseDTO validate() { public LicenseDTO validate() {
LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class); LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class);
if (licenseService != null) { if (licenseService != null) {

View File

@ -22,6 +22,9 @@ import io.metersphere.system.service.SystemProjectService;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated; import io.metersphere.validation.groups.Updated;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.NotBlank; import org.apache.logging.log4j.core.config.plugins.validation.constraints.NotBlank;
@ -32,6 +35,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@Tag(name = "系统-项目")
@RequestMapping("/system/project") @RequestMapping("/system/project")
public class SystemProjectController { public class SystemProjectController {
@Resource @Resource
@ -48,6 +52,7 @@ public class SystemProjectController {
@GetMapping("/get/{id}") @GetMapping("/get/{id}")
@Operation(summary = "根据ID获取项目信息") @Operation(summary = "根据ID获取项目信息")
@Parameter(name = "id", description = "项目id", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
@RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ)
public Project getProject(@PathVariable @NotBlank String id) { public Project getProject(@PathVariable @NotBlank String id) {
return systemProjectService.get(id); return systemProjectService.get(id);
@ -73,6 +78,7 @@ public class SystemProjectController {
@GetMapping("/delete/{id}") @GetMapping("/delete/{id}")
@RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_DELETE) @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_DELETE)
@Operation(summary = "删除项目") @Operation(summary = "删除项目")
@Parameter(name = "id", description = "项目", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
@Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = SystemProjectLogService.class) @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = SystemProjectLogService.class)
public int deleteProject(@PathVariable String id) { public int deleteProject(@PathVariable String id) {
return systemProjectService.delete(id, SessionUtils.getUserId()); return systemProjectService.delete(id, SessionUtils.getUserId());
@ -81,6 +87,7 @@ public class SystemProjectController {
@GetMapping("/revoke/{id}") @GetMapping("/revoke/{id}")
@RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_RECOVER) @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_RECOVER)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#id)", msClass = SystemProjectLogService.class) @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#id)", msClass = SystemProjectLogService.class)
@Parameter(name = "id", description = "项目", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
public int revokeProject(@PathVariable String id) { public int revokeProject(@PathVariable String id) {
return systemProjectService.revoke(id); return systemProjectService.revoke(id);
} }
@ -103,6 +110,8 @@ public class SystemProjectController {
@GetMapping("/remove-member/{projectId}/{userId}") @GetMapping("/remove-member/{projectId}/{userId}")
@Operation(summary = "移除项目成员") @Operation(summary = "移除项目成员")
@Parameter(name = "userId", description = "用户id", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
@Parameter(name = "projectId", description = "项目id", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
@RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE)
@Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#projectId)", msClass = SystemProjectLogService.class) @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#projectId)", msClass = SystemProjectLogService.class)
public int removeProjectMember(@PathVariable String projectId, @PathVariable String userId) { public int removeProjectMember(@PathVariable String projectId, @PathVariable String userId) {

View File

@ -39,15 +39,7 @@
FROM project p FROM project p
LEFT JOIN user_role_relation u on p.id = u.source_id LEFT JOIN user_role_relation u on p.id = u.source_id
INNER JOIN organization o on p.organization_id = o.id INNER JOIN organization o on p.organization_id = o.id
<where> <include refid="queryWhereCondition"/>
<if test="request.organizationId != null">
p.organization_id = #{request.organizationId}
</if>
<if test="request.keyword != null">
and (p.name like CONCAT('%', #{request.keyword},'%')
or p.num like CONCAT('%', #{request.keyword},'%'))
</if>
</where>
group by p.id group by p.id
</select> </select>
<select id="getProjectAdminList" resultType="io.metersphere.system.domain.User"> <select id="getProjectAdminList" resultType="io.metersphere.system.domain.User">
@ -64,4 +56,48 @@
<select id="selectProjectOptions" resultType="io.metersphere.system.dto.OrganizationProjectOptionsDto"> <select id="selectProjectOptions" resultType="io.metersphere.system.dto.OrganizationProjectOptionsDto">
select id, name from project order by create_time desc select id, name from project order by create_time desc
</select> </select>
<sql id="queryWhereCondition">
<where>
<if test="request.organizationId != null">
p.organization_id = #{request.organizationId}
</if>
<if test="request.keyword != null">
and (
p.name like concat('%', #{request.keyword},'%')
or p.num like concat('%', #{request.keyword},'%')
)
</if>
<include refid="filter"/>
<include refid="combine">
<property name="condition" value="request.combine"/>
</include>
</where>
</sql>
<sql id="filter">
<if test="request.filter != null and request.filter.size() > 0">
<foreach collection="request.filter.entrySet()" index="key" item="values">
<if test="values != null and values.size() > 0">
<choose>
<when test="key=='createUser'">
and p.create_user in
<include refid="io.metersphere.sdk.mapper.BaseMapper.filterInWrapper"/>
</when>
</choose>
</if>
</foreach>
</if>
</sql>
<sql id="combine">
<if test="request.combine != null">
<if test='${condition}.createUser != null'>
and p.create_user
<include refid="io.metersphere.sdk.mapper.BaseMapper.condition">
<property name="object" value="${condition}.createUser"/>
</include>
</if>
</if>
</sql>
</mapper> </mapper>

View File

@ -34,7 +34,7 @@ public class SystemProjectLogService {
null, null,
null, null,
OperationLogType.ADD.name(), OperationLogType.ADD.name(),
OperationLogModule.SYSTEM_USER_ROLE, OperationLogModule.SYSTEM_PROJECT,
project.getName()); project.getName());
dto.setPath(PRE_URI + "/add"); dto.setPath(PRE_URI + "/add");
@ -56,7 +56,7 @@ public class SystemProjectLogService {
project.getId(), project.getId(),
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_USER_ROLE, OperationLogModule.SYSTEM_PROJECT,
"编辑全局用户组对应的权限配置"); "编辑全局用户组对应的权限配置");
dto.setPath("/update"); dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name()); dto.setMethod(HttpMethodConstants.POST.name());
@ -75,11 +75,11 @@ public class SystemProjectLogService {
"", "",
project.getId(), project.getId(),
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.RECOVER.name(),
OperationLogModule.SYSTEM_USER_ROLE, OperationLogModule.SYSTEM_PROJECT,
"编辑全局用户组对应的权限配置"); "编辑全局用户组对应的权限配置");
dto.setPath("/revoke"); dto.setPath("/revoke");
dto.setMethod(HttpMethodConstants.POST.name()); dto.setMethod(HttpMethodConstants.GET.name());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
@ -103,11 +103,11 @@ public class SystemProjectLogService {
id, id,
project.getCreateUser(), project.getCreateUser(),
OperationLogType.DELETE.name(), OperationLogType.DELETE.name(),
OperationLogModule.SYSTEM_USER_ROLE, OperationLogModule.SYSTEM_PROJECT,
project.getName()); project.getName());
dto.setPath("/delete"); dto.setPath("/delete");
dto.setMethod(HttpMethodConstants.POST.name()); dto.setMethod(HttpMethodConstants.GET.name());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
} }

View File

@ -8,6 +8,7 @@ import io.metersphere.sdk.dto.AddProjectRequest;
import io.metersphere.sdk.dto.ProjectDTO; import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.sdk.dto.UpdateProjectRequest; import io.metersphere.sdk.dto.UpdateProjectRequest;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.service.OperationLogService;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.User; import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.domain.UserRoleRelation;
@ -44,6 +45,8 @@ public class SystemProjectService {
private UserRoleRelationMapper userRoleRelationMapper; private UserRoleRelationMapper userRoleRelationMapper;
@Resource @Resource
private ExtSystemProjectMapper extSystemProjectMapper; private ExtSystemProjectMapper extSystemProjectMapper;
@Resource
private OperationLogService operationLogService;
public Project get(String id) { public Project get(String id) {
return projectMapper.selectByPrimaryKey(id); return projectMapper.selectByPrimaryKey(id);
@ -173,6 +176,10 @@ public class SystemProjectService {
createUser); createUser);
userRoleRelationMapper.insertSelective(adminRole); userRoleRelationMapper.insertSelective(adminRole);
} }
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(userId)
.andSourceIdEqualTo(request.getProjectId()).andRoleIdEqualTo(InternalUserRole.PROJECT_MEMBER.getValue());
if (userRoleRelationMapper.selectByExample(userRoleRelationExample).size() == 0) {
UserRoleRelation memberRole = new UserRoleRelation( UserRoleRelation memberRole = new UserRoleRelation(
UUID.randomUUID().toString(), UUID.randomUUID().toString(),
userId, userId,
@ -181,6 +188,7 @@ public class SystemProjectService {
System.currentTimeMillis(), System.currentTimeMillis(),
createUser); createUser);
userRoleRelationMapper.insertSelective(memberRole); userRoleRelationMapper.insertSelective(memberRole);
}
}); });
} }

View File

@ -5,6 +5,7 @@ import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectExample; import io.metersphere.project.domain.ProjectExample;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.InternalUserRole; import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.AddProjectRequest; import io.metersphere.sdk.dto.AddProjectRequest;
@ -179,6 +180,10 @@ public class SystemProjectControllerTests extends BaseTest {
userRoleRelationExample.createCriteria().andSourceIdEqualTo(projectId).andRoleIdEqualTo(InternalUserRole.PROJECT_MEMBER.getValue()); userRoleRelationExample.createCriteria().andSourceIdEqualTo(projectId).andRoleIdEqualTo(InternalUserRole.PROJECT_MEMBER.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
Assertions.assertEquals(userRoleRelations.stream().map(UserRoleRelation::getUserId).collect(Collectors.toList()).containsAll(List.of("admin")), true); Assertions.assertEquals(userRoleRelations.stream().map(UserRoleRelation::getUserId).collect(Collectors.toList()).containsAll(List.of("admin")), true);
project.setName("testAddProjectSuccess1");
// @@校验权限
requestPostPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_ADD, addProject, project);
} }
@Test @Test
@ -210,6 +215,8 @@ public class SystemProjectControllerTests extends BaseTest {
MvcResult mvcResult = this.responseGet(getProject + projectId); MvcResult mvcResult = this.responseGet(getProject + projectId);
Project project = this.parseObjectFromMvcResult(mvcResult, Project.class); Project project = this.parseObjectFromMvcResult(mvcResult, Project.class);
Assertions.assertTrue(StringUtils.equals(project.getId(), projectId)); Assertions.assertTrue(StringUtils.equals(project.getId(), projectId));
// @@校验权限
requestGetPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, getProject + projectId);
} }
@Test @Test
@Order(4) @Order(4)
@ -246,6 +253,18 @@ public class SystemProjectControllerTests extends BaseTest {
for (ProjectDTO projectDTO : projectDTOS) { for (ProjectDTO projectDTO : projectDTOS) {
Assertions.assertFalse(projectDTO.getCreateTime() > firstCreateTime); Assertions.assertFalse(projectDTO.getCreateTime() > firstCreateTime);
} }
projectRequest.setFilter(new HashMap<>() {{
put("createUser", List.of("test"));
}});
mvcResult = this.responsePost(getProjectList, projectRequest);
returnPager = parseObjectFromMvcResult(mvcResult, Pager.class);
//返回的数据中的createUser是admin或者admin1
projectDTOS = JSON.parseArray(JSON.toJSONString(returnPager.getList()), ProjectDTO.class);
//拿到所有的createUser
List<String> createUsers = projectDTOS.stream().map(ProjectDTO::getCreateUser).collect(Collectors.toList());
Assertions.assertTrue(List.of("test").containsAll(createUsers));
// @@校验权限
requestPostPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, getProjectList, projectRequest);
} }
@Test @Test
@ -289,13 +308,16 @@ public class SystemProjectControllerTests extends BaseTest {
userRoleRelationExample.createCriteria().andSourceIdEqualTo("projectId").andRoleIdEqualTo(InternalUserRole.PROJECT_MEMBER.getValue()); userRoleRelationExample.createCriteria().andSourceIdEqualTo("projectId").andRoleIdEqualTo(InternalUserRole.PROJECT_MEMBER.getValue());
userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
Assertions.assertEquals(userRoleRelations.stream().map(UserRoleRelation::getUserId).collect(Collectors.toList()).containsAll(List.of("admin", "admin1")), true); Assertions.assertEquals(userRoleRelations.stream().map(UserRoleRelation::getUserId).collect(Collectors.toList()).containsAll(List.of("admin", "admin1")), true);
// @@校验权限
project.setName("TestName2");
requestPostPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE, updateProject, project);
} }
@Test @Test
@Order(8) @Order(8)
public void testUpdateProjectError() throws Exception { public void testUpdateProjectError() throws Exception {
//项目名称存在 500 //项目名称存在 500
UpdateProjectRequest project = this.generatorUpdate("organizationId", "projectId","TestName", "description", true, List.of("admin")); UpdateProjectRequest project = this.generatorUpdate("organizationId", "projectId","TestName2", "description", true, List.of("admin"));
this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER);
//参数组织Id为空 //参数组织Id为空
project = this.generatorUpdate(null, "projectId",null, null, true , List.of("admin")); project = this.generatorUpdate(null, "projectId",null, null, true , List.of("admin"));
@ -328,6 +350,8 @@ public class SystemProjectControllerTests extends BaseTest {
Assertions.assertEquals(currentProject.getDeleted(), true); Assertions.assertEquals(currentProject.getDeleted(), true);
Assertions.assertTrue(currentProject.getId().equals(id)); Assertions.assertTrue(currentProject.getId().equals(id));
Assertions.assertTrue(count == 1); Assertions.assertTrue(count == 1);
// @@校验权限
requestGetPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_DELETE, deleteProject + id);
} }
@Test @Test
@ -349,6 +373,8 @@ public class SystemProjectControllerTests extends BaseTest {
Assertions.assertEquals(currentProject.getDeleted(), false); Assertions.assertEquals(currentProject.getDeleted(), false);
Assertions.assertTrue(currentProject.getId().equals(id)); Assertions.assertTrue(currentProject.getId().equals(id));
Assertions.assertTrue(count == 1); Assertions.assertTrue(count == 1);
// @@校验权限
requestGetPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_RECOVER, revokeProject + id);
} }
@Test @Test

View File

@ -5,7 +5,7 @@ INSERT INTO project (id, num, organization_id, name, description, create_user, u
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId2', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目2', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId2', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目2', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000);
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId3', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目3', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId3', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目3', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000);
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId4', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目4', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId4', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目4', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000);
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId5', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目5', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('projectId5', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目5', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000);
insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source,
last_project_id, create_user, update_user) last_project_id, create_user, update_user)