角色,分组,权限集 名称不能重复验证
This commit is contained in:
parent
1b72a1c10c
commit
8ed88b9813
@ -50,7 +50,10 @@ public class SaasPermissionGroupVO {
|
||||
* 是否为通用权限集 1:是 0:否
|
||||
*/
|
||||
private Integer isCommon;
|
||||
|
||||
/**
|
||||
* 权限集对应的角色
|
||||
*/
|
||||
private Long roleId;
|
||||
/**
|
||||
* 权限
|
||||
*/
|
||||
|
||||
@ -5,6 +5,7 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -19,6 +20,7 @@ public class SaasRoleGroupVO {
|
||||
/**
|
||||
* 工作台类型字典code
|
||||
*/
|
||||
@NotBlank
|
||||
private String workspaceTypeCode;
|
||||
|
||||
/**
|
||||
@ -33,6 +35,8 @@ public class SaasRoleGroupVO {
|
||||
|
||||
/**
|
||||
* 角色
|
||||
* 如果查询时候指定了roleIds,返回的roleIds <= 请求的roleIds
|
||||
* 如果查询时候没有指定roleIds, 返回roleId 等于 该分组对应的所有的角色id
|
||||
*/
|
||||
private List<Long> roleIds;
|
||||
|
||||
|
||||
@ -36,7 +36,7 @@ public class SaasPermissionGroupController implements SaasPermissionGroupApi {
|
||||
|
||||
@Override
|
||||
public ApiResult<Long> saveOrUpdateSpecial(SaveOrUpdatePermissionGroupVO permissionGroup) {
|
||||
return ApiResult.ok(permissionGroupService.saveOrUpdateScope(permissionGroup));
|
||||
return ApiResult.ok(permissionGroupService.saveOrUpdateSpecial(permissionGroup));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -34,7 +34,7 @@ public interface PermissionGroupService {
|
||||
* @param permissionGroup
|
||||
* @return
|
||||
*/
|
||||
Long saveOrUpdateScope(SaveOrUpdatePermissionGroupVO permissionGroup);
|
||||
Long saveOrUpdateSpecial(SaveOrUpdatePermissionGroupVO permissionGroup);
|
||||
|
||||
SaasPermissionGroup getRequiredPermissionGroup(Long permissionGroupId, PermissionGroupType type);
|
||||
|
||||
|
||||
@ -23,11 +23,11 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -41,23 +41,14 @@ import java.util.stream.Collectors;
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class PermissionGroupImpl implements PermissionGroupService {
|
||||
|
||||
@Autowired
|
||||
SaasPermissionGroupDao permissionGroupDao;
|
||||
@Autowired
|
||||
SaasPgroupRoleRelationDao roleRelationDao;
|
||||
@Autowired
|
||||
SaasPgroupPermissionRelationDao permissionRelationDao;
|
||||
@Autowired
|
||||
PermissionPointService featureService;
|
||||
@Autowired
|
||||
SaasPermissionGroupScopeDao saasPermissionGroupScopeDao;
|
||||
@Autowired
|
||||
SaasPgroupPermissionRelationService saasPgroupPermissionRelationService;
|
||||
@Autowired
|
||||
SaasRoleDao saasRoleDao;
|
||||
@Autowired
|
||||
SaasPermissionGroupScopeService saasPermissionGroupScopeService;
|
||||
private final SaasPermissionGroupDao permissionGroupDao;
|
||||
private final SaasPgroupRoleRelationDao pgroupRoleRelationDao;
|
||||
private final SaasPgroupPermissionRelationDao permissionRelationDao;
|
||||
private final PermissionPointService featureService;
|
||||
private final SaasPermissionGroupScopeDao saasPermissionGroupScopeDao;
|
||||
private final SaasPgroupPermissionRelationService saasPgroupPermissionRelationService;
|
||||
private final SaasRoleDao saasRoleDao;
|
||||
private final SaasPermissionGroupScopeService saasPermissionGroupScopeService;
|
||||
|
||||
@Override
|
||||
public List<SaasPermissionGroupVO> query(QuerySaasPermissionGroupReq req) {
|
||||
@ -74,7 +65,7 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
// 如果角色id不为空则先查询角色权限集关联表
|
||||
List<SaasPgroupRoleRelation> relationList = null;
|
||||
if (CollectionUtils.isNotEmpty(req.getRoleIds())) {
|
||||
relationList = roleRelationDao.lambdaQuery()
|
||||
relationList = pgroupRoleRelationDao.lambdaQuery()
|
||||
.in(SaasPgroupRoleRelation::getRoleId, req.getRoleIds())
|
||||
.eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
|
||||
.list();
|
||||
@ -106,6 +97,15 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
}
|
||||
List<Long> groupIds = groupList.stream().map(BaseEntity::getId).collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(relationList)) {
|
||||
relationList = pgroupRoleRelationDao.lambdaQuery()
|
||||
.in(SaasPgroupRoleRelation::getGroupId, groupIds)
|
||||
.eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
|
||||
.list();
|
||||
}
|
||||
// 角色和权限集是1Vn 关系,权限集必然依附角色存在
|
||||
Map<Long, SaasPgroupRoleRelation> pgrrMap = relationList.stream().collect(Collectors.toMap(SaasPgroupRoleRelation::getGroupId, Function.identity(), (e1, e2) -> e2));
|
||||
|
||||
List<SaasPermissionGroupScope> saasPermissionGroupScopesSource = saasPermissionGroupScopeDao.lambdaQuery()
|
||||
.in(SaasPermissionGroupScope::getPgroupId, groupIds)
|
||||
.eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
|
||||
@ -145,7 +145,11 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
.feature(feature)
|
||||
.scopes(saasPermissionGroupScopes.stream().filter(e -> e.getPgroupId().equals(group.getId())).map(e -> BeanMapper.copyBean(e, SaasRolePermissionScopeVO.class)).collect(Collectors.toList()))
|
||||
.createBy(group.getCreateBy())
|
||||
.roleId(Optional.ofNullable(pgrrMap.get(group.getId())).map(SaasPgroupRoleRelation::getRoleId).orElse(null))
|
||||
.createBy(group.getCreateBy())
|
||||
.updateBy(group.getUpdateBy())
|
||||
.creatorName(group.getCreatorName())
|
||||
.updatorName(group.getUpdatorName())
|
||||
.type(group.getType())
|
||||
.isCommon(group.getIsCommon())
|
||||
.build();
|
||||
@ -168,8 +172,9 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
}
|
||||
// 如果角色id不为空则先查询角色权限集关联表
|
||||
IPage<SaasPermissionGroup> iPage = req.toPage();
|
||||
List<SaasPgroupRoleRelation> relationList = null;
|
||||
if (CollectionUtils.isNotEmpty(req.getRoleIds())) {
|
||||
List<SaasPgroupRoleRelation> relationList = roleRelationDao.lambdaQuery()
|
||||
relationList = pgroupRoleRelationDao.lambdaQuery()
|
||||
.in(SaasPgroupRoleRelation::getRoleId, req.getRoleIds())
|
||||
.eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
|
||||
.list();
|
||||
@ -201,6 +206,16 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
return PageResp.zero(iPage.getCurrent(), iPage.getSize());
|
||||
}
|
||||
List<Long> groupIds = groupList.stream().map(BaseEntity::getId).collect(Collectors.toList());
|
||||
|
||||
if (CollectionUtils.isEmpty(relationList)) {
|
||||
relationList = pgroupRoleRelationDao.lambdaQuery()
|
||||
.in(SaasPgroupRoleRelation::getGroupId, groupIds)
|
||||
.eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
|
||||
.list();
|
||||
}
|
||||
// 角色和权限集是1Vn 关系,权限集必然依附角色存在
|
||||
Map<Long, SaasPgroupRoleRelation> pgrrMap = relationList.stream().collect(Collectors.toMap(SaasPgroupRoleRelation::getGroupId, Function.identity(), (e1, e2) -> e2));
|
||||
|
||||
// 查询权限集关联的权限
|
||||
List<SaasPgroupPermissionRelation> permissionList = permissionRelationDao.lambdaQuery()
|
||||
.in(SaasPgroupPermissionRelation::getGroupId, groupIds)
|
||||
@ -241,7 +256,10 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
.feature(finalFeature)
|
||||
.scopes(saasPermissionGroupScopes.stream().filter(e -> e.getPgroupId().equals(group.getId())).map(e -> BeanMapper.copyBean(e, SaasRolePermissionScopeVO.class)).collect(Collectors.toList()))
|
||||
.createBy(group.getCreateBy())
|
||||
.creatorName(group.getCreatorName())
|
||||
.updateBy(group.getUpdateBy())
|
||||
.updatorName(group.getUpdatorName())
|
||||
.roleId(Optional.ofNullable(pgrrMap.get(group.getId())).map(SaasPgroupRoleRelation::getRoleId).orElse(null))
|
||||
.type(group.getType())
|
||||
.isCommon(group.getIsCommon())
|
||||
.build()
|
||||
@ -266,27 +284,9 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long saveOrUpdateScope(SaveOrUpdatePermissionGroupVO permissionGroup) {
|
||||
SaasPermissionGroup saasPermissionGroup;
|
||||
validSaasPermissionGroup(permissionGroup);
|
||||
public Long saveOrUpdateSpecial(SaveOrUpdatePermissionGroupVO permissionGroup) {
|
||||
Date now = new Date();
|
||||
if (Objects.nonNull(permissionGroup.getId())) {
|
||||
saasPermissionGroup = getRequiredPermissionGroup(permissionGroup.getId(), PermissionGroupType.SPECIAL);
|
||||
} else {
|
||||
saasPermissionGroup = new SaasPermissionGroup();
|
||||
saasPermissionGroup.setCreateBy(permissionGroup.getOperatorId());
|
||||
saasPermissionGroup.setCreatorName(Optional.ofNullable(permissionGroup.getOperatorName()).orElse(""));
|
||||
saasPermissionGroup.setUpdateBy(permissionGroup.getOperatorId());
|
||||
saasPermissionGroup.setUpdatorName(Optional.ofNullable(permissionGroup.getOperatorName()).orElse(""));
|
||||
saasPermissionGroup.setIsCommon(PermissionGroupType.SPECIAL.getCode());
|
||||
saasPermissionGroup.setCreateAt(now);
|
||||
}
|
||||
saasPermissionGroup.setType(permissionGroup.getType());
|
||||
saasPermissionGroup.setName(permissionGroup.getName());
|
||||
saasPermissionGroup.setUpdateBy(permissionGroup.getOperatorId());
|
||||
saasPermissionGroup.setUpdatorName(Optional.ofNullable(permissionGroup.getOperatorName()).orElse(""));
|
||||
saasPermissionGroup.setDescription(permissionGroup.getDescription());
|
||||
saasPermissionGroup.setUpdateAt(now);
|
||||
SaasPermissionGroup saasPermissionGroup = validAndBuildPermissionGroup(permissionGroup, now);
|
||||
permissionGroupDao.saveOrUpdate(saasPermissionGroup);
|
||||
if (Objects.isNull(permissionGroup.getId())) {
|
||||
SaasPgroupRoleRelation roleRelation = new SaasPgroupRoleRelation();
|
||||
@ -296,7 +296,7 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
roleRelation.setUpdateBy(permissionGroup.getOperatorId());
|
||||
roleRelation.setCreateAt(now);
|
||||
roleRelation.setUpdateAt(now);
|
||||
roleRelationDao.save(roleRelation);
|
||||
pgroupRoleRelationDao.save(roleRelation);
|
||||
}
|
||||
List<SaasPermissionGroupScope> scopes = new ArrayList<>();
|
||||
if (CollectionUtils.isNotEmpty(permissionGroup.getSelectedWorkspace())) {
|
||||
@ -345,7 +345,7 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deletePermissionGroupSpecial(DeletePermissionGroupVO group) {
|
||||
List<SaasPgroupRoleRelation> relations = roleRelationDao.lambdaQuery().eq(SaasPgroupRoleRelation::getRoleId, group.getRoleId())
|
||||
List<SaasPgroupRoleRelation> relations = pgroupRoleRelationDao.lambdaQuery().eq(SaasPgroupRoleRelation::getRoleId, group.getRoleId())
|
||||
.in(SaasPgroupRoleRelation::getGroupId, group.getSpecialPermissionGroupIds())
|
||||
.eq(SaasPgroupRoleRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
if (CollectionUtils.isEmpty(relations)) {
|
||||
@ -359,7 +359,7 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "只能删除例外权限集");
|
||||
}
|
||||
//删除角色关联
|
||||
roleRelationDao.removeByIds(relations.stream().map(SaasPgroupRoleRelation::getId).sorted().collect(Collectors.toList()));
|
||||
pgroupRoleRelationDao.removeByIds(relations.stream().map(SaasPgroupRoleRelation::getId).sorted().collect(Collectors.toList()));
|
||||
List<SaasPermissionGroupScope> scopes = saasPermissionGroupScopeDao.lambdaQuery().in(SaasPermissionGroupScope::getPgroupId, deleteGroupIds)
|
||||
.eq(SaasPermissionGroupScope::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
if (CollectionUtils.isNotEmpty(scopes)) {
|
||||
@ -382,13 +382,14 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
.update();
|
||||
}
|
||||
|
||||
private void validSaasPermissionGroup(SaveOrUpdatePermissionGroupVO permissionGroup) {
|
||||
private SaasPermissionGroup validAndBuildPermissionGroup(SaveOrUpdatePermissionGroupVO permissionGroup, Date now) {
|
||||
// 验证角色是否存在
|
||||
SaasRole saasRole = saasRoleDao.getById(permissionGroup.getRoleId());
|
||||
if (Objects.isNull(saasRole)) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "权限集不存在");
|
||||
}
|
||||
if (Objects.nonNull(permissionGroup.getId())) {
|
||||
int relationCount = roleRelationDao.lambdaQuery()
|
||||
int relationCount = pgroupRoleRelationDao.lambdaQuery()
|
||||
.eq(SaasPgroupRoleRelation::getRoleId, permissionGroup.getRoleId())
|
||||
.eq(SaasPgroupRoleRelation::getGroupId, permissionGroup.getId())
|
||||
.eq(SaasPgroupRoleRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).count();
|
||||
@ -396,13 +397,48 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "角色和权限组不存在关联关系");
|
||||
}
|
||||
}
|
||||
validPermissionGroupScope(permissionGroup);
|
||||
SaasPermissionGroup saasPermissionGroup;
|
||||
if (Objects.isNull(permissionGroup.getId())) {
|
||||
saasPermissionGroup = new SaasPermissionGroup();
|
||||
saasPermissionGroup.setCreateBy(permissionGroup.getOperatorId());
|
||||
saasPermissionGroup.setCreatorName(Optional.ofNullable(permissionGroup.getOperatorName()).orElse(""));
|
||||
saasPermissionGroup.setIsCommon(PermissionGroupType.SPECIAL.getCode());
|
||||
saasPermissionGroup.setCreateAt(now);
|
||||
} else {
|
||||
saasPermissionGroup = getRequiredPermissionGroup(permissionGroup.getId(), PermissionGroupType.SPECIAL);
|
||||
}
|
||||
saasPermissionGroup.setType(permissionGroup.getType());
|
||||
saasPermissionGroup.setName(permissionGroup.getName());
|
||||
saasPermissionGroup.setUpdateBy(permissionGroup.getOperatorId());
|
||||
saasPermissionGroup.setUpdatorName(Optional.ofNullable(permissionGroup.getOperatorName()).orElse(""));
|
||||
saasPermissionGroup.setDescription(permissionGroup.getDescription());
|
||||
saasPermissionGroup.setUpdateAt(now);
|
||||
List<SaasPgroupRoleRelation> relations = pgroupRoleRelationDao.lambdaQuery().eq(SaasPgroupRoleRelation::getRoleId, permissionGroup.getRoleId())
|
||||
.eq(SaasPgroupRoleRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
if (CollectionUtils.isNotEmpty(relations)) {
|
||||
List<Long> groupIds = relations.stream().map(SaasPgroupRoleRelation::getGroupId).sorted().collect(Collectors.toList());
|
||||
List<SaasPermissionGroup> groups = permissionGroupDao.lambdaQuery().in(SaasPermissionGroup::getId, groupIds).eq(SaasPermissionGroup::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
Optional<SaasPermissionGroup> repeatName = groups.stream().filter(g -> !Objects.equals(g.getId(), saasPermissionGroup.getId()) && StringUtils.equalsIgnoreCase(saasPermissionGroup.getName(), g.getName()))
|
||||
.findFirst();
|
||||
if (repeatName.isPresent()) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "同角色,分组名称不能重复");
|
||||
}
|
||||
}
|
||||
return saasPermissionGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证例外
|
||||
* @param permissionGroup
|
||||
*/
|
||||
private void validPermissionGroupScope(SaveOrUpdatePermissionGroupVO permissionGroup) {
|
||||
List<SaveOrUpdatePermissionGroupVO.PermissionGroupScopeVO> selectedWorkspace = permissionGroup.getSelectedWorkspace();
|
||||
List<SaveOrUpdatePermissionGroupVO.PermissionGroupScopeVO> selectedOu = permissionGroup.getSelectedOu();
|
||||
Set<Integer> scopeTypes = new HashSet<>();
|
||||
if (CollectionUtils.isEmpty(selectedWorkspace) && CollectionUtils.isEmpty(selectedOu)) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "例外不能为空");
|
||||
}
|
||||
// TODO 校验规则是产品给的吗?
|
||||
if (CollectionUtils.isNotEmpty(selectedWorkspace)) {
|
||||
Map<Integer, List<SaveOrUpdatePermissionGroupVO.PermissionGroupScopeVO>> selectedWorkspaceMap = selectedWorkspace.stream()
|
||||
.collect(Collectors.groupingBy(SaveOrUpdatePermissionGroupVO.PermissionGroupScopeVO::getType));
|
||||
@ -429,6 +465,7 @@ public class PermissionGroupImpl implements PermissionGroupService {
|
||||
}
|
||||
scopeTypes.addAll(selectedOu.stream().map(SaveOrUpdatePermissionGroupVO.PermissionGroupScopeVO::getType).collect(Collectors.toSet()));
|
||||
}
|
||||
// 移除这行,例外设置支持移除和包含
|
||||
if (scopeTypes.size() > 1) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "例外类型不能同时指定适用与不适用");
|
||||
}
|
||||
|
||||
@ -201,36 +201,16 @@ public class RoleServiceImpl implements RoleService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long saveOrUpdate(SaveOrUpdateRoleVO saveOrUpdateRole) {
|
||||
// 新增OR修改角色
|
||||
SaasRole saasRole;
|
||||
Date now = new Date();
|
||||
boolean save = Objects.isNull(saveOrUpdateRole.getId());
|
||||
if (!save) {
|
||||
saasRole = saasRoleDao.getById(saveOrUpdateRole.getId());
|
||||
if (Objects.isNull(saasRole)) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "角色不存在");
|
||||
}
|
||||
if (Objects.isNull(saveOrUpdateRole.getPermissionGroupId())) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "更新角色时权限集不能为空不存在");
|
||||
}
|
||||
} else {
|
||||
saasRole = new SaasRole();
|
||||
saasRole.setCreateBy(saveOrUpdateRole.getOperatorId());
|
||||
saasRole.setCreateAt(now);
|
||||
}
|
||||
|
||||
List<SaveOrUpdateRoleVO.GroupInfoVO> groupTrees = saveOrUpdateRole.getGroupTree();
|
||||
// 验证角色分组信息
|
||||
validRoleGroup(groupTrees);
|
||||
SaasRole saasRole = validAndBuildRole(saveOrUpdateRole, now);
|
||||
//验证权限集信息
|
||||
SaasPermissionGroup saasPermissionGroup = validPermissionGroupCommon(saveOrUpdateRole);
|
||||
validFeature(saveOrUpdateRole.getSelectedPPIds());
|
||||
saasRole.setId(saveOrUpdateRole.getId());
|
||||
saasRole.setName(saveOrUpdateRole.getName());
|
||||
saasRole.setDescription(saasRole.getDescription());
|
||||
saasRole.setRoleType(saveOrUpdateRole.getRoleType());
|
||||
saasRole.setWorkspaceId(saveOrUpdateRole.getWorkspaceId());
|
||||
saasRole.setOwnerOuId(saveOrUpdateRole.getOwnerOuId());
|
||||
saasRole.setUpdateBy(saveOrUpdateRole.getOperatorId());
|
||||
saasRole.setUpdateAt(now);
|
||||
|
||||
saasRoleDao.saveOrUpdate(saasRole);
|
||||
// 新增或者保存分组和角色映射关系
|
||||
saasRoleGroupRelationService.saveOrUpdate(groupTrees.stream().map(g -> {
|
||||
@ -264,6 +244,50 @@ public class RoleServiceImpl implements RoleService {
|
||||
return saasRole.getId();
|
||||
}
|
||||
|
||||
private SaasRole validAndBuildRole(SaveOrUpdateRoleVO saveOrUpdateRole, Date now) {
|
||||
SaasRole saasRole;
|
||||
if (Objects.isNull(saveOrUpdateRole.getId())) {
|
||||
saasRole = new SaasRole();
|
||||
saasRole.setCreateBy(saveOrUpdateRole.getOperatorId());
|
||||
saasRole.setCreateAt(now);
|
||||
} else {
|
||||
saasRole = saasRoleDao.getById(saveOrUpdateRole.getId());
|
||||
if (Objects.isNull(saasRole)) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "角色不存在");
|
||||
}
|
||||
if (Objects.isNull(saveOrUpdateRole.getPermissionGroupId())) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "更新角色时权限集不能为空不存在");
|
||||
}
|
||||
}
|
||||
saasRole.setName(saveOrUpdateRole.getName());
|
||||
saasRole.setDescription(saasRole.getDescription());
|
||||
saasRole.setRoleType(saveOrUpdateRole.getRoleType());
|
||||
saasRole.setWorkspaceId(saveOrUpdateRole.getWorkspaceId());
|
||||
saasRole.setOwnerOuId(saveOrUpdateRole.getOwnerOuId());
|
||||
saasRole.setUpdateBy(saveOrUpdateRole.getOperatorId());
|
||||
saasRole.setUpdateAt(now);
|
||||
// 不可能为空
|
||||
List<Long> groupIds = saveOrUpdateRole.getGroupTree().stream().map(SaveOrUpdateRoleVO.GroupInfoVO::getId).sorted().collect(Collectors.toList());
|
||||
//同分组内角色名称不能重复
|
||||
List<SaasRoleGroupRelation> relations = roleGroupRelationDao.lambdaQuery().in(SaasRoleGroupRelation::getSaasRoleGroupId, groupIds)
|
||||
.eq(SaasRoleGroupRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
if (CollectionUtils.isNotEmpty(relations)) {
|
||||
List<Long> roleIds = relations.stream().map(SaasRoleGroupRelation::getRoleId).sorted().collect(Collectors.toList());
|
||||
Map<Long, Set<Long>> groupRoleMap = relations.stream().collect(Collectors.groupingBy(SaasRoleGroupRelation::getSaasRoleGroupId, Collectors.mapping(SaasRoleGroupRelation::getRoleId, Collectors.toSet())));
|
||||
List<SaasRole> roles = saasRoleDao.lambdaQuery().in(SaasRole::getId, roleIds).eq(SaasRole::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
Map<Long, SaasRole> roleMap = roles.stream().collect(Collectors.toMap(SaasRole::getId, Function.identity(), (e1, e2) -> e2));
|
||||
groupRoleMap.forEach((groupId, roleSet) -> {
|
||||
Optional<SaasRole> repeatNameRole = roleSet.stream().map(roleMap::get)
|
||||
.filter(e -> Objects.nonNull(e) && !Objects.equals(e.getId(), saveOrUpdateRole.getId()) && StringUtils.equalsIgnoreCase(e.getName(), saasRole.getName()))
|
||||
.findFirst();
|
||||
if (repeatNameRole.isPresent()) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "同分组内不角色名称不能重复");
|
||||
}
|
||||
});
|
||||
}
|
||||
return saasRole;
|
||||
}
|
||||
|
||||
private void validFeature(List<Long> featureIds) {
|
||||
if (CollectionUtils.isEmpty(featureIds)) {
|
||||
return;
|
||||
|
||||
@ -17,10 +17,7 @@ import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@ -42,8 +39,9 @@ public class SaasRoleGroupServiceImpl implements SaasRoleGroupService {
|
||||
} else if (!req.getOuIds().contains(-1L)) {
|
||||
req.getOuIds().add(-1L);
|
||||
}
|
||||
List<SaasRoleGroupRelation> saasRoleGroupRelations = null;
|
||||
if (CollectionUtils.isNotEmpty(req.getRoleIds())) {
|
||||
List<SaasRoleGroupRelation> saasRoleGroupRelations = saasRoleGroupRelationDao.lambdaQuery()
|
||||
saasRoleGroupRelations = saasRoleGroupRelationDao.lambdaQuery()
|
||||
.in(SaasRoleGroupRelation::getRoleId, req.getRoleIds())
|
||||
.eq(SaasRoleGroupRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
List<Long> groupIds = saasRoleGroupRelations.stream().map(SaasRoleGroupRelation::getSaasRoleGroupId).distinct().collect(Collectors.toList());
|
||||
@ -61,9 +59,12 @@ public class SaasRoleGroupServiceImpl implements SaasRoleGroupService {
|
||||
if (CollectionUtils.isEmpty(groups)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<SaasRoleGroupRelation> saasRoleGroupRelations = saasRoleGroupRelationDao.lambdaQuery()
|
||||
.in(SaasRoleGroupRelation::getSaasRoleGroupId, groups.stream().map(SaasRoleGroup::getId).collect(Collectors.toList()))
|
||||
.eq(SaasRoleGroupRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
// 如果参数指定了roleIds, 返回的值就包含在请求的roleIds中,否则返回全量的roleIds
|
||||
if (CollectionUtils.isEmpty(saasRoleGroupRelations)) {
|
||||
saasRoleGroupRelations = saasRoleGroupRelationDao.lambdaQuery()
|
||||
.in(SaasRoleGroupRelation::getSaasRoleGroupId, groups.stream().map(SaasRoleGroup::getId).collect(Collectors.toList()))
|
||||
.eq(SaasRoleGroupRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
}
|
||||
Map<Long, List<Long>> groupRoleMap = saasRoleGroupRelations.stream().collect(Collectors.groupingBy(SaasRoleGroupRelation::getSaasRoleGroupId, Collectors.mapping(SaasRoleGroupRelation::getRoleId, Collectors.toList())));
|
||||
List<SaasRoleGroupVO> results = groups.stream()
|
||||
.map(e -> {
|
||||
@ -80,19 +81,7 @@ public class SaasRoleGroupServiceImpl implements SaasRoleGroupService {
|
||||
|
||||
@Override
|
||||
public Long saveOrUpdate(SaasRoleGroupVO req) {
|
||||
// 拼接ouTypeCode字符串
|
||||
String ouTypeCodeStr = null;
|
||||
if (CollectionUtils.isNotEmpty(req.getOuTypeCode())) {
|
||||
ouTypeCodeStr = StringUtils.join(req.getOuTypeCode(),",");
|
||||
}
|
||||
SaasRoleGroup saasRoleGroup = new SaasRoleGroup();
|
||||
saasRoleGroup.setId(req.getId());
|
||||
saasRoleGroup.setWorkspaceTypeCode(req.getWorkspaceTypeCode());
|
||||
saasRoleGroup.setOuTypeCode(ouTypeCodeStr);
|
||||
saasRoleGroup.setName(req.getName());
|
||||
saasRoleGroup.setSort(req.getSort());
|
||||
saasRoleGroup.setWorkspaceId(req.getWorkspaceId() != null ? req.getWorkspaceId():-1L);
|
||||
saasRoleGroup.setOuId(req.getOuId() != null ? req.getOuId():-1L);
|
||||
SaasRoleGroup saasRoleGroup = validAndBuildGroup(req);
|
||||
saasRoleGroupDao.saveOrUpdate(saasRoleGroup);
|
||||
return saasRoleGroup.getId();
|
||||
}
|
||||
@ -116,4 +105,42 @@ public class SaasRoleGroupServiceImpl implements SaasRoleGroupService {
|
||||
saasRoleGroupDao.delete(ids);
|
||||
}
|
||||
|
||||
private SaasRoleGroup validAndBuildGroup(SaasRoleGroupVO req) {
|
||||
SaasRoleGroup saasRoleGroup;
|
||||
Date now = new Date();
|
||||
if (Objects.isNull(req.getId())) {
|
||||
//新增
|
||||
saasRoleGroup = new SaasRoleGroup();
|
||||
saasRoleGroup.setCreateAt(now);
|
||||
} else {
|
||||
//修改
|
||||
saasRoleGroup = saasRoleGroupDao.lambdaQuery().eq(SaasRoleGroup::getId, req.getId())
|
||||
.eq(SaasRoleGroup::getIsDelete, TableIsDeleteEnum.NORMAL.value).one();
|
||||
if (Objects.isNull(saasRoleGroup)) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "角色分组不存在");
|
||||
}
|
||||
}
|
||||
List<SaasRoleGroup> groups = saasRoleGroupDao.lambdaQuery().eq(SaasRoleGroup::getWorkspaceTypeCode, req.getWorkspaceTypeCode())
|
||||
.eq(SaasRoleGroup::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
|
||||
if (CollectionUtils.isNotEmpty(groups)) {
|
||||
Optional<SaasRoleGroup> repeatGroupName = groups.stream()
|
||||
.filter(g -> !Objects.equals(g.getId(), req.getId()) && StringUtils.equalsIgnoreCase(g.getName(), req.getName())).findFirst();
|
||||
if (repeatGroupName.isPresent()) {
|
||||
throw new BizException(BaseCode.BAD_REQUEST, "同工作台类型内,分组名称不能重复");
|
||||
}
|
||||
}
|
||||
// 拼接ouTypeCode字符串
|
||||
String ouTypeCodeStr = null;
|
||||
if (CollectionUtils.isNotEmpty(req.getOuTypeCode())) {
|
||||
ouTypeCodeStr = StringUtils.join(req.getOuTypeCode(),",");
|
||||
}
|
||||
saasRoleGroup.setUpdateAt(now);
|
||||
saasRoleGroup.setWorkspaceTypeCode(req.getWorkspaceTypeCode());
|
||||
saasRoleGroup.setOuTypeCode(ouTypeCodeStr);
|
||||
saasRoleGroup.setName(req.getName());
|
||||
saasRoleGroup.setSort(req.getSort());
|
||||
saasRoleGroup.setWorkspaceId(req.getWorkspaceId() != null ? req.getWorkspaceId() : -1L);
|
||||
saasRoleGroup.setOuId(req.getOuId() != null ? req.getOuId() : -1L);
|
||||
return saasRoleGroup;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user