Skip to content

Commit

Permalink
refactor: 重构权限变更逻辑,修改角色、变更用户角色不再下线用户
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Aug 31, 2024
1 parent 25240fa commit ad9a600
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.stream.Collectors;

/**
* 登录用户信息
Expand Down Expand Up @@ -109,16 +110,17 @@ public class LoginUser implements Serializable {
*/
private Integer passwordExpirationDays;

public LoginUser(Set<String> permissions,
Set<String> roleCodes,
Set<RoleDTO> roles,
Integer passwordExpirationDays) {
public LoginUser(Set<String> permissions, Set<RoleDTO> roles, Integer passwordExpirationDays) {
this.permissions = permissions;
this.roleCodes = roleCodes;
this.roles = roles;
this.setRoles(roles);
this.passwordExpirationDays = passwordExpirationDays;
}

public void setRoles(Set<RoleDTO> roles) {
this.roles = roles;
this.roleCodes = roles.stream().map(RoleDTO::getCode).collect(Collectors.toSet());
}

/**
* 是否为管理员
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public static String login(LoginUser loginUser) {
return tokenValue;
}

/**
* 更新登录用户信息
*
* @param loginUser
* 登录用户信息
* @param token 令牌
*/
public static void updateLoginUser(LoginUser loginUser, String token) {
SaHolder.getStorage().delete(CacheConstants.LOGIN_USER_KEY);
StpUtil.getTokenSessionByToken(token).set(CacheConstants.LOGIN_USER_KEY, loginUser);
}

/**
* 获取登录用户信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ public class OnlineUserQuery implements Serializable {
@Schema(description = "登录时间", example = "2023-08-08 00:00:00,2023-08-08 23:59:59")
@DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
private List<Date> loginTime;

/**
* 用户 ID
*/
@Schema(hidden = true)
private Long userId;

/**
* 角色 ID
*/
@Schema(hidden = true)
private Long roleId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,9 @@ public interface OnlineUserService {
LocalDateTime getLastActiveTime(String token);

/**
* 根据角色 ID 清除
*
* @param roleId 角色 ID
*/
void cleanByRoleId(Long roleId);

/**
* 根据用户 ID 清除登录
* 踢出用户
*
* @param userId 用户 ID
*/
void cleanByUserId(Long userId);
void kickOut(Long userId);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.auth.model.resp.RouteResp;
import top.continew.admin.auth.service.LoginService;
import top.continew.admin.auth.service.PermissionService;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.constant.RegexConstants;
import top.continew.admin.common.constant.SysConstants;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.enums.GenderEnum;
import top.continew.admin.system.enums.MenuTypeEnum;
import top.continew.admin.system.enums.MessageTypeEnum;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.model.dto.RoleDTO;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.admin.system.enums.MenuTypeEnum;
import top.continew.admin.system.enums.MessageTemplateEnum;
import top.continew.admin.system.enums.MessageTypeEnum;
import top.continew.admin.system.enums.PasswordPolicyEnum;
import top.continew.admin.system.model.entity.DeptDO;
import top.continew.admin.system.model.entity.RoleDO;
Expand Down Expand Up @@ -81,17 +80,16 @@
public class LoginServiceImpl implements LoginService {

private final ProjectProperties projectProperties;
private final PasswordEncoder passwordEncoder;
private final ThreadPoolTaskExecutor threadPoolTaskExecutor;
private final UserService userService;
private final DeptService deptService;
private final RoleService roleService;
private final MenuService menuService;
private final PermissionService permissionService;
private final UserRoleService userRoleService;
private final UserSocialService userSocialService;
private final MessageService messageService;
private final PasswordEncoder passwordEncoder;
private final OptionService optionService;
private final ThreadPoolTaskExecutor threadPoolTaskExecutor;
private final MessageService messageService;

@Override
public String accountLogin(String username, String password, HttpServletRequest request) {
Expand Down Expand Up @@ -163,7 +161,7 @@ public String socialLogin(AuthUser authUser) {

@Override
public List<RouteResp> buildRouteTree(Long userId) {
Set<String> roleCodeSet = permissionService.listRoleCodeByUserId(userId);
Set<String> roleCodeSet = roleService.listCodeByUserId(userId);
if (CollUtil.isEmpty(roleCodeSet)) {
return new ArrayList<>(0);
}
Expand Down Expand Up @@ -205,17 +203,15 @@ public List<RouteResp> buildRouteTree(Long userId) {
*/
private String login(UserDO user) {
Long userId = user.getId();
CompletableFuture<Set<String>> permissionFuture = CompletableFuture.supplyAsync(() -> permissionService
CompletableFuture<Set<String>> permissionFuture = CompletableFuture.supplyAsync(() -> roleService
.listPermissionByUserId(userId), threadPoolTaskExecutor);
CompletableFuture<Set<String>> roleCodeFuture = CompletableFuture.supplyAsync(() -> permissionService
.listRoleCodeByUserId(userId), threadPoolTaskExecutor);
CompletableFuture<Set<RoleDTO>> roleFuture = CompletableFuture.supplyAsync(() -> roleService
.listByUserId(userId), threadPoolTaskExecutor);
CompletableFuture<Integer> passwordExpirationDaysFuture = CompletableFuture.supplyAsync(() -> optionService
.getValueByCode2Int(PASSWORD_EXPIRATION_DAYS.name()));
CompletableFuture.allOf(permissionFuture, roleCodeFuture, roleFuture);
LoginUser loginUser = new LoginUser(permissionFuture.join(), roleCodeFuture.join(), roleFuture
.join(), passwordExpirationDaysFuture.join());
CompletableFuture.allOf(permissionFuture, roleFuture, passwordExpirationDaysFuture);
LoginUser loginUser = new LoginUser(permissionFuture.join(), roleFuture.join(), passwordExpirationDaysFuture
.join());
BeanUtil.copyProperties(user, loginUser);
return LoginHelper.login(loginUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import cn.crane4j.annotation.AutoOperate;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.auth.model.query.OnlineUserQuery;
import top.continew.admin.auth.model.resp.OnlineUserResp;
Expand All @@ -44,10 +44,10 @@
* 在线用户业务实现
*
* @author Charles7c
* @author Lion Li(<a href="https://gitee.com/dromara/RuoYi-Vue-Plus">RuoYi-Vue-Plus</a>)
* @since 2023/3/25 22:49
*/
@Service
@RequiredArgsConstructor
public class OnlineUserServiceImpl implements OnlineUserService {

@Override
Expand All @@ -63,18 +63,18 @@ public List<LoginUser> list(OnlineUserQuery query) {
List<LoginUser> loginUserList = new ArrayList<>();
// 查询所有登录用户
List<String> tokenKeyList = StpUtil.searchTokenValue(StringConstants.EMPTY, 0, -1, false);
for (String tokenKey : tokenKeyList) {
tokenKeyList.parallelStream().forEach(tokenKey -> {
String token = StrUtil.subAfter(tokenKey, StringConstants.COLON, true);
// 忽略已过期或失效 Token
if (StpUtil.stpLogic.getTokenActiveTimeoutByToken(token) < SaTokenDao.NEVER_EXPIRE) {
continue;
return;
}
// 检查是否符合查询条件
LoginUser loginUser = LoginHelper.getLoginUser(token);
if (this.isMatchQuery(query, loginUser)) {
loginUserList.add(loginUser);
}
}
});
// 设置排序
CollUtil.sort(loginUserList, Comparator.comparing(LoginUser::getLoginTime).reversed());
return loginUserList;
Expand All @@ -87,20 +87,7 @@ public LocalDateTime getLastActiveTime(String token) {
}

@Override
public void cleanByRoleId(Long roleId) {
List<LoginUser> loginUserList = this.list(new OnlineUserQuery());
loginUserList.parallelStream().forEach(u -> {
if (u.getRoles().stream().anyMatch(r -> r.getId().equals(roleId))) {
try {
StpUtil.logoutByTokenValue(u.getToken());
} catch (NotLoginException ignored) {
}
}
});
}

@Override
public void cleanByUserId(Long userId) {
public void kickOut(Long userId) {
if (!StpUtil.isLogin(userId)) {
return;
}
Expand All @@ -121,13 +108,22 @@ private boolean isMatchQuery(OnlineUserQuery query, LoginUser loginUser) {
flag1 = StrUtil.contains(loginUser.getUsername(), nickname) || StrUtil.contains(LoginHelper
.getNickname(loginUser.getId()), nickname);
}

boolean flag2 = true;
List<Date> loginTime = query.getLoginTime();
if (CollUtil.isNotEmpty(loginTime)) {
flag2 = DateUtil.isIn(DateUtil.date(loginUser.getLoginTime()).toJdkDate(), loginTime.get(0), loginTime
.get(1));
}
return flag1 && flag2;
boolean flag3 = true;
Long userId = query.getUserId();
if (null != userId) {
flag3 = userId.equals(loginUser.getId());
}
boolean flag4 = true;
Long roleId = query.getRoleId();
if (null != roleId) {
flag4 = loginUser.getRoles().stream().anyMatch(r -> r.getId().equals(roleId));
}
return flag1 && flag2 && flag3 && flag4;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
*/
public interface RoleService extends BaseService<RoleResp, RoleDetailResp, RoleQuery, RoleReq>, IService<RoleDO> {

/**
* 根据用户 ID 查询权限码
*
* @param userId 用户 ID
* @return 权限码集合
*/
Set<String> listPermissionByUserId(Long userId);

/**
* 根据 ID 列表查询
*
Expand Down
Loading

0 comments on commit ad9a600

Please sign in to comment.