refactor(系统设置): 组织项目增加查询用户的接口

This commit is contained in:
wxg0103 2023-08-28 11:58:28 +08:00 committed by wxg0103
parent ecd7fcb2e7
commit 8d94d8547c
8 changed files with 184 additions and 24 deletions

View File

@ -14,7 +14,6 @@ import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.system.request.OrganizationProjectRequest;
import io.metersphere.system.request.ProjectAddMemberBatchRequest;
@ -146,11 +145,18 @@ public class OrganizationProjectController {
return organizationProjectService.removeProjectMember(projectId, userId, SessionUtils.getUserId());
}
@GetMapping("/user-list")
@Operation(summary = "系统设置-组织-项目-获取用户列表")
@GetMapping("/user-admin-list/{organizationId}/{projectId}")
@Operation(summary = "系统设置-组织-项目-获取管理员列表")
@RequiresPermissions(PermissionConstants.ORGANIZATION_PROJECT_READ)
public List<User> getUserList() {
return userService.getUserList();
public List<UserExtend> getUserAdminList(@PathVariable String organizationId, @PathVariable String projectId) {
return organizationProjectService.getUserAdminList(organizationId, projectId);
}
@GetMapping("/user-member-list/{organizationId}/{projectId}")
@Operation(summary = "系统设置-组织-项目-获取成员列表")
@RequiresPermissions(PermissionConstants.ORGANIZATION_PROJECT_READ)
public List<UserExtend> getUserMemberList(@PathVariable String organizationId, @PathVariable String projectId) {
return organizationProjectService.getUserMemberList(organizationId, projectId);
}
}

View File

@ -19,4 +19,8 @@ public interface ExtSystemProjectMapper {
List<User> getProjectAdminList(String projectId);
List<OrganizationProjectOptionsDTO> selectProjectOptions(@Param("organizationId") String organizationId);
List<UserExtend> getUserAdminList(@Param("userIds") List<String> userIds, @Param("projectId") String projectId);
List<UserExtend> getUserMemberList(@Param("userIds") List<String> userIds, @Param("projectId") String projectId);
}

View File

@ -10,8 +10,9 @@
FROM user_role_relation
LEFT JOIN `user` ON user_role_relation.user_id = `user`.id
<where>
`user`.deleted = 0
<if test="request.projectId != null">
user_role_relation.source_id = #{request.projectId}
and user_role_relation.source_id = #{request.projectId}
</if>
<if test="request.keyword != null">
and (user.name like CONCAT('%', #{request.keyword},'%')
@ -19,7 +20,7 @@
or user.phone like CONCAT('%', #{request.keyword},'%'))
</if>
</where>
ORDER BY `user`.update_time DESC) temp
ORDER BY `user`.update_time DESC) temp GROUP BY temp.id
</select>
<select id="getProjectList" resultType="io.metersphere.sdk.dto.ProjectDTO">
select p.id,
@ -40,17 +41,18 @@
p.module_setting
FROM project p
LEFT JOIN user_role_relation ur on p.id = ur.source_id
left join user u on u.id = ur.user_id and u.deleted = 0
left join user u on u.id = p.create_user
INNER JOIN organization o on p.organization_id = o.id
<include refid="queryWhereCondition"/>
group by p.id
</select>
<select id="getProjectAdminList" resultType="io.metersphere.system.domain.User">
<select id="getProjectAdminList" resultType="io.metersphere.system.dto.UserExtend">
SELECT `user`.*
FROM user_role_relation
LEFT JOIN `user` ON user_role_relation.user_id = `user`.id and `user`.deleted = 0
LEFT JOIN `user` ON user_role_relation.user_id = `user`.id
<where>
user_role_relation.source_id = #{projectId}
`user`.deleted = 0
and user_role_relation.source_id = #{projectId}
and user_role_relation.role_id = 'project_admin'
</where>
</select>
@ -69,8 +71,9 @@
<sql id="queryWhereCondition">
<where>
u.deleted = 0
<if test="request.organizationId != null">
p.organization_id = #{request.organizationId}
and p.organization_id = #{request.organizationId}
</if>
<if test="request.keyword != null">
and (
@ -110,4 +113,43 @@
</if>
</if>
</sql>
<select id="getUserAdminList" resultType="io.metersphere.system.dto.UserExtend">
select distinct u.*, MAX( if (temp.role_id = 'project_admin', true, false)) as adminFlag from
user u left join (select * from user_role_relation urr where urr.source_id = #{projectId}) temp on temp.user_id = u.id
<where>
u.deleted = 0
<if test="userIds != null and userIds.size > 0 ">
and u.id in
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
</if>
</where>
group by u.id
</select>
<select id="getUserMemberList" resultType="io.metersphere.system.dto.UserExtend">
select distinct u.*, count(temp.id) > 0 as memberFlag from
user u left join (select * from user_role_relation urr where urr.source_id = #{projectId}) temp on temp.user_id = u.id
<where>
u.deleted = 0
<if test="userIds != null and userIds.size > 0 ">
and u.id in
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
</if>
</where>
group by u.id
</select>
</mapper>

View File

@ -4,22 +4,29 @@ import io.metersphere.sdk.dto.AddProjectRequest;
import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.sdk.dto.ProjectExtendDTO;
import io.metersphere.sdk.dto.UpdateProjectRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.system.mapper.ExtSystemProjectMapper;
import io.metersphere.system.mapper.OrganizationMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.request.OrganizationProjectRequest;
import io.metersphere.system.request.ProjectAddMemberBatchRequest;
import io.metersphere.system.request.ProjectMemberRequest;
import io.metersphere.system.request.ProjectRequest;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
@ -29,6 +36,10 @@ public class OrganizationProjectService {
private ExtSystemProjectMapper extSystemProjectMapper;
@Resource
private CommonProjectService commonProjectService;
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
@Resource
private OrganizationMapper organizationMapper;
private final static String PREFIX = "/organization-project";
@ -65,7 +76,6 @@ public class OrganizationProjectService {
}
public List<UserExtend> getProjectMember(ProjectMemberRequest request) {
commonProjectService.checkProjectNotExist(request.getProjectId());
return extSystemProjectMapper.getProjectMemberList(request);
}
@ -94,4 +104,38 @@ public class OrganizationProjectService {
public void disable(String id) {
commonProjectService.disable(id);
}
public List<UserExtend> getUserAdminList(String organizationId, String projectId) {
checkOrgIsExist(organizationId);
commonProjectService.checkProjectNotExist(projectId);
UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria().andSourceIdEqualTo(organizationId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(example);
List<String> userIds = userRoleRelations.stream().map(UserRoleRelation::getUserId).distinct().collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(userIds)) {
return extSystemProjectMapper.getUserAdminList(userIds, projectId);
} else {
return null;
}
}
public List<UserExtend> getUserMemberList(String organizationId, String projectId) {
checkOrgIsExist(organizationId);
commonProjectService.checkProjectNotExist(projectId);
UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria().andSourceIdEqualTo(organizationId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(example);
List<String> userIds = userRoleRelations.stream().map(UserRoleRelation::getUserId).distinct().collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(userIds)) {
return extSystemProjectMapper.getUserMemberList(userIds, projectId);
} else {
return null;
}
}
public void checkOrgIsExist(String organizationId) {
if (organizationMapper.selectByPrimaryKey(organizationId) == null) {
throw new MSException(Translator.get("organization_not_exists"));
}
}
}

View File

@ -62,7 +62,6 @@ public class SystemProjectService {
}
public List<UserExtend> getProjectMember(ProjectMemberRequest request) {
commonProjectService.checkProjectNotExist(request.getProjectId());
return extSystemProjectMapper.getProjectMemberList(request);
}

View File

@ -8,10 +8,7 @@ import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.AddProjectRequest;
import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.sdk.dto.ProjectExtendDTO;
import io.metersphere.sdk.dto.UpdateProjectRequest;
import io.metersphere.sdk.dto.*;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
@ -68,7 +65,8 @@ public class OrganizationProjectControllerTests extends BaseTest {
private final static String removeProjectMember = prefix + "/remove-member/";
private final static String disableProject = prefix + "/disable/";
private final static String enableProject = prefix + "/enable/";
private final static String userList = prefix + "/user-list";
private final static String getAdminList = prefix + "/user-admin-list/";
private final static String getMemberList = prefix + "/user-member-list/";
private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest();
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
@ -591,6 +589,13 @@ public class OrganizationProjectControllerTests extends BaseTest {
Assertions.assertEquals(returnPager.getCurrent(), memberRequest.getCurrent());
//返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(((List<UserExtend>) returnPager.getList()).size() <= memberRequest.getPageSize());
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andSourceIdEqualTo(projectId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
//去重
List<String> userIds = userRoleRelations.stream().map(UserRoleRelation::getUserId).distinct().toList();
//返回的数据量和数据库中的数据量相同
Assertions.assertEquals(returnPager.getTotal(), userIds.size());
memberRequest.setSort(new HashMap<>() {{
put("createTime", "desc");
}});
@ -713,11 +718,62 @@ public class OrganizationProjectControllerTests extends BaseTest {
@Test
@Order(21)
public void testUserList() throws Exception {
this.requestGetWithOkAndReturn(userList);
public void testGetAdminList() throws Exception {
//组织下面有成员 返回不为空
String organizationId = getDefault().getId();
String projectId = "projectId4";
MvcResult mvcResult = responseGet(getAdminList + organizationId + "/" + projectId);
List<UserDTO> userDTOS = parseObjectFromMvcResult(mvcResult, List.class);
assert userDTOS != null;
Assertions.assertFalse(userDTOS.isEmpty());
// @@校验权限
requestGetPermissionTest(PermissionConstants.ORGANIZATION_PROJECT_READ, userList);
requestGetPermissionTest(PermissionConstants.ORGANIZATION_PROJECT_READ, getAdminList + organizationId + "/" + projectId);
//组织下面没有成员 返回为空
organizationId = "default-organization-20";
projectId = "projectId4";
mvcResult = responseGet(getAdminList + organizationId + "/" + projectId);
userDTOS = parseObjectFromMvcResult(mvcResult, List.class);
Assertions.assertNull(userDTOS);
//组织不存在
organizationId = "organizationId111";
projectId = "projectId4";
this.responseGet(getAdminList + organizationId + "/" + projectId, ERROR_REQUEST_MATCHER);
//项目不存在
organizationId = getDefault().getId();
projectId = "projectId111";
this.responseGet(getAdminList + organizationId + "/" + projectId, ERROR_REQUEST_MATCHER);
}
@Test
@Order(22)
public void testGetMemberList() throws Exception {
//组织下面有成员 返回不为空
String organizationId = getDefault().getId();
String projectId = "projectId4";
MvcResult mvcResult = responseGet(getMemberList + organizationId + "/" + projectId);
List<UserDTO> userDTOS = parseObjectFromMvcResult(mvcResult, List.class);
assert userDTOS != null;
Assertions.assertFalse(userDTOS.isEmpty());
// @@校验权限
requestGetPermissionTest(PermissionConstants.ORGANIZATION_PROJECT_READ, getMemberList + organizationId + "/" + projectId);
//组织下面没有成员 返回为空
organizationId = "default-organization-20";
projectId = "projectId4";
mvcResult = responseGet(getMemberList + organizationId + "/" + projectId);
userDTOS = parseObjectFromMvcResult(mvcResult, List.class);
Assertions.assertNull(userDTOS);
//组织不存在
organizationId = "organizationId111";
projectId = "projectId4";
this.responseGet(getMemberList + organizationId + "/" + projectId, ERROR_REQUEST_MATCHER);
//项目不存在
organizationId = getDefault().getId();
projectId = "projectId111";
this.responseGet(getMemberList + organizationId + "/" + projectId, ERROR_REQUEST_MATCHER);
}
}

View File

@ -590,7 +590,9 @@ public class SystemProjectControllerTests extends BaseTest {
memberRequest.setCurrent(1);
memberRequest.setPageSize(5);
memberRequest.setProjectId("projectId111");
this.requestPost(getProjectMemberList, memberRequest, ERROR_REQUEST_MATCHER);
MvcResult mvcResult = this.responsePost(getProjectMemberList, memberRequest);
Pager<?> returnPager = parseObjectFromMvcResult(mvcResult, Pager.class);
Assertions.assertEquals(0, returnPager.getTotal());
//排序字段不合法
memberRequest = new ProjectMemberRequest();
memberRequest.setCurrent(1);

View File

@ -22,3 +22,10 @@ replace INTO project (id, num, organization_id, name, description, create_user,
replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time, module_setting) VALUES ('projectId3', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目3', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000,'["apiTest","uiTest"]');
replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time,module_setting) VALUES ('projectId4', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目4', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000,'["apiTest","uiTest","loadTest"]');
replace INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time,module_setting) VALUES ('projectId5', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目5', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000,'["apiTest","uiTest"]');
# 插入测试数据 给组织增加成员
replace INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user )VALUES ('c3bb9b4f-46d8-4952-9681-1213333131','admin2','org_member',(SELECT id FROM organization WHERE name LIKE '默认组织'),'1684747668321','admin');
replace INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user )VALUES ('c3bb9b4f-46d8-4952-9681-3124121332','admin1','org_member',(SELECT id FROM organization WHERE name LIKE '默认组织'),'1684747668321','admin');
INSERT INTO organization(id,num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE
('default-organization-20',null, 'default-20', 'XXX-1', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null);