接口调整
This commit is contained in:
@@ -82,7 +82,7 @@ public class MenuController {
|
||||
return success(BeanUtils.toBean(list, MenuRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping({"/list-all-simple", "simple-list"})
|
||||
@GetMapping("/simple-list")
|
||||
@Operation(summary = "获取菜单精简信息列表", description = "只包含已启用的菜单,用于【角色分配菜单】功能的选项")
|
||||
public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
|
||||
List<MenuDO> list = menuService.getMenuList(new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||
|
||||
@@ -84,11 +84,12 @@ public class RoleController {
|
||||
@Operation(summary = "获得角色分页")
|
||||
@PreAuthorize("@ss.hasPermission('system:role:query')")
|
||||
public CommonResult<PageResult<RoleRespVO>> getRolePage(RolePageReqVO pageReqVO) {
|
||||
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
||||
PageResult<RoleDO> pageResult = roleService.getRolePage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, RoleRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping({"/list-all-simple", "/simple-list"})
|
||||
@GetMapping("/simple-list")
|
||||
@Operation(summary = "获取角色精简信息列表", description = "只包含被开启的角色,主要用于前端的下拉选项")
|
||||
public CommonResult<List<RoleRespVO>> getSimpleRoleList() {
|
||||
List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.njcn.rdms.module.system.controller.admin.permission.vo.role.RolePageR
|
||||
import com.njcn.rdms.module.system.dal.dataobject.permission.RoleDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -16,12 +17,21 @@ import java.util.List;
|
||||
public interface RoleMapper extends BaseMapperX<RoleDO> {
|
||||
|
||||
default PageResult<RoleDO> selectPage(RolePageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<RoleDO>()
|
||||
.likeIfPresent(RoleDO::getName, reqVO.getName())
|
||||
.likeIfPresent(RoleDO::getCode, reqVO.getCode())
|
||||
.eqIfPresent(RoleDO::getStatus, reqVO.getStatus())
|
||||
LambdaQueryWrapperX<RoleDO> queryWrapper = new LambdaQueryWrapperX<>();
|
||||
boolean hasName = StringUtils.hasText(reqVO.getName());
|
||||
boolean hasCode = StringUtils.hasText(reqVO.getCode());
|
||||
if (hasName && hasCode) {
|
||||
queryWrapper.and(wrapper -> wrapper.like(RoleDO::getName, reqVO.getName())
|
||||
.or()
|
||||
.like(RoleDO::getCode, reqVO.getCode()));
|
||||
} else {
|
||||
queryWrapper.likeIfPresent(RoleDO::getName, reqVO.getName())
|
||||
.likeIfPresent(RoleDO::getCode, reqVO.getCode());
|
||||
}
|
||||
queryWrapper.eqIfPresent(RoleDO::getStatus, reqVO.getStatus())
|
||||
.betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByAsc(RoleDO::getSort));
|
||||
.orderByAsc(RoleDO::getSort);
|
||||
return selectPage(reqVO, queryWrapper);
|
||||
}
|
||||
|
||||
default RoleDO selectByName(String name) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.njcn.rdms.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.njcn.rdms.module.system.dal.mysql.oauth2.OAuth2AccessTokenMapper;
|
||||
import com.njcn.rdms.module.system.dal.mysql.oauth2.OAuth2RefreshTokenMapper;
|
||||
import com.njcn.rdms.module.system.dal.redis.oauth2.OAuth2AccessTokenRedisDAO;
|
||||
import com.njcn.rdms.module.system.enums.ErrorCodeConstants;
|
||||
import com.njcn.rdms.module.system.service.user.AdminUserService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@@ -33,6 +34,7 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.exception0;
|
||||
import static com.njcn.rdms.framework.common.util.collection.CollectionUtils.convertSet;
|
||||
|
||||
@@ -93,7 +95,7 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
|
||||
// 已过期的情况下,删除刷新令牌
|
||||
if (DateUtils.isExpired(refreshTokenDO.getExpiresTime())) {
|
||||
oauth2RefreshTokenMapper.deleteById(refreshTokenDO.getId());
|
||||
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "刷新令牌已过期");
|
||||
throw exception(ErrorCodeConstants.OAUTH2_REFRESH_TOKEN_EXPIRE);
|
||||
}
|
||||
|
||||
// 创建访问令牌
|
||||
@@ -134,7 +136,7 @@ public class OAuth2TokenServiceImpl implements OAuth2TokenService {
|
||||
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌不存在");
|
||||
}
|
||||
if (DateUtils.isExpired(accessTokenDO.getExpiresTime())) {
|
||||
throw exception0(GlobalErrorCodeConstants.UNAUTHORIZED.getCode(), "访问令牌已过期");
|
||||
throw exception(ErrorCodeConstants.OAUTH2_ACCESS_TOKEN_EXPIRE);
|
||||
}
|
||||
return accessTokenDO;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.mzt.logapi.context.LogRecordContext;
|
||||
import com.mzt.logapi.service.impl.DiffParseFunction;
|
||||
import com.mzt.logapi.starter.annotation.LogRecord;
|
||||
import com.njcn.rdms.framework.common.enums.CommonStatusEnum;
|
||||
import com.njcn.rdms.framework.common.pojo.PageResult;
|
||||
import com.njcn.rdms.framework.common.util.collection.CollectionUtils;
|
||||
@@ -16,10 +20,6 @@ import com.njcn.rdms.module.system.dal.mysql.permission.RoleMapper;
|
||||
import com.njcn.rdms.module.system.dal.redis.RedisKeyConstants;
|
||||
import com.njcn.rdms.module.system.enums.permission.RoleCodeEnum;
|
||||
import com.njcn.rdms.module.system.enums.permission.RoleTypeEnum;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.mzt.logapi.context.LogRecordContext;
|
||||
import com.mzt.logapi.service.impl.DiffParseFunction;
|
||||
import com.mzt.logapi.starter.annotation.LogRecord;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
@@ -35,14 +35,20 @@ import java.util.Map;
|
||||
|
||||
import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static com.njcn.rdms.framework.common.util.collection.CollectionUtils.convertMap;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.*;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.*;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.ROLE_ADMIN_CODE_ERROR;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.ROLE_CODE_DUPLICATE;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.ROLE_IS_DISABLE;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.ROLE_NAME_DUPLICATE;
|
||||
import static com.njcn.rdms.module.system.enums.ErrorCodeConstants.ROLE_NOT_EXISTS;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_ROLE_CREATE_SUB_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_ROLE_CREATE_SUCCESS;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_ROLE_DELETE_SUB_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_ROLE_DELETE_SUCCESS;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_ROLE_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_ROLE_UPDATE_SUB_TYPE;
|
||||
import static com.njcn.rdms.module.system.enums.LogRecordConstants.SYSTEM_ROLE_UPDATE_SUCCESS;
|
||||
|
||||
/**
|
||||
* 角色 Service 实现类
|
||||
*
|
||||
* @author hongawen
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class RoleServiceImpl implements RoleService {
|
||||
@@ -58,16 +64,13 @@ public class RoleServiceImpl implements RoleService {
|
||||
@LogRecord(type = SYSTEM_ROLE_TYPE, subType = SYSTEM_ROLE_CREATE_SUB_TYPE, bizNo = "{{#role.id}}",
|
||||
success = SYSTEM_ROLE_CREATE_SUCCESS)
|
||||
public Long createRole(RoleSaveReqVO createReqVO, Integer type) {
|
||||
// 1. 校验角色
|
||||
validateRoleDuplicate(createReqVO.getName(), createReqVO.getCode(), null);
|
||||
|
||||
// 2. 插入到数据库
|
||||
RoleDO role = BeanUtils.toBean(createReqVO, RoleDO.class)
|
||||
.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType()))
|
||||
.setStatus(ObjUtil.defaultIfNull(createReqVO.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
|
||||
roleMapper.insert(role);
|
||||
|
||||
// 3. 记录操作日志上下文
|
||||
LogRecordContext.putVariable("role", role);
|
||||
return role.getId();
|
||||
}
|
||||
@@ -77,16 +80,16 @@ public class RoleServiceImpl implements RoleService {
|
||||
@LogRecord(type = SYSTEM_ROLE_TYPE, subType = SYSTEM_ROLE_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}",
|
||||
success = SYSTEM_ROLE_UPDATE_SUCCESS)
|
||||
public void updateRole(RoleSaveReqVO updateReqVO) {
|
||||
// 1.1 校验是否可以更新
|
||||
RoleDO role = validateRoleForUpdate(updateReqVO.getId());
|
||||
// 1.2 校验角色的唯一字段是否重复
|
||||
validateRoleDuplicate(updateReqVO.getName(), updateReqVO.getCode(), updateReqVO.getId());
|
||||
RoleDO role = validateRoleExists(updateReqVO.getId());
|
||||
String effectiveCode = shouldPreserveBuiltInCode(role) ? role.getCode() : updateReqVO.getCode();
|
||||
validateRoleDuplicate(updateReqVO.getName(), effectiveCode, updateReqVO.getId());
|
||||
|
||||
// 2. 更新到数据库
|
||||
RoleDO updateObj = BeanUtils.toBean(updateReqVO, RoleDO.class);
|
||||
if (shouldPreserveBuiltInCode(role)) {
|
||||
updateObj.setCode(role.getCode());
|
||||
}
|
||||
roleMapper.updateById(updateObj);
|
||||
|
||||
// 3. 记录操作日志上下文
|
||||
LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(role, RoleSaveReqVO.class));
|
||||
LogRecordContext.putVariable("role", role);
|
||||
}
|
||||
@@ -97,15 +100,11 @@ public class RoleServiceImpl implements RoleService {
|
||||
@LogRecord(type = SYSTEM_ROLE_TYPE, subType = SYSTEM_ROLE_DELETE_SUB_TYPE, bizNo = "{{#id}}",
|
||||
success = SYSTEM_ROLE_DELETE_SUCCESS)
|
||||
public void deleteRole(Long id) {
|
||||
// 1. 校验是否可以更新
|
||||
RoleDO role = validateRoleForUpdate(id);
|
||||
RoleDO role = validateRoleForDelete(id);
|
||||
|
||||
// 2.1 标记删除
|
||||
roleMapper.deleteById(id);
|
||||
// 2.2 删除相关数据
|
||||
permissionService.processRoleDeleted(id);
|
||||
|
||||
// 3. 记录操作日志上下文
|
||||
LogRecordContext.putVariable("role", role);
|
||||
}
|
||||
|
||||
@@ -113,78 +112,73 @@ public class RoleServiceImpl implements RoleService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@CacheEvict(value = RedisKeyConstants.ROLE, allEntries = true)
|
||||
public void deleteRoleList(List<Long> ids) {
|
||||
// 1. 校验是否可以删除
|
||||
ids.forEach(this::validateRoleForUpdate);
|
||||
ids.forEach(this::validateRoleForDelete);
|
||||
|
||||
// 2.1 标记删除
|
||||
roleMapper.deleteByIds(ids);
|
||||
// 2.2 删除相关数据
|
||||
ids.forEach(id -> permissionService.processRoleDeleted(id));
|
||||
ids.forEach(permissionService::processRoleDeleted);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验角色的唯一字段是否重复
|
||||
*
|
||||
* 1. 是否存在相同名字的角色
|
||||
* 2. 是否存在相同编码的角色
|
||||
*
|
||||
* @param name 角色名字
|
||||
* @param code 角色额编码
|
||||
* @param id 角色编号
|
||||
*/
|
||||
@VisibleForTesting
|
||||
void validateRoleDuplicate(String name, String code, Long id) {
|
||||
// 0. 超级管理员,不允许创建
|
||||
if (RoleCodeEnum.isSuperAdmin(code)) {
|
||||
throw exception(ROLE_ADMIN_CODE_ERROR, code);
|
||||
if (RoleCodeEnum.isBuiltIn(code)) {
|
||||
if (id == null) {
|
||||
throw exception(ROLE_ADMIN_CODE_ERROR, code);
|
||||
}
|
||||
RoleDO currentRole = roleMapper.selectById(id);
|
||||
if (currentRole == null || !ObjUtil.equal(currentRole.getCode(), code)) {
|
||||
throw exception(ROLE_ADMIN_CODE_ERROR, code);
|
||||
}
|
||||
}
|
||||
// 1. 该 name 名字被其它角色所使用
|
||||
|
||||
RoleDO role = roleMapper.selectByName(name);
|
||||
if (role != null && !role.getId().equals(id)) {
|
||||
throw exception(ROLE_NAME_DUPLICATE, name);
|
||||
}
|
||||
// 2. 是否存在相同编码的角色
|
||||
|
||||
if (!StringUtils.hasText(code)) {
|
||||
return;
|
||||
}
|
||||
// 该 code 编码被其它角色所使用
|
||||
role = roleMapper.selectByCode(code);
|
||||
if (role != null && !role.getId().equals(id)) {
|
||||
throw exception(ROLE_CODE_DUPLICATE, code);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验角色是否可以被更新
|
||||
*
|
||||
* @param id 角色编号
|
||||
*/
|
||||
@VisibleForTesting
|
||||
RoleDO validateRoleForUpdate(Long id) {
|
||||
RoleDO validateRoleExists(Long id) {
|
||||
RoleDO role = roleMapper.selectById(id);
|
||||
if (role == null) {
|
||||
throw exception(ROLE_NOT_EXISTS);
|
||||
}
|
||||
// 内置角色,不允许删除
|
||||
return role;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
RoleDO validateRoleForDelete(Long id) {
|
||||
RoleDO role = validateRoleExists(id);
|
||||
if (RoleTypeEnum.SYSTEM.getType().equals(role.getType())) {
|
||||
throw exception(ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
|
||||
throw exception(ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE);
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
private boolean shouldPreserveBuiltInCode(RoleDO role) {
|
||||
return role != null
|
||||
&& RoleTypeEnum.SYSTEM.getType().equals(role.getType())
|
||||
&& RoleCodeEnum.isBuiltIn(role.getCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public RoleDO getRole(Long id) {
|
||||
return roleMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(value = RedisKeyConstants.ROLE, key = "#id",
|
||||
unless = "#result == null")
|
||||
@Cacheable(value = RedisKeyConstants.ROLE, key = "#id", unless = "#result == null")
|
||||
public RoleDO getRoleFromCache(Long id) {
|
||||
return roleMapper.selectById(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<RoleDO> getRoleListByStatus(Collection<Integer> statuses) {
|
||||
return roleMapper.selectListByStatus(statuses);
|
||||
@@ -208,7 +202,6 @@ public class RoleServiceImpl implements RoleService {
|
||||
if (CollectionUtil.isEmpty(ids)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 这里采用 for 循环从缓存中获取,主要考虑 Spring CacheManager 无法批量操作的问题
|
||||
RoleServiceImpl self = getSelf();
|
||||
return CollectionUtils.convertList(ids, self::getRoleFromCache);
|
||||
}
|
||||
@@ -235,10 +228,8 @@ public class RoleServiceImpl implements RoleService {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return;
|
||||
}
|
||||
// 获得角色信息
|
||||
List<RoleDO> roles = roleMapper.selectByIds(ids);
|
||||
Map<Long, RoleDO> roleMap = convertMap(roles, RoleDO::getId);
|
||||
// 校验
|
||||
ids.forEach(id -> {
|
||||
RoleDO role = roleMap.get(id);
|
||||
if (role == null) {
|
||||
@@ -250,11 +241,6 @@ public class RoleServiceImpl implements RoleService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得自身的代理对象,解决 AOP 生效问题
|
||||
*
|
||||
* @return 自己
|
||||
*/
|
||||
private RoleServiceImpl getSelf() {
|
||||
return SpringUtil.getBean(getClass());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user