refactor: UserSessionUtils.getGroupNames
This commit is contained in:
parent
68c030242b
commit
84c730844c
@ -24,6 +24,13 @@ public final class UserSessionVO {
|
||||
|
||||
private List<String> groupNames;
|
||||
|
||||
/**
|
||||
* 是否是管理员用户
|
||||
*/
|
||||
public boolean isAdmin() {
|
||||
return Objects.equals(this.role, RoleEnum.ADMIN.getRoleId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是普通用户
|
||||
*/
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.aizuda.snailjob.server.web.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.aizuda.snailjob.common.core.exception.SnailJobAuthenticationException;
|
||||
import com.aizuda.snailjob.server.web.model.request.UserSessionVO;
|
||||
import com.google.common.collect.Lists;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@ -25,35 +26,34 @@ public final class UserSessionUtils {
|
||||
|
||||
/**
|
||||
* 计算用户组权限及组名查询条件的组合结果
|
||||
* 1. 普通用户:
|
||||
* 1.1 查询条件为空, 返回用户的组权限
|
||||
* 1.2 查询条件不为空,返回用户的组权限与查询条件交集
|
||||
* 2. 管理员:
|
||||
* 2.1 查询条件为空, 返回空
|
||||
* 2.2 查询条件不为空, 返回查询条件组名
|
||||
*
|
||||
* 1. 管理员:
|
||||
* 1.1 查询条件为空, 返回空
|
||||
* 1.2 查询条件不为空, 返回查询条件组名
|
||||
*
|
||||
* 2. 普通用户:
|
||||
* 2.1 查询条件为空, 返回用户的组权限
|
||||
* 2.2 查询条件不为空,返回用户的组权限与查询条件交集
|
||||
*
|
||||
* @param groupNameQuery 组名查询条件
|
||||
* @return 用户组查询集合
|
||||
*/
|
||||
public static List<String> getGroupNames(String groupNameQuery) {
|
||||
UserSessionVO userSessionVO = currentUserSession();
|
||||
if (userSessionVO.isUser()) { // 普通用户
|
||||
List<String> groupNames = userSessionVO.getGroupNames();
|
||||
if (CollUtil.isNotEmpty(groupNames)) {
|
||||
if (StrUtil.isNotBlank(groupNameQuery)) {
|
||||
if (groupNames.contains(groupNameQuery)) {
|
||||
return Lists.newArrayList(groupNameQuery);
|
||||
}
|
||||
} else {
|
||||
return groupNames;
|
||||
}
|
||||
}
|
||||
} else { // 管理员
|
||||
if (userSessionVO.isAdmin()) {
|
||||
// 若是管理员且存在查询条件
|
||||
if (StrUtil.isNotBlank(groupNameQuery)) {
|
||||
return Lists.newArrayList(groupNameQuery);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
List<String> groupNames = userSessionVO.getGroupNames();
|
||||
Assert.notEmpty(groupNames, () -> new SnailJobAuthenticationException("普通用户组权限为空"));
|
||||
// 若是普通用户且权限包括查询条件
|
||||
if (StrUtil.isNotBlank(groupNameQuery) && groupNames.contains(groupNameQuery)) {
|
||||
return Lists.newArrayList(groupNameQuery);
|
||||
}
|
||||
return groupNames;
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user