refactor(项目设置): 项目用户组列表查询改为分页

This commit is contained in:
song-cc-rock 2023-09-14 17:39:34 +08:00 committed by fit2-zhao
parent 73d319ccfa
commit b5ed0ab0f0
8 changed files with 225 additions and 55 deletions

View File

@ -2,9 +2,11 @@ package io.metersphere.project.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.project.dto.ProjectUserRoleDTO;
import io.metersphere.project.request.ProjectUserRoleEditRequest;
import io.metersphere.project.request.ProjectUserRoleMemberEditRequest;
import io.metersphere.project.request.ProjectUserRoleMemberRequest;
import io.metersphere.project.request.ProjectUserRoleRequest;
import io.metersphere.project.service.ProjectUserRoleLogService;
import io.metersphere.project.service.ProjectUserRoleService;
import io.metersphere.sdk.constants.PermissionConstants;
@ -39,12 +41,12 @@ public class ProjectUserRoleController {
@Resource
ProjectUserRoleService projectUserRoleService;
@GetMapping("/list/{projectId}")
@PostMapping("/list")
@Operation(summary = "项目管理-项目与权限-用户组-获取用户组列表")
@Parameter(name = "projectId", description = "当前项目ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
@RequiresPermissions(PermissionConstants.PROJECT_GROUP_READ)
public List<UserRole> list(@PathVariable String projectId) {
return projectUserRoleService.list(projectId);
public Pager<List<ProjectUserRoleDTO>> list(@Validated @RequestBody ProjectUserRoleRequest request) {
Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize());
return PageUtils.setPageInfo(page, projectUserRoleService.list(request));
}
@PostMapping("/add")

View File

@ -0,0 +1,27 @@
package io.metersphere.project.dto;
import io.metersphere.system.domain.UserRole;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serial;
/**
* 项目用户组列表DTO
*
* @author song-cc-rock
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class ProjectUserRoleDTO extends UserRole {
@Serial
private static final long serialVersionUID = 1L;
/**
* 成员数目
*/
@Schema(description = "成员数目")
private Integer memberCount;
}

View File

@ -1,7 +1,10 @@
package io.metersphere.project.mapper;
import io.metersphere.project.dto.ProjectUserRoleDTO;
import io.metersphere.project.request.ProjectUserRoleMemberRequest;
import io.metersphere.project.request.ProjectUserRoleRequest;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRoleRelation;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -11,8 +14,25 @@ import java.util.List;
*/
public interface ExtProjectUserRoleMapper {
/**
* 获取项目用户组列表
*
* @param request 请求参数
* @return 项目用户组列表
*/
List<ProjectUserRoleDTO> list(@Param("request") ProjectUserRoleRequest request);
/**
* 根据用户组ID获取用户组成员关系
*
* @param roleIds 用户组ID集合
* @return 用户组成员关系
*/
List<UserRoleRelation> getRelationByRoleIds(@Param("roleIds") List<String> roleIds);
/**
* 获取项目成员列表
*
* @param request 请求参数
* @return 项目成员列表
*/
@ -20,6 +40,7 @@ public interface ExtProjectUserRoleMapper {
/**
* 根据项目id获取 项目成员
*
* @param sourceId
* @return
*/

View File

@ -1,6 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.project.mapper.ExtProjectUserRoleMapper">
<select id="list" resultType="io.metersphere.project.dto.ProjectUserRoleDTO">
select ur.* from user_role ur
where ur.type = 'PROJECT' and scope_id in ('global', #{request.projectId})
<if test="request.keyword != null and request.keyword != ''">
and (
ur.id like concat('%', #{request.keyword}, '%') or
ur.name like concat('%', #{request.keyword}, '%')
)
</if>
order by ur.create_time desc
</select>
<select id="getRelationByRoleIds" resultType="io.metersphere.system.domain.UserRoleRelation">
select urr.*
from user_role_relation urr
left join user u on urr.user_id = u.id
where u.deleted = 0 and urr.role_id in
<foreach collection="roleIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<select id="listProjectRoleMember" resultType="io.metersphere.system.domain.User">
select u.*
from user_role_relation urr left join user u on urr.user_id = u.id
@ -12,14 +34,12 @@
</select>
<select id="getProjectUserList" resultType="io.metersphere.system.domain.User">
SELECT
u.id,
u.NAME
FROM
`user` u
LEFT JOIN user_role_relation urr ON u.id = urr.user_id
WHERE
urr.source_id = #{sourceId} and u.deleted = false
SELECT u.id,
u.NAME
FROM `user` u
LEFT JOIN user_role_relation urr ON u.id = urr.user_id
WHERE urr.source_id = #{sourceId}
and u.deleted = false
order by u.create_time desc
</select>
</mapper>

View File

@ -0,0 +1,22 @@
package io.metersphere.project.request;
import io.metersphere.sdk.dto.BasePageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 项目用户组列表请求参数
*
* @author song-cc-rock
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class ProjectUserRoleRequest extends BasePageRequest {
/**
* 项目ID
*/
@Schema(description = "项目ID")
private String projectId;
}

View File

@ -1,10 +1,11 @@
package io.metersphere.project.service;
import io.metersphere.project.dto.ProjectUserRoleDTO;
import io.metersphere.project.mapper.ExtProjectUserRoleMapper;
import io.metersphere.project.request.ProjectUserRoleMemberEditRequest;
import io.metersphere.project.request.ProjectUserRoleMemberRequest;
import io.metersphere.project.request.ProjectUserRoleRequest;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.UserRoleEnum;
import io.metersphere.sdk.constants.UserRoleType;
import io.metersphere.sdk.dto.PermissionDefinitionItem;
import io.metersphere.sdk.dto.UserExtend;
@ -25,7 +26,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -50,12 +50,28 @@ public class ProjectUserRoleService extends BaseUserRoleService {
@Resource
ExtProjectUserRoleMapper extProjectUserRoleMapper;
public List<UserRole> list(String projectId) {
UserRoleExample example = new UserRoleExample();
example.createCriteria().andTypeEqualTo(UserRoleType.PROJECT.name())
.andScopeIdIn(Arrays.asList(projectId, UserRoleEnum.GLOBAL.toString()));
example.setOrderByClause("create_time asc");
return userRoleMapper.selectByExample(example);
public List<ProjectUserRoleDTO> list(ProjectUserRoleRequest request) {
List<ProjectUserRoleDTO> roles = extProjectUserRoleMapper.list(request);
if (CollectionUtils.isEmpty(roles)) {
return new ArrayList<>();
}
List<String> roleIds = roles.stream().map(ProjectUserRoleDTO::getId).toList();
List<UserRoleRelation> relations = extProjectUserRoleMapper.getRelationByRoleIds(roleIds);
if (CollectionUtils.isNotEmpty(relations)) {
Map<String, Long> countMap = relations.stream().collect(Collectors.groupingBy(UserRoleRelation::getRoleId, Collectors.counting()));
roles.forEach(role -> {
if (countMap.containsKey(role.getId())) {
role.setMemberCount(countMap.get(role.getId()).intValue());
} else {
role.setMemberCount(0);
}
});
} else {
roles.forEach(role -> {
role.setMemberCount(0);
});
}
return roles;
}
@Override

View File

@ -1,8 +1,10 @@
package io.metersphere.project.controller;
import io.metersphere.project.dto.ProjectUserRoleDTO;
import io.metersphere.project.request.ProjectUserRoleEditRequest;
import io.metersphere.project.request.ProjectUserRoleMemberEditRequest;
import io.metersphere.project.request.ProjectUserRoleMemberRequest;
import io.metersphere.project.request.ProjectUserRoleRequest;
import io.metersphere.sdk.base.BaseTest;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.PermissionConstants;
@ -13,7 +15,6 @@ import io.metersphere.sdk.service.BaseUserRolePermissionService;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.request.OrganizationUserRoleEditRequest;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
@ -28,6 +29,7 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.Serial;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@ -63,17 +65,42 @@ public class ProjectUserRoleControllerTests extends BaseTest {
@Order(0)
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "/dml/init_project_user_role.sql")
public void testProjectUserRoleListSuccess() throws Exception {
String projectId = "default-project-2";
MvcResult mvcResult = this.responseGet(PROJECT_USER_ROLE_LIST + "/" + projectId);
ProjectUserRoleRequest request = new ProjectUserRoleRequest();
request.setCurrent(1);
request.setPageSize(10);
request.setProjectId("default-project-2");
request.setKeyword("default-pro-role-3");
MvcResult mvcResult = this.responsePost(PROJECT_USER_ROLE_LIST, request);
// 获取返回值
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
// 返回总条数是否为init_project_user_role.sql中的数据总数
Assertions.assertFalse(JSON.parseArray(JSON.toJSONString(resultHolder.getData())).isEmpty());
Pager<?> pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
// 返回值不为空
Assertions.assertNotNull(pageData);
// 返回值的页码和当前页码相同
Assertions.assertEquals(pageData.getCurrent(), request.getCurrent());
// 返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= request.getPageSize());
// 返回值中取出第一条数据, 并判断是否包含关键字default
ProjectUserRoleDTO projectUserRoleDTO = JSON.parseArray(JSON.toJSONString(pageData.getList()), ProjectUserRoleDTO.class).get(0);
Assertions.assertTrue(StringUtils.contains(projectUserRoleDTO.getName(), request.getKeyword())
|| StringUtils.contains(projectUserRoleDTO.getId(), request.getKeyword()));
// 权限校验
requestGetPermissionTest(PermissionConstants.PROJECT_GROUP_READ, PROJECT_USER_ROLE_LIST + "/" + DEFAULT_PROJECT_ID);
request.setProjectId(DEFAULT_PROJECT_ID);
requestPostPermissionTest(PermissionConstants.PROJECT_GROUP_READ, PROJECT_USER_ROLE_LIST, request);
// 覆盖用户组没有成员的情况
request.setProjectId("default-project-2");
request.setKeyword("");
this.requestPost(PROJECT_USER_ROLE_LIST, request);
// 覆盖空数据
request.setProjectId("default-project-3");
request.setKeyword("project_member");
this.requestPost(PROJECT_USER_ROLE_LIST, request);
request.setKeyword("not_exit");
this.requestPost(PROJECT_USER_ROLE_LIST, request);
}
@Test
@ -84,15 +111,23 @@ public class ProjectUserRoleControllerTests extends BaseTest {
request.setScopeId("default-project-2");
this.requestPost(PROJECT_USER_ROLE_ADD, request);
// 验证是否添加成功
String projectId = "default-project-2";
MvcResult mvcResult = this.responseGet(PROJECT_USER_ROLE_LIST + "/" + projectId);
ProjectUserRoleRequest roleRequest = new ProjectUserRoleRequest();
roleRequest.setCurrent(1);
roleRequest.setPageSize(10);
roleRequest.setProjectId("default-project-2");
roleRequest.setKeyword("default-pro-role-5");
MvcResult mvcResult = this.responsePost(PROJECT_USER_ROLE_LIST, roleRequest);
// 获取返回值
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
// 返回总条数是否为init_project_user_role.sql中的数据总数
Assertions.assertFalse(JSON.parseArray(JSON.toJSONString(resultHolder.getData())).isEmpty());
Pager<?> pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
// 返回值不为空, 取第一条是否包含关键字
Assertions.assertNotNull(pageData);
ProjectUserRoleDTO roleDTO = JSON.parseArray(JSON.toJSONString(pageData.getList()), ProjectUserRoleDTO.class).get(0);
Assertions.assertTrue(StringUtils.contains(roleDTO.getName(), roleRequest.getKeyword())
|| StringUtils.contains(roleDTO.getId(), roleRequest.getKeyword()));
// 权限校验
requestPostPermissionTest(PermissionConstants.PROJECT_GROUP_ADD, PROJECT_USER_ROLE_ADD, request);
}
@ -139,16 +174,23 @@ public class ProjectUserRoleControllerTests extends BaseTest {
request.setScopeId("default-project-2");
this.requestPost(PROJECT_USER_ROLE_UPDATE, request, status().isOk());
// 验证是否修改成功
String projectId = "default-project-2";
MvcResult mvcResult = this.responseGet(PROJECT_USER_ROLE_LIST + "/" + projectId);
ProjectUserRoleRequest roleRequest = new ProjectUserRoleRequest();
roleRequest.setCurrent(1);
roleRequest.setPageSize(10);
roleRequest.setProjectId("default-project-2");
roleRequest.setKeyword("default-pro-role-x");
MvcResult mvcResult = this.responsePost(PROJECT_USER_ROLE_LIST, roleRequest);
// 获取返回值
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
// 返回总条数是否包含修改后的数据
List<UserRole> userRoles = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), UserRole.class);
Assertions.assertTrue(userRoles.stream().anyMatch(userRole -> "default-pro-role-x".equals(userRole.getName())));
Pager<?> pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
// 返回值不为空
Assertions.assertNotNull(pageData);
ProjectUserRoleDTO roleDTO = JSON.parseArray(JSON.toJSONString(pageData.getList()), ProjectUserRoleDTO.class).get(0);
Assertions.assertTrue(StringUtils.contains(roleDTO.getName(), roleRequest.getKeyword())
|| StringUtils.contains(roleDTO.getId(), roleRequest.getKeyword()));
// 权限校验
requestPostPermissionTest(PermissionConstants.PROJECT_GROUP_UPDATE, PROJECT_USER_ROLE_UPDATE, request);
}
@ -256,7 +298,7 @@ public class ProjectUserRoleControllerTests extends BaseTest {
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= request.getPageSize());
// 返回值中取出第一条数据, 并判断是否包含关键字
List<User> userList = JSON.parseArray(JSON.toJSONString(pageData.getList()), User.class);
if(CollectionUtils.isNotEmpty(userList)) {
if (CollectionUtils.isNotEmpty(userList)) {
User user = userList.get(0);
Assertions.assertTrue(StringUtils.contains(user.getName(), request.getKeyword())
|| StringUtils.contains(user.getId(), request.getKeyword()));
@ -376,9 +418,12 @@ public class ProjectUserRoleControllerTests extends BaseTest {
this.requestGet(PROJECT_USER_ROLE_DELETE + "/default-pro-role-id-3", status().isOk());
}
private PermissionSettingUpdateRequest getPermissionSettingUpdateRequest(){
private PermissionSettingUpdateRequest getPermissionSettingUpdateRequest() {
PermissionSettingUpdateRequest request = new PermissionSettingUpdateRequest();
request.setPermissions(new ArrayList<>() {
@Serial
private static final long serialVersionUID = -1719021806631967745L;
{
// 取消PROJECT_GROUP:READ权限
add(new PermissionSettingUpdateRequest.PermissionUpdateRequest("PROJECT_GROUP:READ", false));

View File

@ -1,19 +1,36 @@
# 项目用户组数据准备
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUE
('default-project-1', null, 'default-organization-1', '默认项目-1', '系统默认创建的项目-1', '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) VALUE
('default-project-2', null, 'default-organization-2', '默认项目-2', '系统默认创建的项目-2', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);
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
('default-pro-admin-user', 'default-pro-admin-1', 'admin-default-pro-user@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin');
INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUES
('default-pro-role-id-1', 'default-pro-role-1', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2'),
('default-pro-role-id-2', 'default-pro-role-2', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2'),
('default-pro-role-id-3', 'default-pro-role-3', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2'),
('default-pro-role-id-4', 'default-pro-role-4', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2');
INSERT INTO user_role_permission (id, role_id, permission_id) VALUE
(uuid(), 'default-org-role-id-3', 'PROJECT_USER_GROUP:READ');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUES
(UUID(), 'default-pro-admin-user', 'default-pro-role-id-3', 'default-project-2', 'default-project-2', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-pro-admin-user', 'default-pro-role-id-4', 'default-project-2', 'default-project-2', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-admin-user-x', 'default-pro-role-id-4', 'default-project-2', 'default-project-2', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-admin-user-x', 'default-pro-role-id-3', 'default-organization-4', 'default-project-4', UNIX_TIMESTAMP() * 1000, 'admin');
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time,
update_time) VALUE
('default-project-1', null, 'default-organization-1', '默认项目-1', '系统默认创建的项目-1', '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) VALUE
('default-project-2', null, 'default-organization-2', '默认项目-2', '系统默认创建的项目-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) VALUE
('default-project-3', null, 'default-organization-2', '默认项目-2', '系统默认创建的项目-2', 'admin', 'admin',
UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);
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
('default-pro-admin-user', 'default-pro-admin-1', 'admin-default-pro-user@metersphere.io', MD5('metersphere'),
UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin');
INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id)
VALUES ('default-pro-role-id-1', 'default-pro-role-1', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000,
UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2'),
('default-pro-role-id-2', 'default-pro-role-2', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000,
UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2'),
('default-pro-role-id-3', 'default-pro-role-3', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000,
UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2'),
('default-pro-role-id-4', 'default-pro-role-4', 'XXX', FALSE, 'PROJECT', UNIX_TIMESTAMP() * 1000,
UNIX_TIMESTAMP() * 1000, 'admin', 'default-project-2');
INSERT INTO user_role_permission (id, role_id, permission_id) VALUE (uuid(), 'default-org-role-id-3', 'PROJECT_USER_GROUP:READ');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user)
VALUES (UUID(), 'default-pro-admin-user', 'default-pro-role-id-3', 'default-project-2', 'default-project-2',
UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-pro-admin-user', 'default-pro-role-id-4', 'default-project-2', 'default-project-2',
UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-admin-user-x', 'default-pro-role-id-4', 'default-project-2', 'default-project-2',
UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'default-admin-user-x', 'default-pro-role-id-3', 'default-organization-4', 'default-project-4',
UNIX_TIMESTAMP() * 1000, 'admin');