2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-03-01 b6d1d79b162d297cc81e1ddd315ddda41e01e49c
fix
已修改4个文件
43 ■■■■ 文件已修改
dao/src/main/java/com/sandu/ximon/dao/mapper/MenuMapper.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
dao/src/main/java/com/sandu/ximon/dao/mapper/RoleMapper.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
dao/src/main/resources/mapper/MenuMapper.xml 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
dao/src/main/java/com/sandu/ximon/dao/mapper/MenuMapper.java
@@ -4,12 +4,15 @@
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
 * @Entity com.sandu.ximon.dao.domain.Menu
 */
@Mapper
public interface MenuMapper extends BaseMapper<Menu> {
    List<Long> listMenu();
}
dao/src/main/java/com/sandu/ximon/dao/mapper/RoleMapper.java
@@ -1,5 +1,6 @@
package com.sandu.ximon.dao.mapper;
import com.sandu.ximon.dao.domain.Menu;
import com.sandu.ximon.dao.domain.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sandu.ximon.dao.domain.RoleDetail;
dao/src/main/resources/mapper/MenuMapper.xml
@@ -27,4 +27,7 @@
        permission,type,create_time,
        update_time
    </sql>
    <select id="listMenu" resultType="java.lang.Long">
        SELECT id FROM menu
    </select>
</mapper>
ximon-admin/src/main/java/com/sandu/ximon/admin/service/RoleService.java
@@ -12,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;
@@ -33,6 +34,8 @@
    private final AdminRoleRelationService adminRoleRelationService;
    private final RoleMenuRelationService roleMenuRelationService;
    private final MenuService menuService;
    private final MenuMapper menuMapper;
    /**
     * 获取指定管理员的spring security权限认证列表
@@ -81,18 +84,18 @@
    @Transactional(rollbackFor = Exception.class)
    public boolean addRole(RoleParam param) {
        if(param.getMenuIdList().isEmpty()){
        if (param.getMenuIdList().isEmpty()) {
            throw new BusinessException("权限参数异常");
        }
        Role role = new Role();
        role.setName(param.getName());
        role.setRemark(param.getRemark());
        role.setLevel(RoleLevelStatus.COMMON.getCode());
        if(!save(role)){
        if (!save(role)) {
            throw new BusinessException("添加角色失败");
        }
        roleMenuRelationService.addRoleMenuList(role.getId(),param.getMenuIdList());
        roleMenuRelationService.addRoleMenuList(role.getId(), param.getMenuIdList());
        return true;
    }
@@ -108,25 +111,26 @@
        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;
    }
    /**
     * 判断当前角色能不能操作目标用户 不行的话直接抛出异常
     *
     * @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());
@@ -146,19 +150,29 @@
        }
        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;
    }
}