| /** | 
|  * Copyright (c) 2018 人人开源 All rights reserved. | 
|  * | 
|  * https://www.renren.io | 
|  * | 
|  * 版权所有,侵权必究! | 
|  */ | 
|   | 
| package com.zt.security.service; | 
|   | 
| import java.util.*; | 
| import java.util.stream.Collectors; | 
|   | 
| import javax.annotation.Resource; | 
|   | 
| import com.zt.core.sys.model.SysDept; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.cache.annotation.Cacheable; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import com.zt.common.constant.CacheKey; | 
| import com.zt.common.constant.Constant; | 
| import com.zt.common.utils.CacheUtils; | 
| import com.zt.core.context.User; | 
| import com.zt.core.context.UserContext; | 
| import com.zt.core.sys.model.SysUser; | 
| import com.zt.modules.sys.dao.SysMenuDao; | 
| import com.zt.modules.sys.dao.SysDeptDao; | 
| import com.zt.modules.sys.mapstruct.SysUserMapper; | 
| import com.zt.modules.sys.service.SysUserService; | 
| import com.zt.modules.sys.service.SysDeptService; | 
| import com.zt.security.model.SysUserToken; | 
|   | 
| /** | 
|  * shiro相关接口 | 
|  * | 
|  * @author hehz | 
|  */ | 
| @Service | 
| public class ShiroService { | 
|     @Resource | 
|     private SysUserMapper sysUserMapper; | 
|     @Autowired | 
|     private SysMenuDao sysMenuDao; | 
|     @Autowired | 
|     private SysUserService sysUserService; | 
|     @Autowired | 
|     private SysDeptService sysDeptService; | 
|     @Autowired | 
|     private SysUserTokenService sysUserTokenService; | 
|   | 
|     public SysUserToken getByToken(String token) { | 
|         return sysUserTokenService.getByToken(token); | 
|     } | 
|     public void updateTokenById(SysUserToken tokenEntity) { | 
|         sysUserTokenService.updateById(tokenEntity); | 
|     } | 
|   | 
|     @Cacheable(value = Constant.Cache.TOKEN, key = "'user:' + #userId") | 
|     public User getUser(Long userId) { | 
|         SysUser entity = sysUserService.get(userId); | 
|         User user = sysUserMapper.toUser(entity); | 
|         user.setPlatform(Constant.Sys.PLATFORM_TENANT_ID == user.getTenantId()); | 
|         user.setPermissions(getUserPermissions(user)); | 
|         SysDept dept = sysDeptService.get(user.getCompanyId()); | 
|         if (dept!=null){ | 
|             user.setUnitType(dept.getNature()); | 
|             user.setUnitCode(dept.getCode()); | 
|         } | 
|         return user; | 
|     } | 
|   | 
|     /** | 
|      * 获取用户权限列表 | 
|      */ | 
|     private Set<String> getUserPermissions(User user) { | 
|         // 系统管理员,拥有最高权限 | 
|         List<String> permissionsList; | 
|         if (user.isSuperAdmin()) { | 
|             permissionsList = sysMenuDao.getPermissionsList(); | 
|             permissionsList.add(Constant.Permissions.SUPER_ADMIN);// 超级管理员权限 | 
|         } else { | 
|             permissionsList = sysMenuDao.getUserMenuList(user.getTenantId(), user.getId()).stream() | 
|                     .map(menu -> menu.getPermissions()).collect(Collectors.toList()); | 
|         } | 
|   | 
|         // 用户权限列表 | 
|         Set<String> permsSet = new HashSet<>(); | 
|         for (String permissions : permissionsList) { | 
|             if (StringUtils.isBlank(permissions)) { | 
|                 continue; | 
|             } | 
|             permsSet.addAll(Arrays.asList(permissions.trim().split(","))); | 
|         } | 
|   | 
|         return permsSet; | 
|     } | 
| } |