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,7 +31,8 @@ public interface ErrorCodeConstants { | ||
| 31 | ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1002003001, "已经存在名为【{}】的角色"); | 31 | ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1002003001, "已经存在名为【{}】的角色"); |
| 32 | ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1002003002, "已经存在编码为【{}】的角色"); | 32 | ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1002003002, "已经存在编码为【{}】的角色"); |
| 33 | ErrorCode ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE = new ErrorCode(1002003004, "不能操作类型为系统内置的角色"); | 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 | // ========== 用户模块 1002004000 ========== | 37 | // ========== 用户模块 1002004000 ========== |
| 37 | ErrorCode USER_USERNAME_EXISTS = new ErrorCode(1002004000, "用户账号已经存在"); | 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 | package cn.iocoder.yudao.module.system.enums.permission; | 1 | package cn.iocoder.yudao.module.system.enums.permission; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||
| 3 | import lombok.AllArgsConstructor; | 4 | import lombok.AllArgsConstructor; |
| 4 | import lombok.Getter; | 5 | import lombok.Getter; |
| 5 | 6 | ||
| @@ -23,4 +24,8 @@ public enum RoleCodeEnum { | @@ -23,4 +24,8 @@ public enum RoleCodeEnum { | ||
| 23 | */ | 24 | */ |
| 24 | private final String name; | 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,7 +136,7 @@ public class RoleServiceImpl implements RoleService { | ||
| 136 | @Override | 136 | @Override |
| 137 | public void updateRole(RoleUpdateReqVO reqVO) { | 137 | public void updateRole(RoleUpdateReqVO reqVO) { |
| 138 | // 校验是否可以更新 | 138 | // 校验是否可以更新 |
| 139 | - this.checkUpdateRole(reqVO.getId()); | 139 | + checkUpdateRole(reqVO.getId()); |
| 140 | // 校验角色的唯一字段是否重复 | 140 | // 校验角色的唯一字段是否重复 |
| 141 | checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); | 141 | checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); |
| 142 | // 更新到数据库 | 142 | // 更新到数据库 |
| @@ -149,7 +149,7 @@ public class RoleServiceImpl implements RoleService { | @@ -149,7 +149,7 @@ public class RoleServiceImpl implements RoleService { | ||
| 149 | @Override | 149 | @Override |
| 150 | public void updateRoleStatus(Long id, Integer status) { | 150 | public void updateRoleStatus(Long id, Integer status) { |
| 151 | // 校验是否可以更新 | 151 | // 校验是否可以更新 |
| 152 | - this.checkUpdateRole(id); | 152 | + checkUpdateRole(id); |
| 153 | // 更新状态 | 153 | // 更新状态 |
| 154 | RoleDO updateObject = new RoleDO(); | 154 | RoleDO updateObject = new RoleDO(); |
| 155 | updateObject.setId(id); | 155 | updateObject.setId(id); |
| @@ -217,7 +217,7 @@ public class RoleServiceImpl implements RoleService { | @@ -217,7 +217,7 @@ public class RoleServiceImpl implements RoleService { | ||
| 217 | if (CollectionUtil.isEmpty(roleList)) { | 217 | if (CollectionUtil.isEmpty(roleList)) { |
| 218 | return false; | 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 | @Override | 223 | @Override |
| @@ -247,6 +247,10 @@ public class RoleServiceImpl implements RoleService { | @@ -247,6 +247,10 @@ public class RoleServiceImpl implements RoleService { | ||
| 247 | */ | 247 | */ |
| 248 | @VisibleForTesting | 248 | @VisibleForTesting |
| 249 | public void checkDuplicateRole(String name, String code, Long id) { | 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 | // 1. 该 name 名字被其它角色所使用 | 254 | // 1. 该 name 名字被其它角色所使用 |
| 251 | RoleDO role = roleMapper.selectByName(name); | 255 | RoleDO role = roleMapper.selectByName(name); |
| 252 | if (role != null && !role.getId().equals(id)) { | 256 | if (role != null && !role.getId().equals(id)) { |