fix: 修复用户管理/角色管理编辑及状态变更问题 (#53)

修复用户管理修改任意信息,导致密码二次加密修改造成无法登录的问题

补充用户管理、权限管理状态变更后的逻辑:
1、禁用的角色不再允许分配给用户
2、已经分配给用户的角色不允许禁用
3、禁用用户后将清理该用户所有登录token
This commit is contained in:
kils 2024-04-25 18:00:38 +08:00 committed by GitHub
parent 70ed667c16
commit abf1e651e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 56 additions and 3 deletions

View File

@ -51,6 +51,12 @@ public class LabelValueResp<T> implements Serializable {
@Schema(description = "", example = "1")
private T value;
/**
* 是否禁用
*/
@Schema(description = "是否禁用", example = "false")
private Boolean disabled;
/**
* 颜色
*/
@ -68,4 +74,10 @@ public class LabelValueResp<T> implements Serializable {
this.value = value;
this.color = color;
}
public LabelValueResp(String label, T value, Boolean disabled) {
this.label = label;
this.value = value;
this.disabled = disabled;
}
}

View File

@ -55,4 +55,11 @@ public interface OnlineUserService {
* @param roleId 角色 ID
*/
void cleanByRoleId(Long roleId);
/**
* 根据用户 ID 清除登录
*
* @param userId 用户 ID
*/
void cleanByUserId(Long userId);
}

View File

@ -92,6 +92,14 @@ public class OnlineUserServiceImpl implements OnlineUserService {
});
}
@Override
public void cleanByUserId(Long userId) {
if (!StpUtil.isLogin(userId)) {
return;
}
StpUtil.logout(userId);
}
/**
* 是否符合查询条件
*

View File

@ -57,4 +57,12 @@ public interface UserRoleService {
* @return 总记录数
*/
Long countByRoleIds(List<Long> roleIds);
/**
* 根据角色 ID 判断是否已被用户关联
*
* @param roleId 角色 ID
* @return 是否已关联
*/
boolean isRoleIdExists(Long roleId);
}

View File

@ -84,6 +84,8 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
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
@ -141,7 +143,9 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
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

View File

@ -78,4 +78,9 @@ public class UserRoleServiceImpl implements UserRoleService {
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();
}
}

View File

@ -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;
@ -32,6 +33,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
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;
@ -68,6 +70,7 @@ import java.util.Optional;
@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;
@ -125,9 +128,15 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
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