mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-13 02:57:13 +08:00
refactor: 获取不到当前登录用户信息则抛出未登录异常
修复 Qodana 扫描问题
This commit is contained in:
@@ -16,16 +16,15 @@
|
||||
|
||||
package top.charles7c.continew.admin.common.config.mybatis;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
|
||||
import top.charles7c.continew.admin.common.model.dto.LoginUser;
|
||||
import top.charles7c.continew.admin.common.util.helper.LoginHelper;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionCurrentUser;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionFilter;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataScope;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据权限过滤器实现类
|
||||
*
|
||||
@@ -37,15 +36,12 @@ public class DataPermissionFilterImpl implements DataPermissionFilter {
|
||||
@Override
|
||||
public boolean isFilter() {
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
return null != loginUser && !loginUser.isAdmin();
|
||||
return !loginUser.isAdmin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataPermissionCurrentUser getCurrentUser() {
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
if (null == loginUser) {
|
||||
throw new IllegalArgumentException("Current user is not allowed to be empty.");
|
||||
}
|
||||
DataPermissionCurrentUser currentUser = new DataPermissionCurrentUser();
|
||||
currentUser.setUserId(Convert.toStr(loginUser.getId()));
|
||||
currentUser.setDeptId(Convert.toStr(loginUser.getDeptId()));
|
||||
|
@@ -17,6 +17,7 @@
|
||||
package top.charles7c.continew.admin.common.util.helper;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -67,17 +68,16 @@ public class LoginHelper {
|
||||
/**
|
||||
* 获取登录用户信息
|
||||
*
|
||||
* @return 登录用户信息(获取 TokenSession 时如未登录,会抛出异常)
|
||||
* @return 登录用户信息
|
||||
* @throws NotLoginException 未登录异常
|
||||
*/
|
||||
public static LoginUser getLoginUser() {
|
||||
public static LoginUser getLoginUser() throws NotLoginException {
|
||||
StpUtil.checkLogin();
|
||||
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConstants.LOGIN_USER_KEY);
|
||||
if (null != loginUser) {
|
||||
return loginUser;
|
||||
}
|
||||
SaSession tokenSession = StpUtil.getTokenSession();
|
||||
if (null == tokenSession) {
|
||||
return null;
|
||||
}
|
||||
loginUser = (LoginUser)tokenSession.get(CacheConstants.LOGIN_USER_KEY);
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, loginUser);
|
||||
return loginUser;
|
||||
@@ -103,7 +103,7 @@ public class LoginHelper {
|
||||
* @return 登录用户 ID
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
return ExceptionUtils.exToNull(() -> getLoginUser().getId());
|
||||
return getLoginUser().getId();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,7 +112,7 @@ public class LoginHelper {
|
||||
* @return 登录用户名
|
||||
*/
|
||||
public static String getUsername() {
|
||||
return ExceptionUtils.exToNull(() -> getLoginUser().getUsername());
|
||||
return getLoginUser().getUsername();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user