疯狂的狮子li
2021-11-01 c4e17ff8472fd9f8123e86b593b2968ad5936b15
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java
@@ -4,10 +4,12 @@
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.SysRoleDept;
@@ -27,7 +29,7 @@
/**
 * 角色 业务层处理
 *
 * @author ruoyi
 * @author Lion Li
 */
@Service
public class SysRoleServiceImpl extends ServicePlusImpl<SysRoleMapper, SysRole, SysRole> implements ISysRoleService {
@@ -115,7 +117,7 @@
     * @return 选中角色ID列表
     */
    @Override
    public List<Integer> selectRoleListByUserId(Long userId) {
    public List<Long> selectRoleListByUserId(Long userId) {
        return baseMapper.selectRoleListByUserId(userId);
    }
@@ -172,7 +174,24 @@
    @Override
    public void checkRoleAllowed(SysRole role) {
        if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) {
            throw new CustomException("不允许操作超级管理员角色");
            throw new ServiceException("不允许操作超级管理员角色");
        }
    }
    /**
     * 校验角色是否有数据权限
     *
     * @param roleId 角色id
     */
    @Override
    public void checkRoleDataScope(Long roleId) {
        if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
            SysRole role = new SysRole();
            role.setRoleId(roleId);
            List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
            if (StringUtils.isEmpty(roles)) {
                throw new ServiceException("没有权限访问角色数据!");
            }
        }
    }
@@ -183,7 +202,7 @@
     * @return 结果
     */
    @Override
    public int countUserRoleByRoleId(Long roleId) {
    public long countUserRoleByRoleId(Long roleId) {
        return userRoleMapper.selectCount(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, roleId));
    }
@@ -261,7 +280,7 @@
            list.add(rm);
        }
        if (list.size() > 0) {
         rows = roleMenuMapper.insertAll(list);
            rows = roleMenuMapper.insertAll(list);
        }
        return rows;
    }
@@ -282,7 +301,7 @@
            list.add(rd);
        }
        if (list.size() > 0) {
         rows = roleDeptMapper.insertAll(list);
            rows = roleDeptMapper.insertAll(list);
        }
        return rows;
    }
@@ -316,7 +335,7 @@
            checkRoleAllowed(new SysRole(roleId));
            SysRole role = selectRoleById(roleId);
            if (countUserRoleByRoleId(roleId) > 0) {
                throw new CustomException(String.format("%1$s已分配,不能删除", role.getRoleName()));
                throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName()));
            }
        }
        List<Long> ids = Arrays.asList(roleIds);
@@ -336,45 +355,45 @@
    @Override
    public int deleteAuthUser(SysUserRole userRole) {
        return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
         .eq(SysUserRole::getRoleId, userRole.getRoleId())
         .eq(SysUserRole::getUserId, userRole.getUserId()));
                .eq(SysUserRole::getRoleId, userRole.getRoleId())
                .eq(SysUserRole::getUserId, userRole.getUserId()));
    }
    /**
     * 批量取消授权用户角色
     *
     * @param roleId 角色ID
     * @param roleId  角色ID
     * @param userIds 需要取消授权的用户数据ID
     * @return 结果
     */
    @Override
    public int deleteAuthUsers(Long roleId, Long[] userIds) {
      return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
         .eq(SysUserRole::getRoleId, roleId)
         .in(SysUserRole::getUserId, Arrays.asList(userIds)));
        return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getRoleId, roleId)
                .in(SysUserRole::getUserId, Arrays.asList(userIds)));
    }
    /**
     * 批量选择授权用户角色
     *
     * @param roleId 角色ID
     * @param roleId  角色ID
     * @param userIds 需要删除的用户数据ID
     * @return 结果
     */
    @Override
    public int insertAuthUsers(Long roleId, Long[] userIds) {
        // 新增用户与角色管理
      int rows = 1;
      List<SysUserRole> list = new ArrayList<SysUserRole>();
        int rows = 1;
        List<SysUserRole> list = new ArrayList<SysUserRole>();
        for (Long userId : userIds) {
            SysUserRole ur = new SysUserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
        }
      if (list.size() > 0) {
         rows = userRoleMapper.insertAll(list);
      }
        if (list.size() > 0) {
            rows = userRoleMapper.insertAll(list);
        }
        return rows;
    }
}