|
@@ -21,10 +21,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -285,50 +282,45 @@ public class CommonCacheServiceImpl implements CommonCacheService {
|
|
|
*/
|
|
|
@Override
|
|
|
public AuthBean userAuthCache(Long userId) {
|
|
|
- AuthBean authBean = null;
|
|
|
- try {
|
|
|
- SysUser user = this.userCache(userId);
|
|
|
- if (Objects.isNull(user)) {
|
|
|
- throw ExceptionResultEnum.USER_NO_DATA.exception();
|
|
|
- }
|
|
|
- //查询用户角色和权限
|
|
|
- List<SysUserRole> sysUserRoleList = this.userRolePrivilegeCache(user.getId());
|
|
|
- if (Objects.nonNull(sysUserRoleList) && sysUserRoleList.size() > 0) {
|
|
|
- Set<Long> roleIds = sysUserRoleList.stream().map(s -> s.getRoleId()).collect(Collectors.toSet());
|
|
|
- List<SysRolePrivilege> sysRolePrivilegeList = new ArrayList<>();
|
|
|
- for (Long l : roleIds) {
|
|
|
- sysRolePrivilegeList.addAll(this.rolePrivilegeCache(l));
|
|
|
- }
|
|
|
- Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
|
|
|
- QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
|
|
|
- sysRoleQueryWrapper.lambda().in(SysRole::getId, roleIds)
|
|
|
- .eq(SysRole::getEnable, true);
|
|
|
- List<SysRole> sysRoleList = sysRoleService.list(sysRoleQueryWrapper);
|
|
|
- int count = Objects.nonNull(sysRoleList) && sysRoleList.size() > 0 ? (int) sysRoleList.stream().filter(s -> Objects.equals(s.getName(), RoleTypeEnum.ADMIN.getDesc())).count() : 0;
|
|
|
- QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
|
|
|
- if (count > 0) {//超级系统管理员
|
|
|
- sysPrivilegeQueryWrapper.lambda()
|
|
|
- .eq(SysPrivilege::getType, PrivilegeEnum.URL)
|
|
|
- .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
|
|
|
- List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
- authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
|
|
|
- } else {
|
|
|
- SysOrg org = Objects.nonNull(user.getOrgId()) ? this.orgCache(user.getOrgId()) : null;
|
|
|
- List<SysPrivilege> sysPrivilegeList = new ArrayList<>();
|
|
|
- if (privilegeIds.size() > 0) {
|
|
|
- sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds)
|
|
|
- .eq(SysPrivilege::getType, PrivilegeEnum.URL)
|
|
|
- .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
|
|
|
- sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
+ AuthBean authBean = (AuthBean) cacheService.get(SystemConstant.USER_OAUTH_CACHE, String.valueOf(userId));
|
|
|
+ if (Objects.isNull(authBean)) {
|
|
|
+ try {
|
|
|
+ SysUser user = this.userCache(userId);
|
|
|
+ Optional.ofNullable(user).orElseThrow(() -> ExceptionResultEnum.USER_NO_DATA.exception());
|
|
|
+ //查询用户角色和权限
|
|
|
+ List<SysUserRole> sysUserRoleList = this.userRolePrivilegeCache(user.getId());
|
|
|
+ if (Objects.nonNull(sysUserRoleList) && sysUserRoleList.size() > 0) {
|
|
|
+ Set<Long> roleIds = sysUserRoleList.stream().map(s -> s.getRoleId()).collect(Collectors.toSet());
|
|
|
+ List<SysRolePrivilege> sysRolePrivilegeList = new ArrayList<>();
|
|
|
+ for (Long l : roleIds) {
|
|
|
+ sysRolePrivilegeList.addAll(this.rolePrivilegeCache(l));
|
|
|
}
|
|
|
- authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), org);
|
|
|
+ Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
|
|
|
+ List<SysRole> sysRoleList = sysRoleService.list(new QueryWrapper<SysRole>().lambda().in(SysRole::getId, roleIds).eq(SysRole::getEnable, true));
|
|
|
+ int count = Objects.nonNull(sysRoleList) && sysRoleList.size() > 0 ? (int) sysRoleList.stream().filter(s -> Objects.equals(s.getName(), RoleTypeEnum.ADMIN.getDesc())).count() : 0;
|
|
|
+ QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
|
|
|
+ if (count > 0) {//超级系统管理员
|
|
|
+ sysPrivilegeQueryWrapper.lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
|
|
|
+ List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
+ authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
|
|
|
+ } else {
|
|
|
+ SysOrg org = Objects.nonNull(user.getOrgId()) ? this.orgCache(user.getOrgId()) : null;
|
|
|
+ List<SysPrivilege> sysPrivilegeList = new ArrayList<>();
|
|
|
+ if (privilegeIds.size() > 0) {
|
|
|
+ sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds).eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
|
|
|
+ sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
+ }
|
|
|
+ authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), org);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(authBean)) {
|
|
|
+ cacheService.put(SystemConstant.USER_OAUTH_CACHE, String.valueOf(userId), authBean);
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("添加用户鉴权缓存失败");
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(SystemConstant.LOG_ERROR, e);
|
|
|
- throw ExceptionResultEnum.ERROR.exception("添加用户鉴权缓存失败");
|
|
|
}
|
|
|
- cacheService.put(SystemConstant.USER_OAUTH_CACHE, String.valueOf(userId), authBean);
|
|
|
return authBean;
|
|
|
}
|
|
|
|