2021与蓝度共同重构项目,服务端
chenjiantian
2022-01-18 a98228c5e829bff75969b51a37652d7dbd6bee7e
ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java
@@ -4,6 +4,7 @@
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.enums.RoleLevelStatus;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.execption.EntityExistException;
import com.sandu.common.service.impl.BaseServiceImpl;
@@ -83,7 +84,7 @@
        Role role = new Role();
        role.setName(param.getName());
        role.setRemark(param.getRemark());
        role.setLevel(2);
        role.setLevel(RoleLevelStatus.COMMON.getCode());
        if(!save(role)){
            throw new BusinessException("添加角色失败");
        }
@@ -101,6 +102,7 @@
        }
        assertLevels(role.getLevel());
        Role update = new Role();
        update.setId(roleId);
        update.setName(param.getName());
        update.setRemark(param.getRemark());
        if(!updateById(update)){
@@ -113,8 +115,12 @@
    }
    private int assertLevels(Integer level) {
    /**
     * 判断当前角色能不能操作目标用户 不行的话直接抛出异常
     * @param roleLevel 目标用户的最大角色
     * @return
     */
    public int assertLevels(Integer roleLevel) {
        Long userId = SecurityUtils.getUserId();
        List<Role> roles = listByAdminId(userId);
        if(CollectionUtil.isEmpty(roles)){
@@ -122,9 +128,9 @@
        }
        List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList());
        int min = Collections.min(levels);
        if (level != null) {
            if (level < min) {
                throw new BusinessException("权限不足,你的角色级别:" + min + ",低于操作的角色级别:" + level);
        if (roleLevel != null) {
            if (roleLevel < min) {
                throw new BusinessException("权限不足,你的角色级别:" + min + ",低于操作的角色级别:" + roleLevel);
            }
        }
        return min;