2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-03-09 7477ef6ab4e6aa1189424c5bd3af6f12ae591e88
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;
@@ -11,6 +12,7 @@
import com.sandu.ximon.admin.param.RoleParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.dao.domain.*;
import com.sandu.ximon.dao.mapper.MenuMapper;
import com.sandu.ximon.dao.mapper.RoleMapper;
import lombok.AllArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
@@ -30,16 +32,23 @@
public class RoleService extends BaseServiceImpl<RoleMapper, Role> {
    private final AdminRoleRelationService adminRoleRelationService;
    private final ClientRoleRelationService clientRoleRelationService;
    private final RoleMenuRelationService roleMenuRelationService;
    private final MenuService menuService;
    private final MenuMapper menuMapper;
    /**
     * 获取指定管理员的spring security权限认证列表
     */
    public Collection<GrantedAuthority> mapToGrantedAuthorities(Long adminId) {
        // 获取管理员的角色id
        List<Role> roles = listByAdminId(adminId);
    public Collection<GrantedAuthority> mapToGrantedAuthorities(Long adminId, boolean flag) {
        List<Role> roles;
        if (flag) {
            // 获取管理员的角色id
            roles = listByAdminId(adminId);
        } else {
            roles = listByClientId(adminId);
        }
        if (CollectionUtil.isEmpty(roles)) {
            throw new BusinessException("当前用户没有角色");
@@ -78,17 +87,33 @@
        return listByIds(roleIdList);
    }
    /**
     * 获取某个普通用户的角色列表
     */
    public List<Role> listByClientId(Long clientId) {
        // 获取普通用户的角色id
        List<ClientRoleRelation> list = clientRoleRelationService.list(Wrappers.lambdaQuery(ClientRoleRelation.class).eq(ClientRoleRelation::getClientId, clientId).select(ClientRoleRelation::getRoleId));
        List<Long> roleIdList = list.stream().map(ClientRoleRelation::getRoleId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(roleIdList)) {
            return null;
        }
        return listByIds(roleIdList);
    }
    @Transactional(rollbackFor = Exception.class)
    public boolean addRole(RoleParam param) {
        if (param.getMenuIdList().isEmpty()) {
            throw new BusinessException("权限参数异常");
        }
        Role role = new Role();
        role.setName(param.getName());
        role.setRemark(param.getRemark());
        role.setLevel(2);
        if(!save(role)){
        role.setLevel(RoleLevelStatus.COMMON.getCode());
        if (!save(role)) {
            throw new BusinessException("添加角色失败");
        }
        roleMenuRelationService.addRoleMenuList(role.getId(),param.getMenuIdList());
        roleMenuRelationService.addRoleMenuList(role.getId(), param.getMenuIdList());
        return true;
    }
@@ -101,30 +126,36 @@
        }
        assertLevels(role.getLevel());
        Role update = new Role();
        update.setId(roleId);
        update.setName(param.getName());
        update.setRemark(param.getRemark());
        if(!updateById(update)){
        if (!updateById(update)) {
            throw new BusinessException("编辑角色失败");
        }
        roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId,roleId));
        roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, roleId));
        roleMenuRelationService.addRoleMenuList(role.getId(),param.getMenuIdList());
        roleMenuRelationService.addRoleMenuList(role.getId(), param.getMenuIdList());
        return true;
    }
    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)){
        if (CollectionUtil.isEmpty(roles)) {
            throw new BusinessException("当前用户没有角色");
        }
        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;
@@ -135,21 +166,36 @@
        if (role == null) {
            throw new BusinessException("找不到角色");
        }
        //超级管理员不能删除
        if (RoleLevelStatus.SUPER.getCode().equals(role.getLevel())) {
            throw new BusinessException("当前角色为超级管理员,无法删除");
        }
        assertLevels(role.getLevel());
        List<AdminRoleRelation> list = adminRoleRelationService.list(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getRoleId, role));
        if(CollectionUtil.isNotEmpty(list)){
        if (CollectionUtil.isNotEmpty(list)) {
            throw new BusinessException("当前角色有管理员使用,无法删除");
        }
        roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId,roleId));
        roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, roleId));
        return removeById(roleId);
    }
    public List<RoleDetail> listRole(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Long> roleIdList = list().stream().map(Role::getId).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(roleIdList)){
        if (CollectionUtil.isEmpty(roleIdList)) {
            return null;
        }
        return baseMapper.listRole(roleIdList);
        List<RoleDetail> list = baseMapper.listRole(roleIdList);
        for (RoleDetail roleDetail : list) {
            if (roleDetail.getId() == 1) {
                roleDetail.setMenuIdList(menuMapper.listMenu());
                break;
            }
        }
        return list;
    }
}