|
@@ -1,171 +1,170 @@
|
|
|
-package cn.tr.module.sys.oauth2.service;
|
|
|
|
|
-
|
|
|
|
|
-import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
-import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
|
|
-import cn.tr.core.exception.ServiceException;
|
|
|
|
|
-import cn.tr.core.exception.TRExcCode;
|
|
|
|
|
-import cn.tr.module.sys.tenant.service.ISysTenantService;
|
|
|
|
|
-import cn.tr.module.sys.user.dto.SysMenuDTO;
|
|
|
|
|
-import cn.tr.module.sys.user.dto.SysRoleDTO;
|
|
|
|
|
-import cn.tr.module.sys.user.dto.SysUserPortalListDTO;
|
|
|
|
|
-import cn.tr.module.sys.user.enums.MenuEnum;
|
|
|
|
|
-import cn.tr.module.sys.user.service.ISysMenuService;
|
|
|
|
|
-import cn.tr.module.sys.user.service.ISysPortalMenuService;
|
|
|
|
|
-import cn.tr.module.sys.user.service.ISysUserPortalService;
|
|
|
|
|
-import cn.tr.module.sys.user.service.ISysUserRoleService;
|
|
|
|
|
-import cn.tr.module.sys.user.vo.RouteItemVO;
|
|
|
|
|
-import cn.tr.plugin.security.utils.SaTokenUtils;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
-
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * @ClassName : CurrentUserService
|
|
|
|
|
- * @Description :
|
|
|
|
|
- * @Author : LF
|
|
|
|
|
- * @Date: 2023年04月07日
|
|
|
|
|
- */
|
|
|
|
|
-
|
|
|
|
|
-@Component
|
|
|
|
|
-public class CurrentUserService {
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ISysTenantService tenantService;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ISysMenuService menuService;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ISysPortalMenuService portalMenuService;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ISysUserPortalService userPortalService;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ISysUserRoleService userRoleService;
|
|
|
|
|
-
|
|
|
|
|
- @Value("${tr.tenant.enable}")
|
|
|
|
|
- private Boolean tenantEnable;
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取当前用户权限
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public Collection<String> currentUserPermission(String userId){
|
|
|
|
|
- if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
- //用户所在租户拥有的权限
|
|
|
|
|
- Set<String> tenantPermissions = tenantService.findTenantPermission();
|
|
|
|
|
- ;
|
|
|
|
|
- //用户角色所拥有的权限
|
|
|
|
|
- Set<String> rolePermissions = menuService.findUserPermission(userId);
|
|
|
|
|
-
|
|
|
|
|
- return CollectionUtil.intersection(tenantPermissions,rolePermissions);
|
|
|
|
|
- }else {
|
|
|
|
|
- return menuService.findUserPermission(userId);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- public Collection<String> currentUserRoleCode(){
|
|
|
|
|
- String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
- List<SysRoleDTO> roles = userRoleService.findAllRolesByUserId(currentUserId);
|
|
|
|
|
- return roles.stream().
|
|
|
|
|
- map(SysRoleDTO::getCode)
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取当前用户菜单
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public List<RouteItemVO> currentUserMenus(String portalId){
|
|
|
|
|
- String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
- Set<SysMenuDTO> tenantMenus =null;
|
|
|
|
|
- if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
- tenantMenus = tenantService.currentTenantMenus();
|
|
|
|
|
- }
|
|
|
|
|
- //查询所有可匿名访问的菜单
|
|
|
|
|
-// List<SysMenuDTO> anonymousMenus = menuService.findAnonymousMenus();
|
|
|
|
|
- Set<SysMenuDTO> roleMenus = menuService.findUserMenus(currentUserId);
|
|
|
|
|
- //当未传门户id时,使用默认门户
|
|
|
|
|
- if(StrUtil.isEmpty(portalId)){
|
|
|
|
|
- List<SysUserPortalListDTO> portalList = currentUserPortals();
|
|
|
|
|
- if (CollectionUtil.isNotEmpty(portalList)) {
|
|
|
|
|
- portalId=portalList.stream()
|
|
|
|
|
- .filter(portal->Boolean.TRUE.equals(portal.getIsDefault()))
|
|
|
|
|
- .map(SysUserPortalListDTO::getId)
|
|
|
|
|
- .findFirst()
|
|
|
|
|
- .orElse(CollectionUtil.getFirst(portalList).getId());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- Set<SysMenuDTO> portalMenus =StrUtil.isEmpty(portalId)?Collections.emptySet():new HashSet<>(portalMenuService.findAllMenusByPortalId(portalId));
|
|
|
|
|
- // 交集(门面菜单,租户菜单,角色菜单)
|
|
|
|
|
- Collection<SysMenuDTO> allMenus=new ArrayList<>();
|
|
|
|
|
- if(Boolean.TRUE.equals(tenantEnable)){
|
|
|
|
|
- allMenus = CollectionUtil.intersection(tenantMenus, roleMenus,portalMenus);
|
|
|
|
|
- }else if(CollectionUtil.isNotEmpty(portalMenus)){
|
|
|
|
|
- allMenus = CollectionUtil.intersection(roleMenus,portalMenus);
|
|
|
|
|
- }else {
|
|
|
|
|
- allMenus=roleMenus;
|
|
|
|
|
- }
|
|
|
|
|
-// if(CollectionUtil.isNotEmpty(anonymousMenus)){
|
|
|
|
|
-// allMenus.addAll(anonymousMenus);
|
|
|
|
|
|
|
+//package cn.tr.module.sys.oauth2.service;
|
|
|
|
|
+//
|
|
|
|
|
+//import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
+//import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
+//import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+//import cn.tr.core.exception.ServiceException;
|
|
|
|
|
+//import cn.tr.core.exception.TRExcCode;
|
|
|
|
|
+//import cn.tr.module.sys.tenant.service.ISysTenantService;
|
|
|
|
|
+//import cn.tr.module.sys.user.dto.SysRoleDTO;
|
|
|
|
|
+//import cn.tr.module.sys.user.dto.SysUserPortalListDTO;
|
|
|
|
|
+//import cn.tr.module.sys.user.enums.MenuEnum;
|
|
|
|
|
+//import cn.tr.module.sys.user.service.ISysMenuService;
|
|
|
|
|
+//import cn.tr.module.sys.user.service.ISysPortalMenuService;
|
|
|
|
|
+//import cn.tr.module.sys.user.service.ISysUserPortalService;
|
|
|
|
|
+//import cn.tr.module.sys.user.service.ISysUserRoleService;
|
|
|
|
|
+//import cn.tr.module.sys.user.vo.RouteItemVO;
|
|
|
|
|
+//import cn.tr.plugin.security.utils.SaTokenUtils;
|
|
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+//import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
|
|
+//
|
|
|
|
|
+//import java.util.*;
|
|
|
|
|
+//import java.util.stream.Collectors;
|
|
|
|
|
+//
|
|
|
|
|
+///**
|
|
|
|
|
+// * @ClassName : CurrentUserService
|
|
|
|
|
+// * @Description :
|
|
|
|
|
+// * @Author : LF
|
|
|
|
|
+// * @Date: 2023年04月07日
|
|
|
|
|
+// */
|
|
|
|
|
+//
|
|
|
|
|
+//@Component
|
|
|
|
|
+//public class CurrentUserService {
|
|
|
|
|
+// @Autowired
|
|
|
|
|
+// private ISysTenantService tenantService;
|
|
|
|
|
+// @Autowired
|
|
|
|
|
+// private ISysMenuService menuService;
|
|
|
|
|
+// @Autowired
|
|
|
|
|
+// private ISysPortalMenuService portalMenuService;
|
|
|
|
|
+// @Autowired
|
|
|
|
|
+// private ISysUserPortalService userPortalService;
|
|
|
|
|
+// @Autowired
|
|
|
|
|
+// private ISysUserRoleService userRoleService;
|
|
|
|
|
+//
|
|
|
|
|
+// @Value("${tr.tenant.enable}")
|
|
|
|
|
+// private Boolean tenantEnable;
|
|
|
|
|
+// /**
|
|
|
|
|
+// * 获取当前用户权限
|
|
|
|
|
+// * @return
|
|
|
|
|
+// */
|
|
|
|
|
+// public Collection<String> currentUserPermission(String userId){
|
|
|
|
|
+// if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
+// //用户所在租户拥有的权限
|
|
|
|
|
+// Set<String> tenantPermissions = tenantService.findTenantPermission();
|
|
|
|
|
+// ;
|
|
|
|
|
+// //用户角色所拥有的权限
|
|
|
|
|
+// Set<String> rolePermissions = menuService.findUserPermission(userId);
|
|
|
|
|
+//
|
|
|
|
|
+// return CollectionUtil.intersection(tenantPermissions,rolePermissions);
|
|
|
|
|
+// }else {
|
|
|
|
|
+// return menuService.findUserPermission(userId);
|
|
|
// }
|
|
// }
|
|
|
- if(CollectionUtil.isEmpty(allMenus)){
|
|
|
|
|
- return new ArrayList<>();
|
|
|
|
|
- }
|
|
|
|
|
- return allMenus
|
|
|
|
|
- .stream()
|
|
|
|
|
- .collect(Collectors.groupingBy(SysMenuDTO::getId, Collectors.collectingAndThen(Collectors.toList(), CollUtil::getFirst)))
|
|
|
|
|
- //未重复的菜单
|
|
|
|
|
- .values()
|
|
|
|
|
- .stream()
|
|
|
|
|
- .filter(menu -> !StrUtil.equals(MenuEnum.button.name(), menu.getMenuType()))
|
|
|
|
|
- .filter(menu-> Boolean.TRUE.equals(menu.getVisible()))
|
|
|
|
|
- .map(menuService::convertToRoute)
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public List<SysUserPortalListDTO> currentUserPortals(){
|
|
|
|
|
- String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
- List<SysUserPortalListDTO> portals = userPortalService.findPortalsByUserId(currentUserId);
|
|
|
|
|
- if (CollectionUtil.size(portals)==0) {
|
|
|
|
|
- if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
- throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"当前用户未设置门户信息");
|
|
|
|
|
- }else {
|
|
|
|
|
- return new ArrayList<>();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
- return portals;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取当前用户菜单
|
|
|
|
|
- * @auth: lisen
|
|
|
|
|
- * @return List<SysMenuDTO>
|
|
|
|
|
- */
|
|
|
|
|
- public Collection<SysMenuDTO> currentUserAllMenus(String portalId){
|
|
|
|
|
- String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
- Set<SysMenuDTO> tenantMenus =null;
|
|
|
|
|
- if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
- tenantMenus = tenantService.currentTenantMenus();
|
|
|
|
|
- }
|
|
|
|
|
- Set<SysMenuDTO> roleMenus = menuService.findUserMenus(currentUserId);
|
|
|
|
|
- //当未传门户id时,使用默认门户
|
|
|
|
|
- if(StrUtil.isEmpty(portalId)){
|
|
|
|
|
- List<SysUserPortalListDTO> portalList = currentUserPortals();
|
|
|
|
|
- if (CollectionUtil.isNotEmpty(portalList)) {
|
|
|
|
|
- portalId=portalList.stream()
|
|
|
|
|
- .filter(portal->Boolean.TRUE.equals(portal.getIsDefault()))
|
|
|
|
|
- .map(SysUserPortalListDTO::getId)
|
|
|
|
|
- .findFirst()
|
|
|
|
|
- .orElse(CollectionUtil.getFirst(portalList).getId());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- Set<SysMenuDTO> portalMenus =StrUtil.isEmpty(portalId)?Collections.emptySet():new HashSet<>(portalMenuService.findAllMenusByPortalId(portalId));
|
|
|
|
|
- // 交集(门面菜单,租户菜单,角色菜单)
|
|
|
|
|
- if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
- return CollectionUtil.intersection(tenantMenus, roleMenus,portalMenus);
|
|
|
|
|
- }
|
|
|
|
|
- return CollectionUtil.intersection(roleMenus,portalMenus);
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
|
|
+//
|
|
|
|
|
+// };
|
|
|
|
|
+//
|
|
|
|
|
+// public Collection<String> currentUserRoleCode(){
|
|
|
|
|
+// String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
+// List<SysRoleDTO> roles = userRoleService.findAllRolesByUserId(currentUserId);
|
|
|
|
|
+// return roles.stream().
|
|
|
|
|
+// map(SysRoleDTO::getCode)
|
|
|
|
|
+// .collect(Collectors.toList());
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// /**
|
|
|
|
|
+// * 获取当前用户菜单
|
|
|
|
|
+// * @return
|
|
|
|
|
+// */
|
|
|
|
|
+// public List<RouteItemVO> currentUserMenus(String portalId){
|
|
|
|
|
+// String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
+// Set<SysMenuDTO> tenantMenus =null;
|
|
|
|
|
+// if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
+// tenantMenus = tenantService.currentTenantMenus();
|
|
|
|
|
+// }
|
|
|
|
|
+// //查询所有可匿名访问的菜单
|
|
|
|
|
+//// List<SysMenuDTO> anonymousMenus = menuService.findAnonymousMenus();
|
|
|
|
|
+// Set<SysMenuDTO> roleMenus = menuService.findUserMenus(currentUserId);
|
|
|
|
|
+// //当未传门户id时,使用默认门户
|
|
|
|
|
+// if(StrUtil.isEmpty(portalId)){
|
|
|
|
|
+// List<SysUserPortalListDTO> portalList = currentUserPortals();
|
|
|
|
|
+// if (CollectionUtil.isNotEmpty(portalList)) {
|
|
|
|
|
+// portalId=portalList.stream()
|
|
|
|
|
+// .filter(portal->Boolean.TRUE.equals(portal.getIsDefault()))
|
|
|
|
|
+// .map(SysUserPortalListDTO::getId)
|
|
|
|
|
+// .findFirst()
|
|
|
|
|
+// .orElse(CollectionUtil.getFirst(portalList).getId());
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// Set<SysMenuDTO> portalMenus =StrUtil.isEmpty(portalId)?Collections.emptySet():new HashSet<>(portalMenuService.findAllMenusByPortalId(portalId));
|
|
|
|
|
+// // 交集(门面菜单,租户菜单,角色菜单)
|
|
|
|
|
+// Collection<SysMenuDTO> allMenus=new ArrayList<>();
|
|
|
|
|
+// if(Boolean.TRUE.equals(tenantEnable)){
|
|
|
|
|
+// allMenus = CollectionUtil.intersection(tenantMenus, roleMenus,portalMenus);
|
|
|
|
|
+// }else if(CollectionUtil.isNotEmpty(portalMenus)){
|
|
|
|
|
+// allMenus = CollectionUtil.intersection(roleMenus,portalMenus);
|
|
|
|
|
+// }else {
|
|
|
|
|
+// allMenus=roleMenus;
|
|
|
|
|
+// }
|
|
|
|
|
+//// if(CollectionUtil.isNotEmpty(anonymousMenus)){
|
|
|
|
|
+//// allMenus.addAll(anonymousMenus);
|
|
|
|
|
+//// }
|
|
|
|
|
+// if(CollectionUtil.isEmpty(allMenus)){
|
|
|
|
|
+// return new ArrayList<>();
|
|
|
|
|
+// }
|
|
|
|
|
+// return allMenus
|
|
|
|
|
+// .stream()
|
|
|
|
|
+// .collect(Collectors.groupingBy(SysMenuDTO::getId, Collectors.collectingAndThen(Collectors.toList(), CollUtil::getFirst)))
|
|
|
|
|
+// //未重复的菜单
|
|
|
|
|
+// .values()
|
|
|
|
|
+// .stream()
|
|
|
|
|
+// .filter(menu -> !StrUtil.equals(MenuEnum.button.name(), menu.getMenuType()))
|
|
|
|
|
+// .filter(menu-> Boolean.TRUE.equals(menu.getVisible()))
|
|
|
|
|
+// .map(menuService::convertToRoute)
|
|
|
|
|
+// .collect(Collectors.toList());
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// public List<SysUserPortalListDTO> currentUserPortals(){
|
|
|
|
|
+// String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
+// List<SysUserPortalListDTO> portals = userPortalService.findPortalsByUserId(currentUserId);
|
|
|
|
|
+// if (CollectionUtil.size(portals)==0) {
|
|
|
|
|
+// if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
+// throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"当前用户未设置门户信息");
|
|
|
|
|
+// }else {
|
|
|
|
|
+// return new ArrayList<>();
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// }
|
|
|
|
|
+// return portals;
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// /**
|
|
|
|
|
+// * 获取当前用户菜单
|
|
|
|
|
+// * @auth: lisen
|
|
|
|
|
+// * @return List<SysMenuDTO>
|
|
|
|
|
+// */
|
|
|
|
|
+// public Collection<SysMenuDTO> currentUserAllMenus(String portalId){
|
|
|
|
|
+// String currentUserId = String.valueOf(SaTokenUtils.getStpUtil().getLoginId());
|
|
|
|
|
+// Set<SysMenuDTO> tenantMenus =null;
|
|
|
|
|
+// if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
+// tenantMenus = tenantService.currentTenantMenus();
|
|
|
|
|
+// }
|
|
|
|
|
+// Set<SysMenuDTO> roleMenus = menuService.findUserMenus(currentUserId);
|
|
|
|
|
+// //当未传门户id时,使用默认门户
|
|
|
|
|
+// if(StrUtil.isEmpty(portalId)){
|
|
|
|
|
+// List<SysUserPortalListDTO> portalList = currentUserPortals();
|
|
|
|
|
+// if (CollectionUtil.isNotEmpty(portalList)) {
|
|
|
|
|
+// portalId=portalList.stream()
|
|
|
|
|
+// .filter(portal->Boolean.TRUE.equals(portal.getIsDefault()))
|
|
|
|
|
+// .map(SysUserPortalListDTO::getId)
|
|
|
|
|
+// .findFirst()
|
|
|
|
|
+// .orElse(CollectionUtil.getFirst(portalList).getId());
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// Set<SysMenuDTO> portalMenus =StrUtil.isEmpty(portalId)?Collections.emptySet():new HashSet<>(portalMenuService.findAllMenusByPortalId(portalId));
|
|
|
|
|
+// // 交集(门面菜单,租户菜单,角色菜单)
|
|
|
|
|
+// if (Boolean.TRUE.equals(tenantEnable)) {
|
|
|
|
|
+// return CollectionUtil.intersection(tenantMenus, roleMenus,portalMenus);
|
|
|
|
|
+// }
|
|
|
|
|
+// return CollectionUtil.intersection(roleMenus,portalMenus);
|
|
|
|
|
+// }
|
|
|
|
|
+//}
|