refactor(系统设置): 组织用户组功能
This commit is contained in:
parent
e3ea6b569e
commit
8e7072e67b
|
@ -1,8 +1,12 @@
|
|||
package io.metersphere.sdk.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class OptionDTO {
|
||||
private String id;
|
||||
private String name;
|
||||
|
|
|
@ -149,6 +149,7 @@ organization_user_role_permission_error=no organization user role permission
|
|||
user_role_exist=User role already exists
|
||||
user_role_not_exist=User role not exist
|
||||
user_role_not_edit=User role can not edit
|
||||
at_least_one_user_role_require=at least one user role require
|
||||
# plugin
|
||||
plugin.id.not_blank=id cannot be empty
|
||||
plugin.id.length_range=id length must be between {min} and {max}
|
||||
|
|
|
@ -148,6 +148,7 @@ organization_user_role_permission_error=没有权限操作非组织用户组
|
|||
user_role_exist=用户组已存在
|
||||
user_role_not_exist=用户组不存在
|
||||
user_role_not_edit=用户组无法编辑
|
||||
at_least_one_user_role_require=至少需要一个用户组
|
||||
# plugin
|
||||
plugin.id.not_blank=ID不能为空
|
||||
plugin.id.length_range=ID长度必须在{min}和{max}之间
|
||||
|
|
|
@ -148,6 +148,7 @@ organization_user_role_permission_error=沒有權限操作非組織用戶組
|
|||
user_role_exist=用戶組已存在
|
||||
user_role_not_exist=用戶組不存在
|
||||
user_role_not_edit=用戶組無法編輯
|
||||
at_least_one_user_role_require=至少需要一個用戶組
|
||||
# plugin
|
||||
plugin.id.not_blank=ID不能為空
|
||||
plugin.id.length_range=ID長度必須在{min}和{max}之间
|
||||
|
|
|
@ -75,7 +75,7 @@ public class OrganizationUserRoleController {
|
|||
@Parameter(name = "id", description = "用户组ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
|
||||
@Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = OrganizationUserRoleLogService.class)
|
||||
public void delete(@PathVariable String id) {
|
||||
organizationUserRoleService.delete(id);
|
||||
organizationUserRoleService.delete(id, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@GetMapping("/permission/setting/{id}")
|
||||
|
|
|
@ -3,6 +3,7 @@ package io.metersphere.system.controller;
|
|||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import io.metersphere.sdk.constants.PermissionConstants;
|
||||
import io.metersphere.sdk.dto.OptionDTO;
|
||||
import io.metersphere.sdk.dto.ProjectDTO;
|
||||
import io.metersphere.sdk.util.PageUtils;
|
||||
import io.metersphere.sdk.util.Pager;
|
||||
|
@ -49,10 +50,10 @@ public class SystemOrganizationController {
|
|||
return PageUtils.setPageInfo(page, organizationService.list(request));
|
||||
}
|
||||
|
||||
@PostMapping("/list-all")
|
||||
@Operation(summary = "获取系统所有组织")
|
||||
@PostMapping("/option/all")
|
||||
@Operation(summary = "获取系统所有组织下拉选项")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ)
|
||||
public List<OrganizationDTO> listAll() {
|
||||
public List<OptionDTO> listAll() {
|
||||
return organizationService.listAll();
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import io.metersphere.sdk.constants.InternalUserRole;
|
|||
import io.metersphere.sdk.constants.UserRoleEnum;
|
||||
import io.metersphere.sdk.constants.UserRoleType;
|
||||
import io.metersphere.sdk.dto.LogDTO;
|
||||
import io.metersphere.sdk.dto.OptionDTO;
|
||||
import io.metersphere.sdk.exception.MSException;
|
||||
import io.metersphere.sdk.log.constants.OperationLogModule;
|
||||
import io.metersphere.sdk.log.constants.OperationLogType;
|
||||
|
@ -16,8 +17,8 @@ import io.metersphere.sdk.util.BeanUtils;
|
|||
import io.metersphere.sdk.util.JSON;
|
||||
import io.metersphere.sdk.util.Translator;
|
||||
import io.metersphere.system.domain.*;
|
||||
import io.metersphere.system.dto.*;
|
||||
import io.metersphere.system.dto.UserExtend;
|
||||
import io.metersphere.system.dto.*;
|
||||
import io.metersphere.system.mapper.*;
|
||||
import io.metersphere.system.request.*;
|
||||
import jakarta.annotation.Resource;
|
||||
|
@ -61,47 +62,60 @@ public class OrganizationService {
|
|||
@Resource
|
||||
private ProjectMapper projectMapper;
|
||||
|
||||
private static final String ADD_MEMBER_PATH = "/system/organization/add-member";
|
||||
private static final String REMOVE_MEMBER_PATH = "/system/organization/remove-member";
|
||||
|
||||
/**
|
||||
* 分页获取系统下组织列表
|
||||
* @param organizationRequest 请求参数
|
||||
* @return 组织集合
|
||||
*/
|
||||
public List<OrganizationDTO> list(OrganizationRequest organizationRequest) {
|
||||
List<OrganizationDTO> organizationDTOS = extOrganizationMapper.list(organizationRequest);
|
||||
return buildOrgAdminInfo(organizationDTOS);
|
||||
}
|
||||
|
||||
private List<OrganizationDTO> buildOrgAdminInfo(List<OrganizationDTO> organizationDTOS) {
|
||||
if (CollectionUtils.isEmpty(organizationDTOS)) {
|
||||
return organizationDTOS;
|
||||
}
|
||||
organizationDTOS.forEach(organizationDTO -> {
|
||||
List<User> orgAdminList = extOrganizationMapper.getOrgAdminList(organizationDTO.getId());
|
||||
organizationDTO.setOrgAdmins(orgAdminList);
|
||||
});
|
||||
return organizationDTOS;
|
||||
}
|
||||
|
||||
public List<OrganizationDTO> listAll() {
|
||||
return extOrganizationMapper.listAll();
|
||||
}
|
||||
|
||||
public OrganizationDTO getDefault() {
|
||||
OrganizationDTO organizationDTO = new OrganizationDTO();
|
||||
OrganizationExample example = new OrganizationExample();
|
||||
example.createCriteria().andNumEqualTo(100001L);
|
||||
List<Organization> organizations = organizationMapper.selectByExample(example);
|
||||
Organization organization = organizations.get(0);
|
||||
BeanUtils.copyBean(organizationDTO, organization);
|
||||
return organizationDTO;
|
||||
/**
|
||||
* 获取系统下组织下拉选项
|
||||
* @return 组织下拉选项集合
|
||||
*/
|
||||
public List<OptionDTO> listAll() {
|
||||
List<OrganizationDTO> organizations = extOrganizationMapper.listAll();
|
||||
return organizations.stream().map(o -> new OptionDTO(o.getId(), o.getName())).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取组织成员列表
|
||||
* @param request 请求参数
|
||||
* @return 组织成员集合
|
||||
*/
|
||||
public List<UserExtend> getMemberListBySystem(OrganizationRequest request) {
|
||||
return extOrganizationMapper.listMember(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统-组织-添加成员
|
||||
* @param organizationMemberRequest 请求参数
|
||||
* @param createUserId 创建人ID
|
||||
*/
|
||||
public void addMemberBySystem(OrganizationMemberRequest organizationMemberRequest, String createUserId) {
|
||||
List<LogDTO> logs = new ArrayList<>();
|
||||
OrganizationMemberBatchRequest batchRequest = new OrganizationMemberBatchRequest();
|
||||
batchRequest.setOrganizationIds(List.of(organizationMemberRequest.getOrganizationId()));
|
||||
batchRequest.setMemberIds(organizationMemberRequest.getMemberIds());
|
||||
addMemberBySystem(batchRequest, createUserId);
|
||||
//添加日志
|
||||
Organization organization = organizationMapper.selectByPrimaryKey(organizationMemberRequest.getOrganizationId());
|
||||
setLog(organizationMemberRequest.getOrganizationId(), createUserId, OperationLogType.ADD.name(), organization.getName(),
|
||||
ADD_MEMBER_PATH, null, batchRequest.getMemberIds(), logs);
|
||||
operationLogService.batchAdd(logs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 组织添加成员公共方法(N个组织添加N个成员)
|
||||
* @param batchRequest 请求参数 [organizationIds 组织集合, memberIds 成员集合]
|
||||
* @param createUserId 创建人ID
|
||||
*/
|
||||
public void addMemberBySystem(OrganizationMemberBatchRequest batchRequest, String createUserId) {
|
||||
checkOrgExistByIds(batchRequest.getOrganizationIds());
|
||||
Map<String, User> userMap = checkUserExist(batchRequest.getMemberIds());
|
||||
|
@ -109,7 +123,7 @@ public class OrganizationService {
|
|||
batchRequest.getOrganizationIds().forEach(organizationId -> {
|
||||
for (String userId : batchRequest.getMemberIds()) {
|
||||
if (userMap.get(userId) == null) {
|
||||
throw new MSException("id:" + userId + Translator.get("user.not.exist"));
|
||||
throw new MSException(Translator.get("user.not.exist") + ", id: " + userId);
|
||||
}
|
||||
//组织用户成员关系已存在, 不再重复添加
|
||||
UserRoleRelationExample example = new UserRoleRelationExample();
|
||||
|
@ -132,12 +146,49 @@ public class OrganizationService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除组织成员
|
||||
* @param organizationId 组织ID
|
||||
* @param userId 成员ID
|
||||
*/
|
||||
public void removeMember(String organizationId, String userId) {
|
||||
List<LogDTO> logs = new ArrayList<>();
|
||||
checkOrgExistById(organizationId);
|
||||
//删除组织下项目与成员的关系
|
||||
List<String> projectIds = getProjectIds(organizationId);
|
||||
if (CollectionUtils.isNotEmpty(projectIds)) {
|
||||
UserRoleRelationExample example = new UserRoleRelationExample();
|
||||
example.createCriteria().andUserIdEqualTo(userId).andSourceIdIn(projectIds);
|
||||
userRoleRelationMapper.deleteByExample(example);
|
||||
}
|
||||
//删除组织与成员的关系
|
||||
UserRoleRelationExample example = new UserRoleRelationExample();
|
||||
example.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(organizationId);
|
||||
userRoleRelationMapper.deleteByExample(example);
|
||||
// 操作记录
|
||||
Organization organization = organizationMapper.selectByPrimaryKey(organizationId);
|
||||
setLog(organizationId, userId, OperationLogType.DELETE.name(), organization.getName(), REMOVE_MEMBER_PATH, userId, null, logs);
|
||||
operationLogService.batchAdd(logs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统默认组织
|
||||
* @return 组织信息
|
||||
*/
|
||||
public OrganizationDTO getDefault() {
|
||||
OrganizationDTO organizationDTO = new OrganizationDTO();
|
||||
OrganizationExample example = new OrganizationExample();
|
||||
example.createCriteria().andNumEqualTo(100001L);
|
||||
List<Organization> organizations = organizationMapper.selectByExample(example);
|
||||
Organization organization = organizations.get(0);
|
||||
BeanUtils.copyBean(organizationDTO, organization);
|
||||
return organizationDTO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组织级别获取组织成员
|
||||
*
|
||||
* @param organizationRequest
|
||||
* @return
|
||||
* @param organizationRequest 请求参数
|
||||
* @return 组织成员集合
|
||||
*/
|
||||
public List<OrgUserExtend> getMemberListByOrg(OrganizationRequest organizationRequest) {
|
||||
//根据组织ID获取所有组织用户关系表
|
||||
|
@ -201,27 +252,6 @@ public class OrganizationService {
|
|||
return orgUserExtends;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除组织成员
|
||||
*
|
||||
* @param organizationId
|
||||
* @param userId
|
||||
*/
|
||||
public void removeMember(String organizationId, String userId) {
|
||||
checkOrgExistById(organizationId);
|
||||
//删除组织下项目与成员的关系
|
||||
List<String> projectIds = getProjectIds(organizationId);
|
||||
if (CollectionUtils.isNotEmpty(projectIds)) {
|
||||
UserRoleRelationExample example = new UserRoleRelationExample();
|
||||
example.createCriteria().andUserIdEqualTo(userId).andSourceIdIn(projectIds);
|
||||
userRoleRelationMapper.deleteByExample(example);
|
||||
}
|
||||
//删除组织与成员的关系
|
||||
UserRoleRelationExample example = new UserRoleRelationExample();
|
||||
example.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(organizationId);
|
||||
userRoleRelationMapper.deleteByExample(example);
|
||||
}
|
||||
|
||||
public List<OrganizationProjectOptionsDTO> getOrganizationOptions() {
|
||||
return extOrganizationMapper.selectOrganizationOptions();
|
||||
}
|
||||
|
@ -283,8 +313,8 @@ public class OrganizationService {
|
|||
|
||||
/**
|
||||
* 添加组织成员至用户组
|
||||
* @param organizationMemberExtendRequest
|
||||
* @param userId
|
||||
* @param organizationMemberExtendRequest 请求参数
|
||||
* @param userId 创建人ID
|
||||
*/
|
||||
public void addMemberRole(OrganizationMemberExtendRequest organizationMemberExtendRequest, String userId) {
|
||||
String organizationId = organizationMemberExtendRequest.getOrganizationId();
|
||||
|
@ -389,9 +419,8 @@ public class OrganizationService {
|
|||
|
||||
/**
|
||||
* 更新用户
|
||||
*
|
||||
* @param organizationMemberUpdateRequest
|
||||
* @param createUserId
|
||||
* @param organizationMemberUpdateRequest 请求参数
|
||||
* @param createUserId 创建人ID
|
||||
*/
|
||||
public void updateMember(OrganizationMemberUpdateRequest organizationMemberUpdateRequest, String createUserId) {
|
||||
String organizationId = organizationMemberUpdateRequest.getOrganizationId();
|
||||
|
@ -474,9 +503,8 @@ public class OrganizationService {
|
|||
|
||||
/**
|
||||
* 获取当前组织下的所有项目
|
||||
*
|
||||
* @param organizationId
|
||||
* @return
|
||||
* @param organizationId 组织ID
|
||||
* @return 项目列表
|
||||
*/
|
||||
public List<IdNameStructureDTO> getProjectList(String organizationId) {
|
||||
//校验组织是否存在
|
||||
|
@ -496,9 +524,8 @@ public class OrganizationService {
|
|||
|
||||
/**
|
||||
* 获取当前组织下的所有自定义用户组以及组织级别的用户组
|
||||
*
|
||||
* @param organizationId
|
||||
* @return
|
||||
* @param organizationId 组织ID
|
||||
* @return 用户组列表
|
||||
*/
|
||||
public List<IdNameStructureDTO> getUserRoleList(String organizationId) {
|
||||
//校验组织是否存在
|
||||
|
@ -523,9 +550,8 @@ public class OrganizationService {
|
|||
|
||||
/**
|
||||
* 获取不在当前组织的所有用户
|
||||
*
|
||||
* @param organizationId
|
||||
* @return
|
||||
* @param organizationId 组织ID
|
||||
* @return 用户列表
|
||||
*/
|
||||
public List<IdNameStructureDTO> getUserList(String organizationId) {
|
||||
//校验组织是否存在
|
||||
|
@ -549,6 +575,10 @@ public class OrganizationService {
|
|||
return userList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查组织是否存在
|
||||
* @param organizationIds 组织ID集合
|
||||
*/
|
||||
private void checkOrgExistByIds(List<String> organizationIds) {
|
||||
OrganizationExample example = new OrganizationExample();
|
||||
example.createCriteria().andIdIn(organizationIds);
|
||||
|
@ -557,6 +587,10 @@ public class OrganizationService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查组织是否存在
|
||||
* @param organizationId 组织ID
|
||||
*/
|
||||
private void checkOrgExistById(String organizationId) {
|
||||
Organization organization = organizationMapper.selectByPrimaryKey(organizationId);
|
||||
if (organization == null) {
|
||||
|
@ -566,9 +600,9 @@ public class OrganizationService {
|
|||
|
||||
/**
|
||||
* 检查组织级别的用户组是否存在
|
||||
* @param userRoleIds
|
||||
* @param organizationId
|
||||
* @return
|
||||
* @param userRoleIds 用户组ID集合
|
||||
* @param organizationId 组织ID
|
||||
* @return 用户组集合
|
||||
*/
|
||||
private Map<String, UserRole> checkUseRoleExist(List<String> userRoleIds, String organizationId) {
|
||||
UserRoleExample userRoleExample = new UserRoleExample();
|
||||
|
@ -582,6 +616,11 @@ public class OrganizationService {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否存在
|
||||
* @param memberIds 成员ID集合
|
||||
* @return 用户集合
|
||||
*/
|
||||
private Map<String, User> checkUserExist(List<String> memberIds) {
|
||||
UserExample userExample = new UserExample();
|
||||
userExample.createCriteria().andIdIn(memberIds);
|
||||
|
@ -592,6 +631,12 @@ public class OrganizationService {
|
|||
return users.stream().collect(Collectors.toMap(User::getId, user -> user));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查项目是否存在
|
||||
* @param projectIds 项目ID集合
|
||||
* @param organizationId 组织ID
|
||||
* @return 项目集合
|
||||
*/
|
||||
private Map<String, Project> checkProjectExist(List<String> projectIds, String organizationId) {
|
||||
ProjectExample projectExample = new ProjectExample();
|
||||
projectExample.createCriteria().andIdIn(projectIds).andOrganizationIdEqualTo(organizationId);
|
||||
|
@ -601,4 +646,47 @@ public class OrganizationService {
|
|||
}
|
||||
return projects.stream().collect(Collectors.toMap(Project::getId, project -> project));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理组织管理员信息
|
||||
* @param organizationDTOS 组织集合
|
||||
* @return 组织列表
|
||||
*/
|
||||
private List<OrganizationDTO> buildOrgAdminInfo(List<OrganizationDTO> organizationDTOS) {
|
||||
if (CollectionUtils.isEmpty(organizationDTOS)) {
|
||||
return organizationDTOS;
|
||||
}
|
||||
organizationDTOS.forEach(organizationDTO -> {
|
||||
List<User> orgAdminList = extOrganizationMapper.getOrgAdminList(organizationDTO.getId());
|
||||
organizationDTO.setOrgAdmins(orgAdminList);
|
||||
});
|
||||
return organizationDTOS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置操作日志
|
||||
* @param organizationId 组织ID
|
||||
* @param createUser 创建人
|
||||
* @param type 操作类型
|
||||
* @param content 操作内容
|
||||
* @param path 请求路径
|
||||
* @param originalValue 原始值
|
||||
* @param modifiedValue 修改值
|
||||
* @param logs 日志集合
|
||||
*/
|
||||
private void setLog(String organizationId, String createUser, String type, String content, String path, Object originalValue, Object modifiedValue, List<LogDTO> logs) {
|
||||
LogDTO dto = new LogDTO(
|
||||
"system",
|
||||
"system",
|
||||
organizationId,
|
||||
createUser,
|
||||
type,
|
||||
OperationLogModule.SYSTEM_ORGANIZATION,
|
||||
content);
|
||||
dto.setPath(path);
|
||||
dto.setMethod(HttpMethodConstants.POST.name());
|
||||
dto.setOriginalValue(JSON.toJSONBytes(originalValue));
|
||||
dto.setModifiedValue(JSON.toJSONBytes(modifiedValue));
|
||||
logs.add(dto);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package io.metersphere.system.service;
|
||||
|
||||
import io.metersphere.sdk.constants.InternalUserRole;
|
||||
import io.metersphere.sdk.dto.PermissionDefinitionItem;
|
||||
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
|
||||
import io.metersphere.sdk.exception.MSException;
|
||||
|
@ -14,9 +15,8 @@ import org.apache.commons.collections.CollectionUtils;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.metersphere.system.controller.result.SystemResultCode.NO_ORG_USER_ROLE_PERMISSION;
|
||||
|
||||
|
@ -41,6 +41,8 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
|
|||
UserRoleRelationMapper userRoleRelationMapper;
|
||||
@Resource
|
||||
UserRolePermissionMapper userRolePermissionMapper;
|
||||
@Resource
|
||||
ExtUserRoleRelationMapper extUserRoleRelationMapper;
|
||||
|
||||
public List<UserRole> list(String organizationId) {
|
||||
UserRoleExample example = new UserRoleExample();
|
||||
|
@ -66,14 +68,40 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
|
|||
return super.update(userRole);
|
||||
}
|
||||
|
||||
public void delete(String roleId) {
|
||||
public void delete(String roleId, String currentUserId) {
|
||||
UserRole oldRole = get(roleId);
|
||||
// 非组织用户组不允许删除, 内置用户组不允许删除
|
||||
checkOrgUserRole(oldRole);
|
||||
checkInternalUserRole(oldRole);
|
||||
// 删除用户组
|
||||
userRoleMapper.deleteByPrimaryKey(roleId);
|
||||
UserRoleRelationExample relationExample = new UserRoleRelationExample();
|
||||
relationExample.createCriteria().andRoleIdEqualTo(roleId);
|
||||
relationExample.createCriteria().andRoleIdEqualTo(roleId).andSourceIdEqualTo(oldRole.getScopeId());
|
||||
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(relationExample);
|
||||
List<UserRoleRelation> orgMemberRelations = new ArrayList<>();
|
||||
if (CollectionUtils.isNotEmpty(userRoleRelations)) {
|
||||
// 如果删除的组织用户组内成员只有当前一个用户组,则给该成员赋予组织成员用户组
|
||||
List<String> userIds = userRoleRelations.stream().map(UserRoleRelation::getUserId).toList();
|
||||
UserRoleRelationExample userRelationExample = new UserRoleRelationExample();
|
||||
userRelationExample.createCriteria().andUserIdIn(userIds).andSourceIdEqualTo(oldRole.getScopeId());
|
||||
List<UserRoleRelation> allUserRelations = userRoleRelationMapper.selectByExample(userRelationExample);
|
||||
Map<String, List<UserRoleRelation>> userRoleRelationMap = allUserRelations.stream().collect(Collectors.groupingBy(UserRoleRelation::getUserId));
|
||||
userRoleRelationMap.forEach((userId, relations) -> {
|
||||
if (relations.size() == 1) {
|
||||
UserRoleRelation relation = new UserRoleRelation();
|
||||
relation.setId(UUID.randomUUID().toString());
|
||||
relation.setUserId(userId);
|
||||
relation.setSourceId(oldRole.getScopeId());
|
||||
relation.setRoleId(InternalUserRole.ORG_MEMBER.getValue());
|
||||
relation.setCreateTime(System.currentTimeMillis());
|
||||
relation.setCreateUser(currentUserId);
|
||||
orgMemberRelations.add(relation);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(orgMemberRelations)) {
|
||||
extUserRoleRelationMapper.batchInsert(orgMemberRelations);
|
||||
}
|
||||
userRoleRelationMapper.deleteByExample(relationExample);
|
||||
UserRolePermissionExample permissionExample = new UserRolePermissionExample();
|
||||
permissionExample.createCriteria().andRoleIdEqualTo(roleId);
|
||||
|
@ -101,7 +129,15 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
|
|||
public void removeMember(OrganizationUserRoleMemberEditRequest request) {
|
||||
String removeUserId = request.getUserIds().get(0);
|
||||
checkMemberParam(removeUserId, request.getUserRoleId());
|
||||
// 移除组织-用户组的成员, 若成员只存在该组织下唯一用户组, 则提示不能移除
|
||||
UserRoleRelationExample example = new UserRoleRelationExample();
|
||||
example.createCriteria().andUserIdEqualTo(removeUserId)
|
||||
.andRoleIdNotEqualTo(request.getUserRoleId())
|
||||
.andSourceIdEqualTo(request.getOrganizationId());
|
||||
if (userRoleRelationMapper.countByExample(example) == 0) {
|
||||
throw new MSException(Translator.get("at_least_one_user_role_require"));
|
||||
}
|
||||
example.clear();
|
||||
example.createCriteria().andUserIdEqualTo(removeUserId)
|
||||
.andRoleIdEqualTo(request.getUserRoleId())
|
||||
.andSourceIdEqualTo(request.getOrganizationId());
|
||||
|
|
|
@ -70,14 +70,14 @@ public class OrganizationUserRoleControllerTests extends BaseTest {
|
|||
// 返回请求正常
|
||||
Assertions.assertNotNull(resultHolder);
|
||||
// 返回总条数是否为init_organization_user_role.sql中的数据总数
|
||||
Assertions.assertEquals(4, JSON.parseArray(JSON.toJSONString(resultHolder.getData())).size());
|
||||
Assertions.assertEquals(5, JSON.parseArray(JSON.toJSONString(resultHolder.getData())).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void testOrganizationUserRoleAddSuccess() throws Exception {
|
||||
OrganizationUserRoleEditRequest request = new OrganizationUserRoleEditRequest();
|
||||
request.setName("default-org-role-4");
|
||||
request.setName("default-org-role-5");
|
||||
request.setType(ORGANIZATION_ROLE_TYPE);
|
||||
request.setScopeId("default-organization-2");
|
||||
this.requestPost(ORGANIZATION_USER_ROLE_ADD, request, status().isOk());
|
||||
|
@ -90,7 +90,7 @@ public class OrganizationUserRoleControllerTests extends BaseTest {
|
|||
// 返回请求正常
|
||||
Assertions.assertNotNull(resultHolder);
|
||||
// 返回总条数是否为init_organization_user_role.sql中的数据总数
|
||||
Assertions.assertEquals(5, JSON.parseArray(JSON.toJSONString(resultHolder.getData())).size());
|
||||
Assertions.assertEquals(6, JSON.parseArray(JSON.toJSONString(resultHolder.getData())).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -343,8 +343,14 @@ public class OrganizationUserRoleControllerTests extends BaseTest {
|
|||
public void testOrganizationUserRoleRemoveMemberSuccess() throws Exception {
|
||||
OrganizationUserRoleMemberEditRequest request = new OrganizationUserRoleMemberEditRequest();
|
||||
request.setOrganizationId("default-organization-2");
|
||||
request.setUserRoleId("default-org-role-id-3");
|
||||
request.setUserRoleId("default-org-role-id-4");
|
||||
request.setUserIds(List.of("admin"));
|
||||
this.requestPost(ORGANIZATION_USER_ROLE_ADD_MEMBER, request, status().isOk());
|
||||
request = new OrganizationUserRoleMemberEditRequest();
|
||||
request.setOrganizationId("default-organization-2");
|
||||
request.setUserRoleId("default-org-role-id-4");
|
||||
request.setUserIds(List.of("admin"));
|
||||
// 成员组织用户组存在多个, 移除成功
|
||||
this.requestPost(ORGANIZATION_USER_ROLE_REMOVE_MEMBER, request, status().isOk());
|
||||
}
|
||||
|
||||
|
@ -363,6 +369,19 @@ public class OrganizationUserRoleControllerTests extends BaseTest {
|
|||
request.setUserRoleId("default-org-role-id-x");
|
||||
// 用户组不存在
|
||||
this.requestPost(ORGANIZATION_USER_ROLE_REMOVE_MEMBER, request, status().is5xxServerError());
|
||||
request = new OrganizationUserRoleMemberEditRequest();
|
||||
request.setOrganizationId("default-organization-2");
|
||||
request.setUserRoleId("default-org-role-id-3");
|
||||
request.setUserIds(List.of("admin"));
|
||||
// 成员用户组只有一个, 移除失败
|
||||
this.requestPost(ORGANIZATION_USER_ROLE_REMOVE_MEMBER, request, status().is5xxServerError());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(17)
|
||||
public void testOrganizationUserRoleDeleteOnlyMemberSuccess() throws Exception {
|
||||
// 移除用户组, 且存在成员仅有该用户组
|
||||
this.requestGet(ORGANIZATION_USER_ROLE_DELETE + "/default-org-role-id-3", status().isOk());
|
||||
}
|
||||
|
||||
private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception {
|
||||
|
|
|
@ -40,7 +40,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
|
|||
private MockMvc mockMvc;
|
||||
|
||||
public static final String ORGANIZATION_LIST = "/system/organization/list";
|
||||
public static final String ORGANIZATION_LIST_ALL = "/system/organization/list-all";
|
||||
public static final String ORGANIZATION_LIST_OPTION_ALL = "/system/organization/option/all";
|
||||
public static final String ORGANIZATION_DEFAULT = "/system/organization/default";
|
||||
public static final String ORGANIZATION_LIST_MEMBER = "/system/organization/list-member";
|
||||
public static final String ORGANIZATION_ADD_MEMBER = "/system/organization/add-member";
|
||||
|
@ -126,7 +126,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
|
|||
@Test
|
||||
@Order(3)
|
||||
public void testListAllOrganizationSuccess() throws Exception {
|
||||
MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_ALL, null);
|
||||
MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_OPTION_ALL, null);
|
||||
// 获取返回值
|
||||
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
|
||||
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
|
||||
|
@ -139,7 +139,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
|
|||
@Test
|
||||
@Order(4)
|
||||
public void testListAllOrganizationError() throws Exception {
|
||||
this.requestGet(ORGANIZATION_LIST_ALL, status().isMethodNotAllowed());
|
||||
this.requestGet(ORGANIZATION_LIST_OPTION_ALL, status().isMethodNotAllowed());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -5,7 +5,13 @@ INSERT INTO user_role(id, name, description, internal, type, create_time, update
|
|||
('default-org-role-id-2', 'default-org-role-2', 'XXX', FALSE, 'ORGANIZATION', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'default-organization-2');
|
||||
INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUE
|
||||
('default-org-role-id-3', 'default-org-role-3', 'XXX', FALSE, 'ORGANIZATION', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'default-organization-2');
|
||||
INSERT INTO user_role(id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUE
|
||||
('default-org-role-id-4', 'default-org-role-4', 'XXX', FALSE, 'ORGANIZATION', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'default-organization-2');
|
||||
-- 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-admin-user', 'default-Administrator-user', 'admin-default@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin');
|
||||
INSERT INTO user_role_permission (id, role_id, permission_id) VALUE
|
||||
(uuid(), 'default-org-role-id-3', 'ORGANIZATION_USER_ROLE:READ');
|
||||
INSERT INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user) VALUE
|
||||
(UUID(), 'default-admin', 'default-org-role-id-3', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin');
|
||||
(UUID(), 'default-admin', 'default-org-role-id-3', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin');
|
||||
-- INSERT INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user) VALUE
|
||||
-- (UUID(), 'default-admin-user', 'default-org-role-id-2', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin');
|
Loading…
Reference in New Issue