Commit 95bb9744c1ae6854532de24a251e0285ac87f62e
1 parent
848fcdf3
新建角色的时候,不允许创建 ADMIN 标识的角色
Showing
3 changed files
with
14 additions
and
4 deletions
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
| ... | ... | @@ -31,7 +31,8 @@ public interface ErrorCodeConstants { |
| 31 | 31 | ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1002003001, "已经存在名为【{}】的角色"); |
| 32 | 32 | ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1002003002, "已经存在编码为【{}】的角色"); |
| 33 | 33 | ErrorCode ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE = new ErrorCode(1002003004, "不能操作类型为系统内置的角色"); |
| 34 | - ErrorCode ROLE_IS_DISABLE = new ErrorCode(1002003004, "名字为【{}】的角色已被禁用"); | |
| 34 | + ErrorCode ROLE_IS_DISABLE = new ErrorCode(1002003005, "名字为【{}】的角色已被禁用"); | |
| 35 | + ErrorCode ROLE_ADMIN_CODE_ERROR = new ErrorCode(1002003006, "编码【{}】不能使用"); | |
| 35 | 36 | |
| 36 | 37 | // ========== 用户模块 1002004000 ========== |
| 37 | 38 | ErrorCode USER_USERNAME_EXISTS = new ErrorCode(1002004000, "用户账号已经存在"); | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/enums/permission/RoleCodeEnum.java
| 1 | 1 | package cn.iocoder.yudao.module.system.enums.permission; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | |
| 3 | 4 | import lombok.AllArgsConstructor; |
| 4 | 5 | import lombok.Getter; |
| 5 | 6 | |
| ... | ... | @@ -23,4 +24,8 @@ public enum RoleCodeEnum { |
| 23 | 24 | */ |
| 24 | 25 | private final String name; |
| 25 | 26 | |
| 27 | + public static boolean isSuperAdmin(String code) { | |
| 28 | + return ObjectUtils.equalsAny(code, SUPER_ADMIN.getCode()); | |
| 29 | + } | |
| 30 | + | |
| 26 | 31 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java
| ... | ... | @@ -136,7 +136,7 @@ public class RoleServiceImpl implements RoleService { |
| 136 | 136 | @Override |
| 137 | 137 | public void updateRole(RoleUpdateReqVO reqVO) { |
| 138 | 138 | // 校验是否可以更新 |
| 139 | - this.checkUpdateRole(reqVO.getId()); | |
| 139 | + checkUpdateRole(reqVO.getId()); | |
| 140 | 140 | // 校验角色的唯一字段是否重复 |
| 141 | 141 | checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); |
| 142 | 142 | // 更新到数据库 |
| ... | ... | @@ -149,7 +149,7 @@ public class RoleServiceImpl implements RoleService { |
| 149 | 149 | @Override |
| 150 | 150 | public void updateRoleStatus(Long id, Integer status) { |
| 151 | 151 | // 校验是否可以更新 |
| 152 | - this.checkUpdateRole(id); | |
| 152 | + checkUpdateRole(id); | |
| 153 | 153 | // 更新状态 |
| 154 | 154 | RoleDO updateObject = new RoleDO(); |
| 155 | 155 | updateObject.setId(id); |
| ... | ... | @@ -217,7 +217,7 @@ public class RoleServiceImpl implements RoleService { |
| 217 | 217 | if (CollectionUtil.isEmpty(roleList)) { |
| 218 | 218 | return false; |
| 219 | 219 | } |
| 220 | - return roleList.stream().anyMatch(roleDO -> RoleCodeEnum.SUPER_ADMIN.getCode().equals(roleDO.getCode())); | |
| 220 | + return roleList.stream().anyMatch(role -> RoleCodeEnum.isSuperAdmin(role.getCode())); | |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | @Override |
| ... | ... | @@ -247,6 +247,10 @@ public class RoleServiceImpl implements RoleService { |
| 247 | 247 | */ |
| 248 | 248 | @VisibleForTesting |
| 249 | 249 | public void checkDuplicateRole(String name, String code, Long id) { |
| 250 | + // 0. 超级管理员,不允许创建 | |
| 251 | + if (RoleCodeEnum.isSuperAdmin(code)) { | |
| 252 | + throw exception(ROLE_ADMIN_CODE_ERROR, code); | |
| 253 | + } | |
| 250 | 254 | // 1. 该 name 名字被其它角色所使用 |
| 251 | 255 | RoleDO role = roleMapper.selectByName(name); |
| 252 | 256 | if (role != null && !role.getId().equals(id)) { | ... | ... |