commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.modular.auth.factory; |
|
26 |
|
|
27 |
import cn.hutool.core.collection.CollectionUtil; |
|
28 |
import cn.hutool.core.convert.Convert; |
|
29 |
import cn.hutool.core.date.DateTime; |
|
30 |
import cn.hutool.core.lang.Dict; |
|
31 |
import cn.hutool.core.util.ObjectUtil; |
|
32 |
import cn.hutool.core.util.StrUtil; |
|
33 |
import cn.hutool.extra.spring.SpringUtil; |
|
34 |
import vip.xiaonuo.core.consts.CommonConstant; |
|
35 |
import vip.xiaonuo.core.context.constant.ConstantContextHolder; |
|
36 |
import vip.xiaonuo.core.exception.ServiceException; |
|
37 |
import vip.xiaonuo.core.exception.enums.ServerExceptionEnum; |
|
38 |
import vip.xiaonuo.core.pojo.login.LoginEmpInfo; |
|
39 |
import vip.xiaonuo.core.pojo.login.SysLoginUser; |
|
40 |
import vip.xiaonuo.core.pojo.node.LoginMenuTreeNode; |
|
41 |
import vip.xiaonuo.core.tenant.consts.TenantConstants; |
|
42 |
import vip.xiaonuo.core.tenant.context.TenantCodeHolder; |
|
43 |
import vip.xiaonuo.core.tenant.context.TenantDbNameHolder; |
|
44 |
import vip.xiaonuo.core.util.HttpServletUtil; |
|
45 |
import vip.xiaonuo.core.util.IpAddressUtil; |
|
46 |
import vip.xiaonuo.core.util.UaUtil; |
|
47 |
import vip.xiaonuo.sys.modular.app.service.SysAppService; |
|
48 |
import vip.xiaonuo.sys.modular.emp.service.SysEmpService; |
|
49 |
import vip.xiaonuo.sys.modular.menu.entity.SysMenu; |
|
50 |
import vip.xiaonuo.sys.modular.menu.service.SysMenuService; |
|
51 |
import vip.xiaonuo.sys.modular.role.service.SysRoleMenuService; |
|
52 |
import vip.xiaonuo.sys.modular.role.service.SysRoleService; |
|
53 |
import vip.xiaonuo.sys.modular.user.service.SysUserService; |
|
54 |
|
|
55 |
import javax.servlet.http.HttpServletRequest; |
|
56 |
import java.util.List; |
|
57 |
import java.util.Map; |
|
58 |
import java.util.stream.Collectors; |
|
59 |
|
|
60 |
/** |
|
61 |
* 登录用户工厂类 |
|
62 |
* |
|
63 |
* @author xuyuxiang |
|
64 |
* @date 2020/3/13 14:58 |
|
65 |
*/ |
|
66 |
public class LoginUserFactory { |
|
67 |
|
|
68 |
private static final SysUserService sysUserService = SpringUtil.getBean(SysUserService.class); |
|
69 |
|
|
70 |
private static final SysEmpService sysEmpService = SpringUtil.getBean(SysEmpService.class); |
|
71 |
|
|
72 |
private static final SysAppService sysAppService = SpringUtil.getBean(SysAppService.class); |
|
73 |
|
|
74 |
private static final SysMenuService sysMenuService = SpringUtil.getBean(SysMenuService.class); |
|
75 |
|
|
76 |
private static final SysRoleService sysRoleService = SpringUtil.getBean(SysRoleService.class); |
|
77 |
|
|
78 |
private static final SysRoleMenuService sysRoleMenuService = SpringUtil.getBean(SysRoleMenuService.class); |
|
79 |
|
|
80 |
/** |
|
81 |
* 填充登录用户相关信息 |
|
82 |
* |
|
83 |
* @author xuyuxiang yubaoshan |
|
84 |
* @date 2020/3/13 15:01 |
|
85 |
*/ |
|
86 |
public static void fillLoginUserInfo(SysLoginUser sysLoginUser) { |
|
87 |
HttpServletRequest request = HttpServletUtil.getRequest(); |
|
88 |
if (ObjectUtil.isNotNull(request)) { |
|
89 |
sysLoginUser.setLastLoginIp(IpAddressUtil.getIp(request)); |
|
90 |
sysLoginUser.setLastLoginTime(DateTime.now().toString()); |
|
91 |
sysLoginUser.setLastLoginAddress(IpAddressUtil.getAddress(request)); |
|
92 |
sysLoginUser.setLastLoginBrowser(UaUtil.getBrowser(request)); |
|
93 |
sysLoginUser.setLastLoginOs(UaUtil.getOs(request)); |
|
94 |
Long userId = sysLoginUser.getId(); |
|
95 |
|
|
96 |
// 员工信息 |
|
97 |
LoginEmpInfo loginEmpInfo = sysEmpService.getLoginEmpInfo(userId); |
|
98 |
sysLoginUser.setLoginEmpInfo(loginEmpInfo); |
|
99 |
|
|
100 |
// 角色信息 |
|
101 |
List<Dict> roles = sysRoleService.getLoginRoles(userId); |
|
102 |
sysLoginUser.setRoles(roles); |
|
103 |
|
|
104 |
// 获取角色id集合 |
|
105 |
List<Long> roleIdList = roles.stream().map(dict -> Convert.toLong(dict.get(CommonConstant.ID))).collect(Collectors.toList()); |
|
106 |
|
|
107 |
// 获取菜单id集合 |
|
108 |
List<Long> menuIdList = sysRoleMenuService.getRoleMenuIdList(roleIdList); |
|
109 |
|
|
110 |
// 权限信息 |
|
111 |
List<String> permissions = sysMenuService.getLoginPermissions(userId, menuIdList); |
|
112 |
sysLoginUser.setPermissions(permissions); |
|
113 |
|
|
114 |
// 数据范围信息 |
|
115 |
List<Long> dataScopes = sysUserService.getUserDataScopeIdList(userId, loginEmpInfo.getOrgId()); |
|
116 |
sysLoginUser.setDataScopes(dataScopes); |
|
117 |
|
|
118 |
// 具备应用信息(多系统,默认激活一个,可根据系统切换菜单),返回的结果中第一个为激活的系统 |
|
119 |
List<Dict> apps = sysAppService.getLoginApps(userId, roleIdList); |
|
120 |
sysLoginUser.setApps(apps); |
|
121 |
|
|
122 |
// 如果根本没有应用信息,则没有菜单信息 |
|
123 |
if (ObjectUtil.isEmpty(apps)) { |
|
124 |
sysLoginUser.setMenus(CollectionUtil.newArrayList()); |
|
125 |
} else { |
|
126 |
//AntDesign菜单信息,根据人获取,用于登录后展示菜单树,默认获取默认激活的系统的菜单 |
|
127 |
String defaultActiveAppCode = apps.get(0).getStr(CommonConstant.CODE); |
|
128 |
List<SysMenu> loginMenus = sysMenuService.getLoginMenus(userId, defaultActiveAppCode, menuIdList); |
|
129 |
Map<String, List<SysMenu>> collect = loginMenus.stream().collect(Collectors.groupingBy(SysMenu::getApplication)); |
|
130 |
List<SysMenu> tempList = collect.get(defaultActiveAppCode); |
|
131 |
List<LoginMenuTreeNode> loginMenuTreeNodes = sysMenuService.convertSysMenuToLoginMenu(tempList); |
|
132 |
sysLoginUser.setMenus(loginMenuTreeNodes); |
|
133 |
} |
|
134 |
|
|
135 |
//如果开启了多租户功能,则设置当前登录用户的租户标识 |
|
136 |
if (ConstantContextHolder.getTenantOpenFlag()) { |
|
137 |
String tenantCode = TenantCodeHolder.get(); |
|
138 |
String dataBaseName = TenantDbNameHolder.get(); |
|
139 |
if (StrUtil.isNotBlank(tenantCode) && StrUtil.isNotBlank(dataBaseName)) { |
|
140 |
Dict tenantInfo = Dict.create(); |
|
141 |
tenantInfo.set(TenantConstants.TENANT_CODE, tenantCode); |
|
142 |
tenantInfo.set(TenantConstants.TENANT_DB_NAME, dataBaseName); |
|
143 |
sysLoginUser.setTenants(tenantInfo); |
|
144 |
} |
|
145 |
//注意,这里remove不代表所有情况,在aop remove |
|
146 |
TenantCodeHolder.remove(); |
|
147 |
TenantDbNameHolder.remove(); |
|
148 |
} |
|
149 |
|
|
150 |
} else { |
|
151 |
throw new ServiceException(ServerExceptionEnum.REQUEST_EMPTY); |
|
152 |
} |
|
153 |
} |
|
154 |
} |