33afc957dd46ec3810fb5b91099194c9c233a9f1.svn-base
8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.modules.sys.utils;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.sys.dao.AreaDao;
import com.thinkgem.jeesite.modules.sys.dao.MenuDao;
import com.thinkgem.jeesite.modules.sys.dao.OfficeDao;
import com.thinkgem.jeesite.modules.sys.dao.RoleDao;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.entity.Menu;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm.Principal;
/**
* 用户工具类
* @author ThinkGem
* @version 2013-12-05
*/
public class UserUtils {
private static UserDao userDao = SpringContextHolder.getBean(UserDao.class);
private static RoleDao roleDao = SpringContextHolder.getBean(RoleDao.class);
private static MenuDao menuDao = SpringContextHolder.getBean(MenuDao.class);
private static AreaDao areaDao = SpringContextHolder.getBean(AreaDao.class);
private static OfficeDao officeDao = SpringContextHolder.getBean(OfficeDao.class);
public static final String USER_CACHE = "userCache";
public static final String USER_CACHE_ID_ = "id_";
public static final String USER_CACHE_LOGIN_NAME_ = "ln";
public static final String USER_CACHE_LIST_BY_OFFICE_ID_ = "oid_";
public static final String CACHE_ROLE_LIST = "roleList";
public static final String CACHE_MENU_LIST = "menuList";
public static final String CACHE_AREA_LIST = "areaList";
public static final String CACHE_OFFICE_LIST = "officeList";
public static final String CACHE_OFFICE_ALL_LIST = "officeAllList";
/**
* 根据ID获取用户
* @param id
* @return 取不到返回null
*/
public static User get(String id){
User user = (User)CacheUtils.get(USER_CACHE, USER_CACHE_ID_ + id);
if (user == null){
user = userDao.get(id);
if (user == null){
return null;
}
user.setRoleList(roleDao.findList(new Role(user)));
CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user);
CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
}
return user;
}
/**
* 根据登录名获取用户
* @param loginName
* @return 取不到返回null
*/
public static User getByLoginName(String loginName){
User user = (User)CacheUtils.get(USER_CACHE, USER_CACHE_LOGIN_NAME_ + loginName);
if (user == null){
user = userDao.getByLoginName(new User(null, loginName));
if (user == null){
return null;
}
user.setRoleList(roleDao.findList(new Role(user)));
CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user);
CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
}
return user;
}
/**
* 根据登录名获取用户
* @param loginName
* @return 取不到返回null
*/
public static User getByName(String loginName){
User user = (User)CacheUtils.get(USER_CACHE, USER_CACHE_LOGIN_NAME_ + loginName);
if (user == null){
user = userDao.getByName(new User(null, loginName));
if (user == null){
return null;
}
user.setRoleList(roleDao.findList(new Role(user)));
CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user);
CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
}
return user;
}
/**
* 清除当前用户缓存
*/
public static void clearCache(){
removeCache(CACHE_ROLE_LIST);
removeCache(CACHE_MENU_LIST);
removeCache(CACHE_AREA_LIST);
removeCache(CACHE_OFFICE_LIST);
removeCache(CACHE_OFFICE_ALL_LIST);
UserUtils.clearCache(getUser());
}
/**
* 清除指定用户缓存
* @param user
*/
public static void clearCache(User user){
CacheUtils.remove(USER_CACHE, USER_CACHE_ID_ + user.getId());
CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName());
CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getOldLoginName());
if (user.getOffice() != null && user.getOffice().getId() != null){
CacheUtils.remove(USER_CACHE, USER_CACHE_LIST_BY_OFFICE_ID_ + user.getOffice().getId());
}
}
/**
* 获取当前用户
* @return 取不到返回 new User()
*/
public static User getUser(){
Principal principal = getPrincipal();
if (principal!=null){
User user = get(principal.getId());
if (user != null){
return user;
}
return new User();
}else {
// 如果没有登录,则返回管理员的User对象。
User user = get("1");
if (user != null){
return user;
}
return new User();
}
}
/**
* 获取当前用户角色列表
* @return
*/
public static List<Role> getRoleList(){
@SuppressWarnings("unchecked")
List<Role> roleList = (List<Role>)getCache(CACHE_ROLE_LIST);
if (roleList == null){
User user = getUser();
if (user.isAdmin()){
roleList = roleDao.findAllList(new Role());
}else{
Role role = new Role();
role.getSqlMap().put("dsf", BaseService.dataScopeFilter(user.getCurrentUser(), "o", "u"));
roleList = roleDao.findList(role);
}
putCache(CACHE_ROLE_LIST, roleList);
}
return roleList;
}
/**
* 获取当前用户授权菜单
* @return
*/
public static List<Menu> getMenuList(){
@SuppressWarnings("unchecked")
List<Menu> menuList = (List<Menu>)getCache(CACHE_MENU_LIST);
if (menuList == null){
User user = getUser();
if (user.isAdmin()){
menuList = menuDao.findAllList(new Menu());
}else{
Menu m = new Menu();
m.setUserId(user.getId());
menuList = menuDao.findByUserId(m);
}
putCache(CACHE_MENU_LIST, menuList);
}
return menuList;
}
/**
* 获取当前用户授权的区域
* @return
*/
public static List<Area> getAreaList(){
@SuppressWarnings("unchecked")
List<Area> areaList = (List<Area>)getCache(CACHE_AREA_LIST);
if (areaList == null){
areaList = areaDao.findAllList(new Area());
putCache(CACHE_AREA_LIST, areaList);
}
return areaList;
}
/**
* 获取当前用户有权限访问的部门
* @return
*/
public static List<Office> getOfficeList(){
@SuppressWarnings("unchecked")
List<Office> officeList = (List<Office>)getCache(CACHE_OFFICE_LIST);
if (officeList == null){
User user = getUser();
if (user.isAdmin()){
officeList = officeDao.findAllList(new Office());
}else{
Office office = new Office();
office.getSqlMap().put("dsf", BaseService.dataScopeFilter(user, "a", ""));
officeList = officeDao.findList(office);
}
putCache(CACHE_OFFICE_LIST, officeList);
}
return officeList;
}
/**
* 获取当前用户有权限访问的部门
* @return
*/
public static List<Office> getOfficeAllList(){
@SuppressWarnings("unchecked")
List<Office> officeList = (List<Office>)getCache(CACHE_OFFICE_ALL_LIST);
if (officeList == null){
officeList = officeDao.findAllList(new Office());
}
return officeList;
}
/**
* 获取授权主要对象
*/
public static Subject getSubject(){
return SecurityUtils.getSubject();
}
/**
* 获取当前登录者对象
*/
public static Principal getPrincipal(){
try{
Subject subject = SecurityUtils.getSubject();
Principal principal = (Principal)subject.getPrincipal();
if (principal != null){
return principal;
}
// subject.logout();
}catch (UnavailableSecurityManagerException e) {
}catch (InvalidSessionException e){
}
return null;
}
public static Session getSession(){
try{
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession(false);
if (session == null){
session = subject.getSession();
}
if (session != null){
return session;
}
// subject.logout();
}catch (InvalidSessionException e){
}
return null;
}
// ============== User Cache ==============
public static Object getCache(String key) {
return getCache(key, null);
}
public static Object getCache(String key, Object defaultValue) {
// Object obj = getCacheMap().get(key);
Object obj = getSession().getAttribute(key);
return obj==null?defaultValue:obj;
}
public static void putCache(String key, Object value) {
// getCacheMap().put(key, value);
getSession().setAttribute(key, value);
}
public static void removeCache(String key) {
// getCacheMap().remove(key);
getSession().removeAttribute(key);
}
// public static Map<String, Object> getCacheMap(){
// Principal principal = getPrincipal();
// if(principal!=null){
// return principal.getCacheMap();
// }
// return new HashMap<String, Object>();
// }
}