|
@@ -1,9 +1,13 @@
|
|
|
package com.ruoyi.system.utils;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
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.common.utils.wrapper.IWrapper;
|
|
|
import com.ruoyi.system.mapper.SysRoleMapper;
|
|
|
+import com.ruoyi.system.service.ISysRoleService;
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
@@ -18,6 +22,7 @@ public class UserUtil {
|
|
|
|
|
|
private static final ISysUserService sysUserService = SpringUtil.getBean(ISysUserService.class);
|
|
|
private static final SysRoleMapper roleMapper = SpringUtil.getBean(SysRoleMapper.class);
|
|
|
+ private static final ISysRoleService roleService = SpringUtil.getBean(ISysRoleService.class);
|
|
|
|
|
|
public static List<SysUser> getListByIds(List<Long> idList) {
|
|
|
if (idList.size() == 0) {
|
|
@@ -70,28 +75,63 @@ public class UserUtil {
|
|
|
/**
|
|
|
* 根据角色KEY获取用户id列表
|
|
|
*/
|
|
|
- public static List<Long> getUserIdsByRoleKey(String roleKey){
|
|
|
+ public static List<Long> getUserIdsByRoleKey(String roleKey) {
|
|
|
return sysUserService.getUserIdsByRoleKey(roleKey);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据角色KEY和公司id获取用户id列表
|
|
|
*/
|
|
|
- public static List<Long> getUserIdsByRoleKey(String roleKey, Long companyId){
|
|
|
- return sysUserService.getUserIdsByRoleKey(roleKey,companyId);
|
|
|
+ public static List<Long> getUserIdsByRoleKey(String roleKey, Long companyId) {
|
|
|
+ return sysUserService.getUserIdsByRoleKey(roleKey, companyId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Long> getUserIdsByRoleKeys(List<String> roleKeys, Long companyId) {
|
|
|
+ roleKeys = roleKeys.stream().distinct().collect(Collectors.toList());
|
|
|
+ List<Long> uidList = new ArrayList<>();
|
|
|
+ for (String roleKey : roleKeys) {
|
|
|
+ uidList.addAll(sysUserService.getUserIdsByRoleKey(roleKey, companyId));
|
|
|
+ }
|
|
|
+ return uidList.stream().distinct().collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取用户角色列表
|
|
|
*/
|
|
|
- public static List<SysRole> getUserRoles(Long userId){
|
|
|
+ public static List<SysRole> getUserRoles(Long userId) {
|
|
|
return roleMapper.selectRolePermissionByUserId(userId);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获取用户角色Key列表
|
|
|
*/
|
|
|
- public static List<String> getUserRoleKeys(Long userId){
|
|
|
+ public static List<String> getUserRoleKeys(Long userId) {
|
|
|
return getUserRoles(userId).stream().map(SysRole::getRoleKey).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取用户角色以及子角色
|
|
|
+ */
|
|
|
+ public static List<SysRole> getUserRoleAndChildRole(Long userId) {
|
|
|
+ return recursionRoles(getUserRoles(userId), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归角色以及子角色
|
|
|
+ */
|
|
|
+ private static List<SysRole> recursionRoles(List<SysRole> userRoles, int count) {
|
|
|
+ count++;
|
|
|
+ if (count >= 100) {
|
|
|
+ throw new ServiceException("查询角色子级循环超过" + count + "次,可能成环,请检查或联系管理员!");
|
|
|
+ }
|
|
|
+ for (SysRole userRole : userRoles) {
|
|
|
+ List<SysRole> list = roleService.list(IWrapper.<SysRole>getWrapper().eq(SysRole::getParentId, userRole.getRoleId()));
|
|
|
+ if (ObjectUtil.isEmpty(list)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ userRoles.addAll(recursionRoles(list, count));
|
|
|
+ }
|
|
|
+ return userRoles;
|
|
|
+ }
|
|
|
+
|
|
|
}
|