feat: REQ-1650 角色名称校验

This commit is contained in:
zuoqinbo 2023-12-06 17:13:33 +08:00
parent 5d8db627d0
commit 1e5991c88a

View File

@ -38,9 +38,12 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.codehaus.groovy.runtime.DefaultGroovyMethods.collect;
/** /**
* 角色 * 角色
* *
@ -262,16 +265,49 @@ public class RoleServiceImpl implements RoleService {
} }
private void validRoleName(SaveOrUpdateRoleVO saveOrUpdateRole) { private void validRoleName(SaveOrUpdateRoleVO saveOrUpdateRole) {
List<SaasRole> roles = saasRoleDao.lambdaQuery() List<SaveOrUpdateRoleVO.GroupInfoVO> groupTrees = saveOrUpdateRole.getGroupTree();
.eq(SaasRole::getWorkspaceId, saveOrUpdateRole.getWorkspaceId()) if (CollectionUtil.isEmpty(groupTrees)) {
.eq(SaasRole::getOwnerOuId, saveOrUpdateRole.getOwnerOuId()) return;
.eq(SaasRole::getIsDelete, TableIsDeleteEnum.NORMAL.value).list(); }
if (CollectionUtils.isNotEmpty(roles)) { String currentWorkspaceCode = groupTrees.get(0).getWorkspaceTypeCode();
Optional<SaasRole> repeatGroupName = roles.stream() List<SaasRoleGroup> roleGroups = saasRoleGroupDao.lambdaQuery()
.filter(g -> !Objects.equals(g.getId(), saveOrUpdateRole.getId()) && StringUtils.equalsIgnoreCase(g.getName(), saveOrUpdateRole.getName())).findFirst(); .in(SaasRoleGroup::getWorkspaceTypeCode, currentWorkspaceCode)
if (repeatGroupName.isPresent()) { .eq(SaasRoleGroup::getWorkspaceId, saveOrUpdateRole.getWorkspaceId())
throw new ServiceException("同一个单位、同一工作台类型内,角色名称不能重复!"); .eq(SaasRoleGroup::getOuId, saveOrUpdateRole.getOwnerOuId())
.eq(SaasRoleGroup::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
List<Long> roleGroupIds = roleGroups.stream().map(SaasRoleGroup::getId).collect(Collectors.toList());
if (CollectionUtil.isEmpty(roleGroupIds)) {
return;
}
List<SaasRoleGroupRelation> roleGroupRelations = roleGroupRelationDao.lambdaQuery()
.in(SaasRoleGroupRelation::getSaasRoleGroupId, roleGroupIds)
.eq(SaasRoleGroupRelation::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
if (CollectionUtil.isNotEmpty(roleGroupRelations)) {
List<Long> saasRoleIds = roleGroupRelations.stream().map(SaasRoleGroupRelation::getRoleId).collect(Collectors.toList());
//确保这些角色id 都是正常使用的
List<SaasRole> roles = saasRoleDao.lambdaQuery()
.eq(SaasRole::getWorkspaceId, saveOrUpdateRole.getWorkspaceId())
.eq(SaasRole::getId, saasRoleIds)
.eq(SaasRole::getName, saveOrUpdateRole.getName())
.eq(SaasRole::getOwnerOuId, saveOrUpdateRole.getOwnerOuId())
.eq(SaasRole::getIsDelete, TableIsDeleteEnum.NORMAL.value).list();
if (CollectionUtil.isNotEmpty(roles)) {
//新增角色 判断角色名称重复
if (Objects.isNull(saveOrUpdateRole.getId())) {
throw new ServiceException("同一企业单位、同一工作台类型,角色名称不能重复!");
} else {
//如果是更新角色必须是当前角色
if (!(roles.size() == 1 && (roles.get(0).getId().equals(saveOrUpdateRole.getId())))) {
throw new ServiceException("同一企业单位、同一工作台类型,角色名称不能重复!");
}
}
} }
// Optional<SaasRole> repeatGroupName = roles.stream()
// .filter(g -> !Objects.equals(g.getId(), saveOrUpdateRole.getId()) && StringUtils.equalsIgnoreCase(g.getName(), saveOrUpdateRole.getName())).findFirst();
// if (repeatGroupName.isPresent()) {
// throw new ServiceException("同一个单位、同一工作台类型内,角色名称不能重复!");
// }
} }
} }