| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import com.ruoyi.common.basic.PageInfo; |
| | | import com.ruoyi.common.core.domain.entity.SysMenu; |
| | | import com.ruoyi.system.dto.SysRoleDTO; |
| | | import com.ruoyi.system.mapper.*; |
| | | import com.ruoyi.system.query.SysRoleQuery; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.entity.SysMenu; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | |
| | | import com.ruoyi.system.domain.SysRoleDept; |
| | | import com.ruoyi.system.domain.SysRoleMenu; |
| | | import com.ruoyi.system.domain.SysUserRole; |
| | | import com.ruoyi.system.dto.SysRoleDTO; |
| | | import com.ruoyi.system.mapper.SysMenuMapper; |
| | | import com.ruoyi.system.mapper.SysRoleDeptMapper; |
| | | import com.ruoyi.system.mapper.SysRoleMapper; |
| | | import com.ruoyi.system.mapper.SysRoleMenuMapper; |
| | | import com.ruoyi.system.mapper.SysUserRoleMapper; |
| | | import com.ruoyi.system.query.SysRoleQuery; |
| | | import com.ruoyi.system.service.ISysRoleService; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | /** |
| | | * 角色 业务层处理 |
| | |
| | | * @author ruoyi |
| | | */ |
| | | @Service |
| | | public class SysRoleServiceImpl implements ISysRoleService |
| | | { |
| | | public class SysRoleServiceImpl implements ISysRoleService { |
| | | |
| | | @Autowired |
| | | private SysRoleMapper roleMapper; |
| | | |
| | |
| | | * @return 角色数据集合信息 |
| | | */ |
| | | @Override |
| | | public List<SysRole> selectRoleList(SysRole role) |
| | | { |
| | | public List<SysRole> selectRoleList(SysRole role) { |
| | | return roleMapper.selectRoleList(role); |
| | | } |
| | | |
| | |
| | | * @return 角色列表 |
| | | */ |
| | | @Override |
| | | public List<SysRole> selectRolesByUserId(Long userId) |
| | | { |
| | | public List<SysRole> selectRolesByUserId(Long userId) { |
| | | List<SysRole> userRoles = roleMapper.selectRolePermissionByUserId(userId); |
| | | List<SysRole> roles = selectRoleAll(); |
| | | for (SysRole role : roles) |
| | | { |
| | | for (SysRole userRole : userRoles) |
| | | { |
| | | if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) |
| | | { |
| | | for (SysRole role : roles) { |
| | | for (SysRole userRole : userRoles) { |
| | | if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) { |
| | | role.setFlag(true); |
| | | break; |
| | | } |
| | |
| | | * @return 权限列表 |
| | | */ |
| | | @Override |
| | | public Set<String> selectRolePermissionByUserId(Long userId) |
| | | { |
| | | public Set<String> selectRolePermissionByUserId(Long userId) { |
| | | List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId); |
| | | Set<String> permsSet = new HashSet<>(); |
| | | for (SysRole perm : perms) |
| | | { |
| | | if (StringUtils.isNotNull(perm)) |
| | | { |
| | | for (SysRole perm : perms) { |
| | | if (StringUtils.isNotNull(perm)) { |
| | | permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(","))); |
| | | } |
| | | } |
| | |
| | | * @return 角色列表 |
| | | */ |
| | | @Override |
| | | public List<SysRole> selectRoleAll() |
| | | { |
| | | public List<SysRole> selectRoleAll() { |
| | | return this.selectRoleList(new SysRole()); |
| | | } |
| | | |
| | |
| | | * @return 选中角色ID列表 |
| | | */ |
| | | @Override |
| | | public List<Long> selectRoleListByUserId(Long userId) |
| | | { |
| | | public List<Long> selectRoleListByUserId(Long userId) { |
| | | return roleMapper.selectRoleListByUserId(userId); |
| | | } |
| | | |
| | |
| | | * @return 角色对象信息 |
| | | */ |
| | | @Override |
| | | public SysRole selectRoleById(Long roleId) |
| | | { |
| | | public SysRole selectRoleById(Long roleId) { |
| | | return roleMapper.selectRoleById(roleId); |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public boolean checkRoleNameUnique(SysRole role) |
| | | { |
| | | public boolean checkRoleNameUnique(SysRole role) { |
| | | Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId(); |
| | | SysRole info = roleMapper.checkRoleNameUnique(role.getRoleName()); |
| | | if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue()) |
| | | { |
| | | if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue()) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public boolean checkRoleKeyUnique(SysRole role) |
| | | { |
| | | public boolean checkRoleKeyUnique(SysRole role) { |
| | | Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId(); |
| | | SysRole info = roleMapper.checkRoleKeyUnique(role.getRoleKey()); |
| | | if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue()) |
| | | { |
| | | if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue()) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | * @param role 角色信息 |
| | | */ |
| | | @Override |
| | | public void checkRoleAllowed(SysRole role) |
| | | { |
| | | if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) |
| | | { |
| | | public void checkRoleAllowed(SysRole role) { |
| | | if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) { |
| | | throw new ServiceException("不允许操作超级管理员角色"); |
| | | } |
| | | } |
| | |
| | | * @param roleId 角色id |
| | | */ |
| | | @Override |
| | | public void checkRoleDataScope(Long roleId) |
| | | { |
| | | if (!SysUser.isAdmin(SecurityUtils.getUserId())) |
| | | { |
| | | 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)) |
| | | { |
| | | if (StringUtils.isEmpty(roles)) { |
| | | throw new ServiceException("没有权限访问角色数据!"); |
| | | } |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int countUserRoleByRoleId(Long roleId) |
| | | { |
| | | public int countUserRoleByRoleId(Long roleId) { |
| | | return userRoleMapper.countUserRoleByRoleId(roleId); |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int insertRole(SysRole role) |
| | | { |
| | | public int insertRole(SysRole role) { |
| | | // 新增角色信息 |
| | | roleMapper.insertRole(role); |
| | | return insertRoleMenu(role); |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int updateRole(SysRole role) |
| | | { |
| | | public int updateRole(SysRole role) { |
| | | // 修改角色信息 |
| | | roleMapper.updateRole(role); |
| | | // 删除角色与菜单关联 |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int updateRoleStatus(SysRole role) |
| | | { |
| | | public int updateRoleStatus(SysRole role) { |
| | | return roleMapper.updateRole(role); |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int authDataScope(SysRole role) |
| | | { |
| | | public int authDataScope(SysRole role) { |
| | | // 修改角色信息 |
| | | roleMapper.updateRole(role); |
| | | // 删除角色与部门关联 |
| | |
| | | * |
| | | * @param role 角色对象 |
| | | */ |
| | | public int insertRoleMenu(SysRole role) |
| | | { |
| | | public int insertRoleMenu(SysRole role) { |
| | | int rows = 1; |
| | | // 新增用户与角色管理 |
| | | List<SysRoleMenu> list = new ArrayList<SysRoleMenu>(); |
| | | for (Long menuId : role.getMenuIds()) |
| | | { |
| | | for (Long menuId : role.getMenuIds()) { |
| | | SysRoleMenu rm = new SysRoleMenu(); |
| | | rm.setRoleId(role.getRoleId()); |
| | | rm.setMenuId(menuId); |
| | | list.add(rm); |
| | | } |
| | | if (list.size() > 0) |
| | | { |
| | | if (list.size() > 0) { |
| | | rows = roleMenuMapper.batchRoleMenu(list); |
| | | } |
| | | return rows; |
| | |
| | | * |
| | | * @param role 角色对象 |
| | | */ |
| | | public int insertRoleDept(SysRole role) |
| | | { |
| | | public int insertRoleDept(SysRole role) { |
| | | int rows = 1; |
| | | // 新增角色与部门(数据权限)管理 |
| | | List<SysRoleDept> list = new ArrayList<SysRoleDept>(); |
| | | for (Long deptId : role.getDeptIds()) |
| | | { |
| | | for (Long deptId : role.getDeptIds()) { |
| | | SysRoleDept rd = new SysRoleDept(); |
| | | rd.setRoleId(role.getRoleId()); |
| | | rd.setDeptId(deptId); |
| | | list.add(rd); |
| | | } |
| | | if (list.size() > 0) |
| | | { |
| | | if (list.size() > 0) { |
| | | rows = roleDeptMapper.batchRoleDept(list); |
| | | } |
| | | return rows; |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int deleteRoleById(Long roleId) |
| | | { |
| | | public int deleteRoleById(Long roleId) { |
| | | // 删除角色与菜单关联 |
| | | roleMenuMapper.deleteRoleMenuByRoleId(roleId); |
| | | // 删除角色与部门关联 |
| | |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public int deleteRoleByIds(List<Long> roleIds) |
| | | { |
| | | for (Long roleId : roleIds) |
| | | { |
| | | public int deleteRoleByIds(List<Long> roleIds) { |
| | | for (Long roleId : roleIds) { |
| | | SysRole role = selectRoleById(roleId); |
| | | if (countUserRoleByRoleId(roleId) > 0) |
| | | { |
| | | throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName())); |
| | | if (countUserRoleByRoleId(roleId) > 0) { |
| | | throw new ServiceException( |
| | | String.format("%1$s已分配,不能删除", role.getRoleName())); |
| | | } |
| | | } |
| | | // 删除角色与菜单关联 |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteAuthUser(SysUserRole userRole) |
| | | { |
| | | public int deleteAuthUser(SysUserRole userRole) { |
| | | return userRoleMapper.deleteUserRoleInfo(userRole); |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteAuthUsers(Long roleId, Long[] userIds) |
| | | { |
| | | public int deleteAuthUsers(Long roleId, Long[] userIds) { |
| | | return userRoleMapper.deleteUserRoleInfos(roleId, userIds); |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int insertAuthUsers(Long roleId, Long[] userIds) |
| | | { |
| | | public int insertAuthUsers(Long roleId, Long[] userIds) { |
| | | // 新增用户与角色管理 |
| | | List<SysUserRole> list = new ArrayList<SysUserRole>(); |
| | | for (Long userId : userIds) |
| | | { |
| | | for (Long userId : userIds) { |
| | | SysUserRole ur = new SysUserRole(); |
| | | ur.setUserId(userId); |
| | | ur.setRoleId(roleId); |
| | |
| | | //获取当前的权限菜单 |
| | | List<SysMenu> all = menuMapper.getAllInIds(menusId); |
| | | // 第三级 |
| | | List<SysMenu> s3 = all.stream().filter(e -> e.getMenuType().equals("F")).collect(Collectors.toList()); |
| | | List<SysMenu> s3 = all.stream().filter(e -> e.getMenuType().equals("F")) |
| | | .collect(Collectors.toList()); |
| | | // 第二级 |
| | | List<SysMenu> s2 = all.stream().filter(e -> e.getMenuType().equals("C")).collect(Collectors.toList()); |
| | | List<SysMenu> s2 = all.stream().filter(e -> e.getMenuType().equals("C")) |
| | | .collect(Collectors.toList()); |
| | | // 第一级 |
| | | List<SysMenu> s1 = all.stream().filter(e -> e.getMenuType().equals("M")).collect(Collectors.toList()); |
| | | List<SysMenu> s1 = all.stream().filter(e -> e.getMenuType().equals("M")) |
| | | .collect(Collectors.toList()); |
| | | |
| | | for (SysMenu menu : s2) { |
| | | List<SysMenu> collect = s3.stream().filter(e -> e.getParentId().equals(menu.getMenuId())).collect(Collectors.toList()); |
| | | List<SysMenu> collect = s3.stream() |
| | | .filter(e -> e.getParentId().equals(menu.getMenuId())) |
| | | .collect(Collectors.toList()); |
| | | menu.setChildren(collect); |
| | | } |
| | | for (SysMenu menu : s1) { |
| | | List<SysMenu> collect = s2.stream().filter(e -> e.getParentId().equals(menu.getMenuId())).collect(Collectors.toList()); |
| | | List<SysMenu> collect = s2.stream() |
| | | .filter(e -> e.getParentId().equals(menu.getMenuId())) |
| | | .collect(Collectors.toList()); |
| | | menu.setChildren(collect); |
| | | } |
| | | return s1; |
| | |
| | | // 添加角色 |
| | | SysRole sysRole = new SysRole(); |
| | | sysRole.setRoleName(dto.getRoleName()); |
| | | sysRole.setPostType(dto.getPostType()); |
| | | roleMapper.insertRole(sysRole); |
| | | |
| | | // 添加角色权限中间表 |
| | |
| | | SysRole sysRole = new SysRole(); |
| | | sysRole.setRoleId(dto.getRoleId()); |
| | | sysRole.setRoleName(dto.getRoleName()); |
| | | sysRole.setPostType(dto.getPostType()); |
| | | roleMapper.updateRole(sysRole); |
| | | // 删除角色与菜单关联 |
| | | roleMenuMapper.deleteRoleMenuByRoleId(dto.getRoleId()); |