fix(工作台): 修复未按照列表顺序获取默认权限项目问题

--bug=1050600 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001050600
This commit is contained in:
guoyuqi 2024-12-20 15:20:59 +08:00 committed by Craftsman
parent 89857c2fff
commit e9bef3c856
6 changed files with 66 additions and 43 deletions

View File

@ -34,5 +34,7 @@ public class LayoutDTO implements Serializable {
private List<String> handleUsers;
@Schema(description = "测试计划ID")
private String planId;
@Schema(description = "测试计划组ID")
private String groupId;
}

View File

@ -36,17 +36,17 @@ public class DashboardProjectService {
boolean isAdmin = isAdmin(userId);
Set<String> projectSet;
Map<String, String> moduleMap;
projectSet = projects.stream().map(Project::getId).collect(Collectors.toSet());
projectSet = projects.stream() .map(Project::getId).collect(Collectors.toCollection(LinkedHashSet::new));
moduleMap = projects.stream().collect(Collectors.toMap(Project::getId, Project::getModuleSetting));
Map<String, Set<String>> hasModuleProjectIds = new HashMap<>();
Map<String, String> finalModuleMap = moduleMap;
Set<String> searchCaseProjectIds = new HashSet<>();
Set<String> searchReviewProjectIds = new HashSet<>();
Set<String> searchApiProjectIds = new HashSet<>();
Set<String> searchApiCaseProjectIds = new HashSet<>();
Set<String> searchScenarioProjectIds = new HashSet<>();
Set<String> searchPlanProjectIds = new HashSet<>();
Set<String> searchBugProjectIds = new HashSet<>();
Set<String> searchCaseProjectIds = new LinkedHashSet<>();
Set<String> searchReviewProjectIds = new LinkedHashSet<>();
Set<String> searchApiProjectIds = new LinkedHashSet<>();
Set<String> searchApiCaseProjectIds = new LinkedHashSet<>();
Set<String> searchScenarioProjectIds = new LinkedHashSet<>();
Set<String> searchPlanProjectIds = new LinkedHashSet<>();
Set<String> searchBugProjectIds = new LinkedHashSet<>();
//查出用户在选中的项目中有读取权限的, admin所有项目都有权限
if (!isAdmin) {
Set<String> permissionSet = getPermissionSet();
@ -56,44 +56,44 @@ public class DashboardProjectService {
//检查是否开启功能用例模块
if (CollectionUtils.isNotEmpty(functionalProjectIds)) {
//有权限
searchCaseProjectIds = functionalProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toSet());
searchCaseProjectIds = functionalProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toCollection(LinkedHashSet::new));
}
Set<String> reviewProjectIds = hasUserPermissionProjectIds.get(PermissionConstants.CASE_REVIEW_READ);
if (CollectionUtils.isNotEmpty(reviewProjectIds)) {
searchReviewProjectIds = reviewProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toSet());
searchReviewProjectIds = reviewProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toCollection(LinkedHashSet::new));
}
//检查是否开启接口模块
Set<String> apiProjectIds = hasUserPermissionProjectIds.get(PermissionConstants.PROJECT_API_DEFINITION_READ);
if (CollectionUtils.isNotEmpty(apiProjectIds)) {
searchApiProjectIds = apiProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toSet());
searchApiProjectIds = apiProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toCollection(LinkedHashSet::new));
}
Set<String> apiCaseProjectIds = hasUserPermissionProjectIds.get(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ);
if (CollectionUtils.isNotEmpty(apiCaseProjectIds)) {
searchApiCaseProjectIds = apiCaseProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toSet());
searchApiCaseProjectIds = apiCaseProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toCollection(LinkedHashSet::new));
}
Set<String> scenarioProjectIds = hasUserPermissionProjectIds.get(PermissionConstants.PROJECT_API_SCENARIO_READ);
if (CollectionUtils.isNotEmpty(scenarioProjectIds)) {
searchScenarioProjectIds = scenarioProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toSet());
searchScenarioProjectIds = scenarioProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toCollection(LinkedHashSet::new));
}
//检查是否开启测试计划模块
Set<String> planProjectIds = hasUserPermissionProjectIds.get(PermissionConstants.TEST_PLAN_READ);
if (CollectionUtils.isNotEmpty(planProjectIds)) {
searchPlanProjectIds = planProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(TEST_PLAN)).collect(Collectors.toSet());
searchPlanProjectIds = planProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(TEST_PLAN)).collect(Collectors.toCollection(LinkedHashSet::new));
}
//检查是否开启缺陷模块
Set<String> bugProjectIds = hasUserPermissionProjectIds.get(PermissionConstants.PROJECT_BUG_READ);
if (CollectionUtils.isNotEmpty(bugProjectIds)) {
searchBugProjectIds = bugProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(BUG)).collect(Collectors.toSet());
searchBugProjectIds = bugProjectIds.stream().filter(t -> finalModuleMap.get(t).contains(BUG)).collect(Collectors.toCollection(LinkedHashSet::new));
}
} else {
//查出这些项目分别有模块的
searchCaseProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toSet());
searchReviewProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toSet());
searchApiProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toSet());
searchApiCaseProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toSet());
searchScenarioProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toSet());
searchPlanProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(TEST_PLAN)).collect(Collectors.toSet());
searchBugProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(BUG)).collect(Collectors.toSet());
searchCaseProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toCollection(LinkedHashSet::new));
searchReviewProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toCollection(LinkedHashSet::new));
searchApiProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toCollection(LinkedHashSet::new));
searchApiCaseProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toCollection(LinkedHashSet::new));
searchScenarioProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(API_TEST)).collect(Collectors.toCollection(LinkedHashSet::new));
searchPlanProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(TEST_PLAN)).collect(Collectors.toCollection(LinkedHashSet::new));
searchBugProjectIds = projectSet.stream().filter(t -> finalModuleMap.get(t).contains(BUG)).collect(Collectors.toCollection(LinkedHashSet::new));
}
//如果value 为空则没有权限或者没开启模块
hasModuleProjectIds.put(PermissionConstants.FUNCTIONAL_CASE_READ, searchCaseProjectIds);
@ -115,11 +115,11 @@ public class DashboardProjectService {
*/
public Map<String, Set<String>> getModuleProjectIds(List<Project> userProject) {
Map<String, String> moduleMap = userProject.stream().collect(Collectors.toMap(Project::getId, Project::getModuleSetting));
Set<String> projectIds = userProject.stream().map(Project::getId).collect(Collectors.toSet());
Set<String> searchCaseProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toSet());
Set<String> searchApiProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(API_TEST)).collect(Collectors.toSet());
Set<String> searchPlanProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(TEST_PLAN)).collect(Collectors.toSet());
Set<String> searchBugProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(BUG)).collect(Collectors.toSet());
Set<String> projectIds = userProject.stream().map(Project::getId).collect(Collectors.toCollection(LinkedHashSet::new));
Set<String> searchCaseProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(FUNCTIONAL_CASE)).collect(Collectors.toCollection(LinkedHashSet::new));
Set<String> searchApiProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(API_TEST)).collect(Collectors.toCollection(LinkedHashSet::new));
Set<String> searchPlanProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(TEST_PLAN)).collect(Collectors.toCollection(LinkedHashSet::new));
Set<String> searchBugProjectIds = projectIds.stream().filter(t -> moduleMap.get(t).contains(BUG)).collect(Collectors.toCollection(LinkedHashSet::new));
Map<String, Set<String>> hasModuleProjectIds = new HashMap<>();
hasModuleProjectIds.put(PermissionConstants.FUNCTIONAL_CASE_READ, searchCaseProjectIds);
hasModuleProjectIds.put(PermissionConstants.CASE_REVIEW_READ, searchCaseProjectIds);

View File

@ -495,6 +495,12 @@ public class DashboardService {
checkHasPermissionProject(layoutDTO, hasReadProjectIds);
if (StringUtils.equalsIgnoreCase(layoutDTO.getKey(), DashboardUserLayoutKeys.PROJECT_PLAN_VIEW.toString())) {
setPlanId(layoutDTO);
if (StringUtils.isBlank(layoutDTO.getPlanId())) {
TestPlan latestPlanByProjectIds = extTestPlanMapper.getLatestPlanByProjectIds(hasReadProjectIds);
layoutDTO.setPlanId(latestPlanByProjectIds.getId());
layoutDTO.setGroupId(latestPlanByProjectIds.getGroupId());
layoutDTO.setProjectIds(List.of(latestPlanByProjectIds.getProjectId()));
}
}
} else if (StringUtils.equalsIgnoreCase(layoutDTO.getKey(), DashboardUserLayoutKeys.BUG_COUNT.toString())
|| StringUtils.equalsIgnoreCase(layoutDTO.getKey(), DashboardUserLayoutKeys.CREATE_BUG_BY_ME.toString())
@ -516,6 +522,9 @@ public class DashboardService {
TestPlan latestPlan = extTestPlanMapper.getLatestPlan(layoutDTO.getProjectIds().getFirst());
if (latestPlan != null) {
layoutDTO.setPlanId(latestPlan.getId());
layoutDTO.setGroupId(layoutDTO.getGroupId());
} else {
layoutDTO.setPlanId(null);
}
}
}

View File

@ -9,6 +9,7 @@ import io.metersphere.sdk.util.JSON;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.domain.UserRolePermission;
import io.metersphere.system.dto.user.UserDTO;
import io.metersphere.system.dto.user.UserRoleResourceDTO;
import io.metersphere.system.service.UserLoginService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
@ -101,28 +102,26 @@ public class PermissionCheckService {
UserDTO user = getUserDTO(userId);
if (user == null) return new HashMap<>();
// 注意超级管理员包含所有权限这里不予返回请在方法外自行判断
Map<String, Set<String>> permissionProjectIdMap = new HashMap<>();
Map<String, List<UserRolePermission>> projectPermissionMap = new HashMap<>();
Map<String, List<UserRolePermission>>rolePermissionMap = new HashMap<>();
Map<String, Set<String>> permissionProjectIdMap = new LinkedHashMap<>();
Map<String, List<UserRolePermission>> projectPermissionMap = new LinkedHashMap<>();
Map<String, List<UserRolePermission>>rolePermissionMap = user.getUserRolePermissions().stream().filter(t->StringUtils.equalsIgnoreCase(t.getUserRole().getType(), UserRoleType.PROJECT.name())).collect(Collectors.toMap(f->f.getUserRole().getId(), UserRoleResourceDTO::getUserRolePermissions));
user.getUserRolePermissions().forEach(t->{
if (StringUtils.equalsIgnoreCase(t.getUserRole().getType(), UserRoleType.PROJECT.name())) {
rolePermissionMap.put(t.getUserRole().getId(),t.getUserRolePermissions());
}
});
user.getUserRoleRelations().forEach(ug -> {
List<UserRolePermission> userRolePermissions = rolePermissionMap.get(ug.getRoleId());
if (CollectionUtils.isNotEmpty(userRolePermissions) && projectIds.contains(ug.getSourceId())) {
projectPermissionMap.put(ug.getSourceId(),userRolePermissions);
}
});
projectPermissionMap.forEach((projectId, userRolePermissions)->{
for (String projectId : projectIds) {
List<UserRolePermission> userRolePermissions = projectPermissionMap.get(projectId);
for (UserRolePermission userRolePermission : userRolePermissions) {
if (permissions.contains(userRolePermission.getPermissionId())) {
permissionProjectIdMap.computeIfAbsent(userRolePermission.getPermissionId(), key -> new HashSet<>()).add(projectId);
permissionProjectIdMap.computeIfAbsent(userRolePermission.getPermissionId(), key -> new LinkedHashSet<>()).add(projectId);
}
}
});
}
return permissionProjectIdMap;
}

View File

@ -71,7 +71,7 @@ public interface ExtTestPlanMapper {
List<String> selectRightfulIdsForExecute(@Param("ids") List<String> ids);
List<TestPlanExecuteHisDTO> listHis(@Param("request")TestPlanExecuteHisPageRequest request);
List<TestPlanExecuteHisDTO> listHis(@Param("request") TestPlanExecuteHisPageRequest request);
List<String> selectGroupIdByKeyword(@Param("projectId") String projectId, @Param("keyword") String keyword);
@ -90,9 +90,10 @@ public interface ExtTestPlanMapper {
/**
* 获取项目下的计划关联缺陷
* @param projectId 项目
* @param type 计划类型
* @param platform 缺陷平台集合
*
* @param projectId 项目
* @param type 计划类型
* @param platform 缺陷平台集合
* @param statusList 缺陷状态
* @return List<SelectOption>
*/
@ -102,12 +103,14 @@ public interface ExtTestPlanMapper {
/**
* @param projectId 项目
* 获取项目下计划组和计划的名称
* 获取项目下计划组和计划的名称
*/
List<TestPlanAndGroupInfoDTO> getGroupAndPlanInfo(@Param("projectId") String projectId);
TestPlan getLatestPlan(@Param("projectId") String projectId);
TestPlan getLatestPlanByProjectIds(@Param("projectIds") Set<String> projectIds);
List<TestPlanConfig> selectTestPlanConfigByTestPlanIds(@Param("testPlanIds") List<String> testPlanIds);

View File

@ -929,6 +929,16 @@
where type = 'TEST_PLAN' and status='NOT_ARCHIVED' and project_id = #{projectId} order by create_time desc limit 1;
</select>
<select id="getLatestPlanByProjectIds" resultType="io.metersphere.plan.domain.TestPlan">
select id, name, group_id, project_id
from test_plan
where type = 'TEST_PLAN' and status='NOT_ARCHIVED' and project_id IN
<foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
#{projectId}
</foreach>
order by create_time desc limit 1;
</select>
<select id="selectTestPlanConfigByTestPlanIds" resultType="io.metersphere.plan.domain.TestPlanConfig">
SELECT *
from test_plan_config test_plan_id