Skip to content

Commit

Permalink
fix: 修复用户管理/角色管理编辑及状态变更问题 (#53)
Browse files Browse the repository at this point in the history
修复用户管理修改任意信息,导致密码二次加密修改造成无法登录的问题

补充用户管理、权限管理状态变更后的逻辑:
1、禁用的角色不再允许分配给用户
2、已经分配给用户的角色不允许禁用
3、禁用用户后将清理该用户所有登录token
  • Loading branch information
jskils authored Apr 25, 2024
1 parent 70ed667 commit abf1e65
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public class LabelValueResp<T> implements Serializable {
@Schema(description = "值", example = "1")
private T value;

/**
* 是否禁用
*/
@Schema(description = "是否禁用", example = "false")
private Boolean disabled;

/**
* 颜色
*/
Expand All @@ -68,4 +74,10 @@ public LabelValueResp(String label, T value, String color) {
this.value = value;
this.color = color;
}

public LabelValueResp(String label, T value, Boolean disabled) {
this.label = label;
this.value = value;
this.disabled = disabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ public interface OnlineUserService {
* @param roleId 角色 ID
*/
void cleanByRoleId(Long roleId);

/**
* 根据用户 ID 清除登录
*
* @param userId 用户 ID
*/
void cleanByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public void cleanByRoleId(Long roleId) {
});
}

@Override
public void cleanByUserId(Long userId) {
if (!StpUtil.isLogin(userId)) {
return;
}
StpUtil.logout(userId);
}

/**
* 是否符合查询条件
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ public interface UserRoleService {
* @return 总记录数
*/
Long countByRoleIds(List<Long> roleIds);

/**
* 根据角色 ID 判断是否已被用户关联
*
* @param roleId 角色 ID
* @return 是否已关联
*/
boolean isRoleIdExists(Long roleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public void update(RoleReq req, Long id) {
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);
RoleDO oldRole = super.getById(id);
CheckUtils.throwIfNotEqual(req.getCode(), oldRole.getCode(), "角色编码不允许修改", oldRole.getName());
CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(req.getStatus()) && userRoleService
.isRoleIdExists(id), "所选角色存在用户关联,请解除关联后重试");
DataScopeEnum oldDataScope = oldRole.getDataScope();
if (Boolean.TRUE.equals(oldRole.getIsSystem())) {
CheckUtils.throwIfEqual(DisEnableStatusEnum.DISABLE, req.getStatus(), "[{}] 是系统内置角色,不允许禁用", oldRole
Expand Down Expand Up @@ -141,7 +143,9 @@ public List<LabelValueResp<Long>> buildDict(List<RoleResp> list) {
if (CollUtil.isEmpty(list)) {
return new ArrayList<>(0);
}
return list.stream().map(r -> new LabelValueResp<>(r.getName(), r.getId())).toList();
return list.stream()
.map(r -> new LabelValueResp<>(r.getName(), r.getId(), DisEnableStatusEnum.DISABLE.equals(r.getStatus())))
.toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ public List<Long> listRoleIdByUserId(Long userId) {
public Long countByRoleIds(List<Long> roleIds) {
return userRoleMapper.lambdaQuery().in(UserRoleDO::getRoleId, roleIds).count();
}

@Override
public boolean isRoleIdExists(Long roleId) {
return userRoleMapper.lambdaQuery().eq(UserRoleDO::getRoleId, roleId).exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package top.continew.admin.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.ObjectUtil;
Expand All @@ -32,6 +33,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.auth.service.OnlineUserService;
import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.util.helper.LoginHelper;
Expand Down Expand Up @@ -68,6 +70,7 @@
@RequiredArgsConstructor
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserResp, UserDetailResp, UserQuery, UserReq> implements UserService, CommonUserService {

private final OnlineUserService onlineUserService;
private final RoleService roleService;
private final UserRoleService userRoleService;
private final FileService fileService;
Expand Down Expand Up @@ -125,9 +128,15 @@ public void update(UserReq req, Long id) {
CheckUtils.throwIfNotEmpty(disjunctionRoleIds, "[{}] 是系统内置用户,不允许变更角色", oldUser.getNickname());
}
// 更新信息
super.update(req, id);
UserDO newUser = BeanUtil.toBean(req, UserDO.class);
newUser.setId(id);
baseMapper.updateById(newUser);
// 保存用户和角色关联
userRoleService.add(req.getRoleIds(), id);
boolean isSaveUserRoleSuccess = userRoleService.add(req.getRoleIds(), id);
// 如果功能权限或数据权限有变更,则清除关联的在线用户(重新登录以获取最新角色权限)
if (DisEnableStatusEnum.DISABLE.equals(newStatus) || isSaveUserRoleSuccess) {
onlineUserService.cleanByUserId(id);
}
}

@Override
Expand Down

0 comments on commit abf1e65

Please sign in to comment.