【8.1.0】【role】更新用户角色缓存,改为存实体对象,增加一个获取用户对应公司角色的方法

pull/60/head
fengshuonan 2024-01-17 16:46:35 +08:00
parent 715ab551a7
commit 4192ca6b04
9 changed files with 119 additions and 36 deletions

View File

@ -12,13 +12,21 @@ import java.util.Set;
public interface SysUserRoleServiceApi {
/**
* id
* id
*
* @author fengshuonan
* @since 2023/6/12 11:29
*/
List<Long> getUserRoleIdList(Long userId);
/**
* id
*
* @author fengshuonan
* @since 2024-01-17 16:24
*/
List<Long> getUserRoleIdListCurrentCompany(Long userId, Long companyId);
/**
* idid
*

View File

@ -3,6 +3,7 @@ package cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
import cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import java.util.List;
@ -12,9 +13,9 @@ import java.util.List;
* @author fengshuonan
* @since 2023/7/14 22:00
*/
public class UserRoleMemoryCache extends AbstractMemoryCacheOperator<List<Long>> {
public class UserRoleMemoryCache extends AbstractMemoryCacheOperator<List<SysUserRole>> {
public UserRoleMemoryCache(TimedCache<String, List<Long>> timedCache) {
public UserRoleMemoryCache(TimedCache<String, List<SysUserRole>> timedCache) {
super(timedCache);
}

View File

@ -2,6 +2,7 @@ package cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole;
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
import cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@ -12,9 +13,9 @@ import java.util.List;
* @author fengshuonan
* @since 2023/7/14 21:58
*/
public class UserRoleRedisCache extends AbstractRedisCacheOperator<List<Long>> {
public class UserRoleRedisCache extends AbstractRedisCacheOperator<List<SysUserRole>> {
public UserRoleRedisCache(RedisTemplate<String, List<Long>> redisTemplate) {
public UserRoleRedisCache(RedisTemplate<String, List<SysUserRole>> redisTemplate) {
super(redisTemplate);
}

View File

@ -3,6 +3,7 @@ package cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.clear;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.event.api.annotation.BusinessListener;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -20,7 +21,7 @@ import static cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants
public class UserRoleClearListener {
@Resource(name = "userRoleCache")
private CacheOperatorApi<List<Long>> userRoleCache;
private CacheOperatorApi<List<SysUserRole>> userRoleCache;
/**
*

View File

@ -41,4 +41,18 @@ public class SysUserRole extends BaseEntity {
@ChineseDescription("角色id")
private Long roleId;
/**
* 10-20-
*/
@TableField("role_type")
@ChineseDescription("角色类型10-系统角色20-公司角色")
private Integer roleType;
/**
* id
*/
@TableField(value = "role_company_id")
@ChineseDescription("角色所属公司id")
private Long roleCompanyId;
}

View File

@ -40,8 +40,7 @@ import static cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants
* @date 2023/06/10 21:26
*/
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService,
RemoveUserCallbackApi, RemoveRoleCallbackApi {
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService, RemoveUserCallbackApi, RemoveRoleCallbackApi {
@Resource
private SysRoleServiceApi sysRoleServiceApi;
@ -50,7 +49,7 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
private SysUserService sysUserService;
@Resource(name = "userRoleCache")
private CacheOperatorApi<List<Long>> userRoleCache;
private CacheOperatorApi<List<SysUserRole>> userRoleCache;
@Resource
private SysRoleLimitServiceApi sysRoleLimitServiceApi;
@ -136,25 +135,42 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
}
// 先从缓存查找用户的角色
List<Long> cachedRoleIds = userRoleCache.get(userId.toString());
if (cachedRoleIds != null) {
return cachedRoleIds;
List<SysUserRole> cachedRoleList = userRoleCache.get(userId.toString());
if (ObjectUtil.isNotEmpty(cachedRoleList)) {
return cachedRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
}
LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysUserRole::getUserId, userId);
wrapper.select(SysUserRole::getRoleId);
List<SysUserRole> sysUserRoleList = this.list(wrapper);
List<Long> userRoleQueryResult = sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
List<SysUserRole> sysUserRoleList = this.dbGetUserTotalRoleList(userId);
// 查询结果缓存起来
if (ObjectUtil.isNotEmpty(userRoleQueryResult)) {
userRoleCache.put(userId.toString(), userRoleQueryResult, SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
return userRoleQueryResult;
if (ObjectUtil.isNotEmpty(sysUserRoleList)) {
userRoleCache.put(userId.toString(), sysUserRoleList, SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
}
return userRoleQueryResult;
return sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
}
@Override
public List<Long> getUserRoleIdListCurrentCompany(Long userId, Long companyId) {
if (ObjectUtil.isEmpty(companyId)) {
return this.getUserRoleIdList(userId);
}
// 先从缓存查找用户的角色
List<SysUserRole> cachedRoleList = userRoleCache.get(userId.toString());
if (ObjectUtil.isNotEmpty(cachedRoleList)) {
return this.getUserCompanyPermissionRole(cachedRoleList, companyId);
}
List<SysUserRole> sysUserRoleList = this.dbGetUserTotalRoleList(userId);
// 查询结果缓存起来
if (ObjectUtil.isNotEmpty(sysUserRoleList)) {
userRoleCache.put(userId.toString(), sysUserRoleList, SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
}
return this.getUserCompanyPermissionRole(sysUserRoleList, companyId);
}
@Override
@ -193,22 +209,16 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
private void removeRoleAlreadyBind(SysUserRoleRequest sysUserRoleRequest) {
// 1. 获取系统中的所有角色,包括系统角色和当前公司角色
List<SysRoleDTO> roleResults = sysRoleServiceApi.getSystemRoleAndCurrentCompanyRole(
LoginContext.me().getCurrentUserCompanyId());
List<SysRoleDTO> roleResults = sysRoleServiceApi.getSystemRoleAndCurrentCompanyRole(LoginContext.me().getCurrentUserCompanyId());
if (ObjectUtil.isEmpty(roleResults)) {
return;
}
// 2. 所有系统角色id集合
Set<Long> systemRoleIdList = roleResults.stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.SYSTEM_ROLE.getCode()))
.map(SysRoleDTO::getRoleId)
.collect(Collectors.toSet());
Set<Long> systemRoleIdList = roleResults.stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.SYSTEM_ROLE.getCode())).map(SysRoleDTO::getRoleId).collect(Collectors.toSet());
// 3. 所有当前公司角色id集合
Set<Long> currentCompanyRoleIdList = roleResults.stream()
.filter(i -> i.getRoleType().equals(RoleTypeEnum.COMPANY_ROLE.getCode()))
.map(SysRoleDTO::getRoleId)
.collect(Collectors.toSet());
Set<Long> currentCompanyRoleIdList = roleResults.stream().filter(i -> i.getRoleType().equals(RoleTypeEnum.COMPANY_ROLE.getCode())).map(SysRoleDTO::getRoleId).collect(Collectors.toSet());
LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
@ -237,4 +247,50 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
}
}
/**
*
*
* @author fengshuonan
* @since 2024-01-17 16:40
*/
private List<SysUserRole> dbGetUserTotalRoleList(Long userId) {
LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysUserRole::getUserId, userId);
wrapper.select(SysUserRole::getRoleId, SysUserRole::getRoleCompanyId, SysUserRole::getRoleType);
return this.list(wrapper);
}
/**
* id
* <p>
* +
*
* @author fengshuonan
* @since 2024-01-17 16:16
*/
private List<Long> getUserCompanyPermissionRole(List<SysUserRole> paramRoles, Long userCurrentCompanyId) {
// 1. 先获取最基本的用户角色,不分公司的,每个人都有的角色
Set<Long> baseRoleIdList = paramRoles.stream().filter(i -> RoleTypeEnum.SYSTEM_ROLE.getCode().equals(i.getRoleType()) && i.getRoleCompanyId() == null).map(SysUserRole::getRoleId)
.collect(Collectors.toSet());
// 2. 获取用户当前登录公司下的角色id集合
Set<Long> currentCompanyRoleIdList = paramRoles.stream()
.filter(i -> RoleTypeEnum.COMPANY_ROLE.getCode().equals(i.getRoleType())
&& i.getRoleCompanyId() != null
&& i.getRoleCompanyId().equals(userCurrentCompanyId))
.map(SysUserRole::getRoleId)
.collect(Collectors.toSet());
// 3. 合并两个集合并返回
List<Long> resultRoleIdList = new ArrayList<>();
if (ObjectUtil.isNotEmpty(baseRoleIdList)) {
resultRoleIdList.addAll(baseRoleIdList);
}
if (ObjectUtil.isNotEmpty(currentCompanyRoleIdList)) {
resultRoleIdList.addAll(currentCompanyRoleIdList);
}
return resultRoleIdList;
}
}

View File

@ -93,7 +93,7 @@ public class UserIndexInfoService {
// 3. 获取用户的权限编码集合
List<SysMenu> userMenuList = this.fillUserPermissionCodeList(loginUser, userIndexInfo);
// 4. 获取用户的当前登录App
// 4. 获取用户的App信息和菜单信息列表
this.fillUserAppList(loginUser, userIndexInfo, userMenuList);
// 5. 构建websocket url

View File

@ -29,6 +29,7 @@ import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.sys.api.constants.SysConstants;
import cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.UserRoleMemoryCache;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -52,9 +53,9 @@ public class UserMemoryCacheAutoConfiguration {
* @since 2023/7/14 22:14
*/
@Bean
public CacheOperatorApi<List<Long>> userRoleCache() {
public CacheOperatorApi<List<SysUserRole>> userRoleCache() {
// 1小时过期
TimedCache<String, List<Long>> cache = CacheUtil.newTimedCache(1000 * SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
TimedCache<String, List<SysUserRole>> cache = CacheUtil.newTimedCache(1000 * SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
return new UserRoleMemoryCache(cache);
}

View File

@ -27,6 +27,7 @@ package cn.stylefeng.roses.kernel.sys.starter.cache.user;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil;
import cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.UserRoleRedisCache;
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -52,8 +53,8 @@ public class UserRedisCacheAutoConfiguration {
* @since 2023/7/14 22:15
*/
@Bean
public CacheOperatorApi<List<Long>> userRoleCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, List<Long>> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
public CacheOperatorApi<List<SysUserRole>> userRoleCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, List<SysUserRole>> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
return new UserRoleRedisCache(redisTemplate);
}