Skip to content

Commit

Permalink
refactor: 获取不到当前登录用户信息则抛出未登录异常
Browse files Browse the repository at this point in the history
修复 Qodana 扫描问题
  • Loading branch information
Charles7c committed Jan 26, 2024
1 parent a2420d3 commit d972a44
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 数据权限过滤器实现类
*
Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,17 +68,16 @@ public static String login(LoginUser loginUser) {
/**
* 获取登录用户信息
*
* @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;
Expand All @@ -103,7 +103,7 @@ public static LoginUser getLoginUser(String token) {
* @return 登录用户 ID
*/
public static Long getUserId() {
return ExceptionUtils.exToNull(() -> getLoginUser().getId());
return getLoginUser().getId();
}

/**
Expand All @@ -112,7 +112,7 @@ public static Long getUserId() {
* @return 登录用户名
*/
public static String getUsername() {
return ExceptionUtils.exToNull(() -> getLoginUser().getUsername());
return getLoginUser().getUsername();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package top.charles7c.continew.admin.system.model.entity;

import java.io.Serializable;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
* 消息和用户关联实体
Expand All @@ -33,6 +33,7 @@
@TableName("sys_message_user")
public class MessageUserDO implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryIgnore;
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryType;

import java.io.Serial;
import java.io.Serializable;

/**
Expand All @@ -34,6 +35,7 @@
@Schema(description = "消息查询条件")
public class MessageQuery implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

/**
Expand Down

0 comments on commit d972a44

Please sign in to comment.