mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-08-06 14:21:52 +08:00
基于部门的数据权限
This commit is contained in:
@@ -92,9 +92,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
throw new UsernameNotFoundException(username);
|
||||
}
|
||||
// 创建 LoginUser 对象
|
||||
LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user);
|
||||
loginUser.setPostIds(user.getPostIds());
|
||||
return loginUser;
|
||||
return this.buildLoginUser(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,9 +105,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
this.createLoginLog(user.getUsername(), SysLoginLogTypeEnum.LOGIN_MOCK, SysLoginResultEnum.SUCCESS);
|
||||
|
||||
// 创建 LoginUser 对象
|
||||
LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user);
|
||||
loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
|
||||
return loginUser;
|
||||
return this.buildLoginUser(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -117,10 +113,9 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
// 判断验证码是否正确
|
||||
this.verifyCaptcha(reqVO.getUsername(), reqVO.getUuid(), reqVO.getCode());
|
||||
|
||||
// 使用账号密码,进行登录。
|
||||
// 使用账号密码,进行登录
|
||||
LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword());
|
||||
loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
|
||||
loginUser.setGroups(this.getUserPosts(loginUser.getPostIds()));
|
||||
|
||||
// 缓存登陆用户到 Redis 中,返回 sessionId 编号
|
||||
return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
|
||||
}
|
||||
@@ -234,8 +229,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
this.createLoginLog(user.getUsername(), SysLoginLogTypeEnum.LOGIN_SOCIAL, SysLoginResultEnum.SUCCESS);
|
||||
|
||||
// 创建 LoginUser 对象
|
||||
LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user);
|
||||
loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
|
||||
LoginUser loginUser = this.buildLoginUser(user);
|
||||
|
||||
// 绑定社交用户(更新)
|
||||
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
|
||||
@@ -252,7 +246,6 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
|
||||
// 使用账号密码,进行登录。
|
||||
LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword());
|
||||
loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
|
||||
|
||||
// 绑定社交用户(新增)
|
||||
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
|
||||
@@ -305,15 +298,14 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
return null;
|
||||
}
|
||||
// 刷新 LoginUser 缓存
|
||||
this.refreshLoginUserCache(token, loginUser);
|
||||
return loginUser;
|
||||
return this.refreshLoginUserCache(token, loginUser);
|
||||
}
|
||||
|
||||
private void refreshLoginUserCache(String token, LoginUser loginUser) {
|
||||
private LoginUser refreshLoginUserCache(String token, LoginUser loginUser) {
|
||||
// 每 1/3 的 Session 超时时间,刷新 LoginUser 缓存
|
||||
if (System.currentTimeMillis() - loginUser.getUpdateTime().getTime() <
|
||||
userSessionCoreService.getSessionTimeoutMillis() / 3) {
|
||||
return;
|
||||
return loginUser;
|
||||
}
|
||||
|
||||
// 重新加载 SysUserDO 信息
|
||||
@@ -323,9 +315,18 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
}
|
||||
|
||||
// 刷新 LoginUser 缓存
|
||||
LoginUser newLoginUser= this.buildLoginUser(user);
|
||||
userSessionCoreService.refreshUserSession(token, newLoginUser);
|
||||
return newLoginUser;
|
||||
}
|
||||
|
||||
private LoginUser buildLoginUser(SysUserDO user) {
|
||||
LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user);
|
||||
// 补全字段
|
||||
loginUser.setDeptId(user.getDeptId());
|
||||
loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId()));
|
||||
userSessionCoreService.refreshUserSession(token, loginUser);
|
||||
loginUser.setGroups(this.getUserPosts(user.getPostIds()));
|
||||
return loginUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -169,9 +169,12 @@ public class SysDeptServiceImpl implements SysDeptService {
|
||||
|
||||
@Override
|
||||
public List<SysDeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive) {
|
||||
List<SysDeptDO> result = new ArrayList<>();
|
||||
if (parentId == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<SysDeptDO> result = new ArrayList<>(); // TODO 芋艿:待优化,新增缓存,避免每次遍历的计算
|
||||
// 递归,简单粗暴
|
||||
this.listDeptsByParentIdFromCache(result, parentId,
|
||||
this.getDeptsByParentIdFromCache(result, parentId,
|
||||
recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次
|
||||
parentDeptCache);
|
||||
return result;
|
||||
@@ -185,8 +188,8 @@ public class SysDeptServiceImpl implements SysDeptService {
|
||||
* @param recursiveCount 递归次数
|
||||
* @param parentDeptMap 父部门 Map,使用缓存,避免变化
|
||||
*/
|
||||
private void listDeptsByParentIdFromCache(List<SysDeptDO> result, Long parentId, int recursiveCount,
|
||||
Multimap<Long, SysDeptDO> parentDeptMap) {
|
||||
private void getDeptsByParentIdFromCache(List<SysDeptDO> result, Long parentId, int recursiveCount,
|
||||
Multimap<Long, SysDeptDO> parentDeptMap) {
|
||||
// 递归次数为 0,结束!
|
||||
if (recursiveCount == 0) {
|
||||
return;
|
||||
@@ -198,7 +201,7 @@ public class SysDeptServiceImpl implements SysDeptService {
|
||||
}
|
||||
result.addAll(depts);
|
||||
// 继续递归
|
||||
depts.forEach(dept -> listDeptsByParentIdFromCache(result, dept.getId(),
|
||||
depts.forEach(dept -> getDeptsByParentIdFromCache(result, dept.getId(),
|
||||
recursiveCount - 1, parentDeptMap));
|
||||
}
|
||||
|
||||
|
@@ -18,6 +18,7 @@ import cn.iocoder.yudao.adminserver.modules.system.enums.permission.SysRoleTypeE
|
||||
import cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission.SysRoleProducer;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService;
|
||||
import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -127,6 +128,7 @@ public class SysRoleServiceImpl implements SysRoleService {
|
||||
SysRoleDO role = SysRoleConvert.INSTANCE.convert(reqVO);
|
||||
role.setType(SysRoleTypeEnum.CUSTOM.getType());
|
||||
role.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
role.setDataScope(DataScopeEnum.ALL.getScope()); // 默认可查看所有数据。原因是,可能一些项目不需要项目权限
|
||||
roleMapper.insert(role);
|
||||
// 发送刷新消息
|
||||
roleProducer.sendRoleRefreshMessage();
|
||||
|
Reference in New Issue
Block a user