| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.dromara.common.core.constant.CacheNames; |
| | | import org.dromara.common.core.constant.SystemConstants; |
| | | import org.dromara.common.core.constant.TenantConstants; |
| | | import org.dromara.common.core.constant.UserConstants; |
| | | import org.dromara.common.core.domain.model.LoginUser; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.service.RoleService; |
| | | import org.dromara.common.core.utils.MapstructUtils; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | |
| | | import org.dromara.system.mapper.SysRoleMenuMapper; |
| | | import org.dromara.system.mapper.SysUserRoleMapper; |
| | | import org.dromara.system.service.ISysRoleService; |
| | | import org.springframework.cache.annotation.CacheEvict; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | |
| | | */ |
| | | @RequiredArgsConstructor |
| | | @Service |
| | | public class SysRoleServiceImpl implements ISysRoleService { |
| | | public class SysRoleServiceImpl implements ISysRoleService, RoleService { |
| | | |
| | | private final SysRoleMapper baseMapper; |
| | | private final SysRoleMenuMapper roleMenuMapper; |
| | |
| | | private Wrapper<SysRole> buildQueryWrapper(SysRoleBo bo) { |
| | | Map<String, Object> params = bo.getParams(); |
| | | QueryWrapper<SysRole> wrapper = Wrappers.query(); |
| | | wrapper.eq("r.del_flag", UserConstants.ROLE_NORMAL) |
| | | wrapper.eq("r.del_flag", SystemConstants.NORMAL) |
| | | .eq(ObjectUtil.isNotNull(bo.getRoleId()), "r.role_id", bo.getRoleId()) |
| | | .like(StringUtils.isNotBlank(bo.getRoleName()), "r.role_name", bo.getRoleName()) |
| | | .eq(StringUtils.isNotBlank(bo.getStatus()), "r.status", bo.getStatus()) |
| | |
| | | @Override |
| | | public List<SysRoleVo> selectRoleByIds(List<Long> roleIds) { |
| | | return baseMapper.selectRoleList(new QueryWrapper<SysRole>() |
| | | .eq("r.status", UserConstants.ROLE_NORMAL) |
| | | .eq("r.status", SystemConstants.NORMAL) |
| | | .in(CollUtil.isNotEmpty(roleIds), "r.role_id", roleIds)); |
| | | } |
| | | |
| | |
| | | public int updateRole(SysRoleBo bo) { |
| | | SysRole role = MapstructUtils.convert(bo, SysRole.class); |
| | | |
| | | if (UserConstants.ROLE_DISABLE.equals(role.getStatus()) && this.countUserRoleByRoleId(role.getRoleId()) > 0) { |
| | | if (SystemConstants.DISABLE.equals(role.getStatus()) && this.countUserRoleByRoleId(role.getRoleId()) > 0) { |
| | | throw new ServiceException("角色已分配,不能禁用!"); |
| | | } |
| | | // 修改角色信息 |
| | |
| | | */ |
| | | @Override |
| | | public int updateRoleStatus(Long roleId, String status) { |
| | | if (UserConstants.ROLE_DISABLE.equals(status) && this.countUserRoleByRoleId(roleId) > 0) { |
| | | if (SystemConstants.DISABLE.equals(status) && this.countUserRoleByRoleId(roleId) > 0) { |
| | | throw new ServiceException("角色已分配,不能禁用!"); |
| | | } |
| | | return baseMapper.update(null, |
| | |
| | | * @param bo 角色信息 |
| | | * @return 结果 |
| | | */ |
| | | @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, key = "#bo.roleId") |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int authDataScope(SysRoleBo bo) { |
| | |
| | | private int insertRoleMenu(SysRoleBo role) { |
| | | int rows = 1; |
| | | // 新增用户与角色管理 |
| | | List<SysRoleMenu> list = new ArrayList<SysRoleMenu>(); |
| | | List<SysRoleMenu> list = new ArrayList<>(); |
| | | for (Long menuId : role.getMenuIds()) { |
| | | SysRoleMenu rm = new SysRoleMenu(); |
| | | rm.setRoleId(role.getRoleId()); |
| | |
| | | private int insertRoleDept(SysRoleBo role) { |
| | | int rows = 1; |
| | | // 新增角色与部门(数据权限)管理 |
| | | List<SysRoleDept> list = new ArrayList<SysRoleDept>(); |
| | | List<SysRoleDept> list = new ArrayList<>(); |
| | | for (Long deptId : role.getDeptIds()) { |
| | | SysRoleDept rd = new SysRoleDept(); |
| | | rd.setRoleId(role.getRoleId()); |
| | |
| | | * @param roleId 角色ID |
| | | * @return 结果 |
| | | */ |
| | | @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, key = "#roleId") |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deleteRoleById(Long roleId) { |
| | |
| | | * @param roleIds 需要删除的角色ID |
| | | * @return 结果 |
| | | */ |
| | | @CacheEvict(cacheNames = CacheNames.SYS_ROLE_CUSTOM, allEntries = true) |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deleteRoleByIds(Long[] roleIds) { |
| | |
| | | .eq(SysUserRole::getRoleId, userRole.getRoleId()) |
| | | .eq(SysUserRole::getUserId, userRole.getUserId())); |
| | | if (rows > 0) { |
| | | cleanOnlineUserByRole(userRole.getRoleId()); |
| | | cleanOnlineUser(List.of(userRole.getUserId())); |
| | | } |
| | | return rows; |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public int deleteAuthUsers(Long roleId, Long[] userIds) { |
| | | List<Long> ids = List.of(userIds); |
| | | int rows = userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() |
| | | .eq(SysUserRole::getRoleId, roleId) |
| | | .in(SysUserRole::getUserId, Arrays.asList(userIds))); |
| | | .in(SysUserRole::getUserId, ids)); |
| | | if (rows > 0) { |
| | | cleanOnlineUserByRole(roleId); |
| | | cleanOnlineUser(ids); |
| | | } |
| | | return rows; |
| | | } |
| | |
| | | public int insertAuthUsers(Long roleId, Long[] userIds) { |
| | | // 新增用户与角色管理 |
| | | int rows = 1; |
| | | List<SysUserRole> list = StreamUtils.toList(List.of(userIds), userId -> { |
| | | List<Long> ids = List.of(userIds); |
| | | List<SysUserRole> list = StreamUtils.toList(ids, userId -> { |
| | | SysUserRole ur = new SysUserRole(); |
| | | ur.setUserId(userId); |
| | | ur.setRoleId(roleId); |
| | |
| | | rows = userRoleMapper.insertBatch(list) ? list.size() : 0; |
| | | } |
| | | if (rows > 0) { |
| | | cleanOnlineUserByRole(roleId); |
| | | cleanOnlineUser(ids); |
| | | } |
| | | return rows; |
| | | } |
| | |
| | | return; |
| | | } |
| | | LoginUser loginUser = LoginHelper.getLoginUser(token); |
| | | if (ObjectUtil.isNull(loginUser) || CollUtil.isEmpty(loginUser.getRoles())) { |
| | | return; |
| | | } |
| | | if (loginUser.getRoles().stream().anyMatch(r -> r.getRoleId().equals(roleId))) { |
| | | try { |
| | | StpUtil.logoutByTokenValue(token); |
| | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public void cleanOnlineUser(List<Long> userIds) { |
| | | List<String> keys = StpUtil.searchTokenValue("", 0, -1, false); |
| | | if (CollUtil.isEmpty(keys)) { |
| | | return; |
| | | } |
| | | // 角色关联的在线用户量过大会导致redis阻塞卡顿 谨慎操作 |
| | | keys.parallelStream().forEach(key -> { |
| | | String token = StringUtils.substringAfterLast(key, ":"); |
| | | // 如果已经过期则跳过 |
| | | if (StpUtil.stpLogic.getTokenActiveTimeoutByToken(token) < -1) { |
| | | return; |
| | | } |
| | | LoginUser loginUser = LoginHelper.getLoginUser(token); |
| | | if (ObjectUtil.isNull(loginUser)) { |
| | | return; |
| | | } |
| | | if (userIds.contains(loginUser.getUserId())) { |
| | | try { |
| | | StpUtil.logoutByTokenValue(token); |
| | | } catch (NotLoginException ignored) { |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | } |