1
2
3
4
5 package gboat2.web.business.impl;
6
7 import gboat2.base.core.cache.ICacheService;
8 import gboat2.base.core.dao.QuerySupport;
9 import gboat2.base.core.service.BaseService;
10 import gboat2.base.bridge.exception.DefaultGboatNestedException;
11 import gboat2.web.Constants;
12 import gboat2.web.business.IAuthorityBusiness;
13 import gboat2.web.business.IDataLevelAuthorityBusiness;
14 import gboat2.web.business.IGroupBusiness;
15 import gboat2.web.business.IOperationBusiness;
16 import gboat2.web.business.IResourceBusiness;
17 import gboat2.web.business.IRoleBusiness;
18 import gboat2.web.model.Authority;
19 import gboat2.web.model.AuthorityDataLevelResourceVO;
20 import gboat2.web.model.AuthorityOperationResourceVO;
21 import gboat2.web.model.AuthorityOperationVO;
22 import gboat2.web.model.AuthorityResourceVO;
23 import gboat2.web.model.DataLevelAuthority;
24 import gboat2.web.model.Group;
25 import gboat2.web.model.Operation;
26 import gboat2.web.model.Resource;
27 import gboat2.web.model.Role;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.apache.commons.lang3.StringUtils;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37 import org.springframework.transaction.annotation.Transactional;
38
39
40
41
42
43
44
45
46 @Transactional
47 @Service
48 public class AuthorityBusinessImpl extends BaseService implements IAuthorityBusiness {
49
50 @Autowired
51 private IOperationBusiness operaBusi;
52
53 @Autowired
54 private IResourceBusiness resourceBusi;
55
56 @Autowired
57 private IDataLevelAuthorityBusiness dataLevelAuthorityBusi;
58
59 @Autowired
60 private IGroupBusiness groupBusi;
61
62 @Autowired
63 private IRoleBusiness roleBusi;
64
65 @Autowired
66 private ICacheService cacheSer;
67
68
69
70
71 @Override
72 public boolean setAuthority(String authForId, String resId) {
73
74
75
76
77
78
79 if (StringUtils.isEmpty(authForId) || StringUtils.isEmpty(resId)) {
80 return false;
81 }
82 Resource res = (Resource) super.get(Resource.class, resId);
83 this.addResourceAuthority(authForId, res);
84 this.addResourceAuthorityForParent(authForId, res);
85 this.addResourceAuthorityForChild(authForId, res);
86 return true;
87 }
88
89 @Override
90 public boolean deleteAuthority(String authForId, String resId, String systemId) {
91 if (StringUtils.isEmpty(authForId) || StringUtils.isEmpty(resId) || StringUtils.isEmpty(systemId)) {
92 return false;
93 }
94
95
96
97
98
99
100
101 this.cascadeSetAuthAtFirstDel(authForId, resId, systemId);
102
103 this.deleteResourceAuthority(authForId, resId);
104 this.deleteResourceAuthorityOfChild(authForId, resId);
105
106 return true;
107 }
108
109 @Override
110 public boolean setOperationsAuthority(String authForId, String resId, String operaId) {
111
112
113
114
115
116
117 if (StringUtils.isEmpty(authForId) || StringUtils.isEmpty(resId) || StringUtils.isEmpty(operaId)) {
118 return false;
119 }
120 Resource res = (Resource) get(Resource.class, resId);
121
122 this.saveAuthority(authForId, resId, res.getSystemId(), operaId, "1");
123
124 this.cascadeAuth(true, authForId, resId, operaId);
125 return true;
126 }
127
128 @Override
129 public boolean deleteOperationAuthority(String authForId, String resId, String operaId, String systemId) {
130 if (StringUtils.isEmpty(authForId) || StringUtils.isEmpty(resId)
131 || StringUtils.isEmpty(operaId) || StringUtils.isEmpty(systemId)) {
132 return false;
133 }
134
135
136
137 this.cascadeSetAuthAtFirstDel(authForId, resId, systemId);
138
139 this.deleteOpertionAuthorityBy(authForId, resId, operaId);
140
141 this.cascadeAuth(false, authForId, resId, operaId);
142 return true;
143 }
144
145 @Override
146 public boolean isAuthorityCustomize(String authForId,String systemId){
147 String hql = "select count(authId) from Authority where authForId=:authForId and systemId=:systemId";
148 Map<String, Object> params = new HashMap<String, Object>();
149 params.put("authForId", authForId);
150 params.put("systemId", systemId);
151 List<?> result = super.baseDAO.queryListByHql(hql, params);
152 Long sum = (Long) result.get(0);
153 return sum.longValue() > 0;
154 }
155
156 @Override
157 public List<Authority> findAuthoritiesByAuthForId(String authForId, String systemId) {
158 String[][] params = { { Authority.class.getName() }, { "_authForId", authForId }, { "_systemId", systemId } };
159 return (List<Authority>) super.baseDAO.queryList(params);
160 }
161
162 @Override
163 public List<AuthorityResourceVO> findBrothersAuthorityForResource(String roleId, String resId) {
164 List<AuthorityResourceVO> brothers = null;
165 Resource res = (Resource) super.get(Resource.class, resId);
166 Resource parent = (Resource) super.get(Resource.class, res.getParentId());
167 if (parent != null) {
168 String[][] params = { { AuthorityResourceVO.class.getName() }, { QuerySupport.PARAM_DISTINCT, "true" }, { "_authForId", roleId },
169 { "_parentId", parent.getResId() }, { "_resId_ne", resId }, { "_useStatus", "1" } };
170 brothers = (List<AuthorityResourceVO>) super.baseDAO.queryList(params);
171 }
172 return brothers;
173 }
174
175 @Override
176 public List<Operation> findOperationsForResource(String resId) {
177 List<Operation> allOpera = null;
178 allOpera = this.operaBusi.getOperationsByResourceId(resId);
179 return allOpera;
180 }
181
182 @Override
183 public List<AuthorityOperationVO> findOperationsForRole(String resId, String roleId) {
184 List<AuthorityOperationVO> result = null;
185
186 String[][] params = { { AuthorityOperationVO.class.getName() }, { QuerySupport.PARAM_DISTINCT, "true" }, { "_authForId", roleId },
187 { "_resId", resId }, { "_operationId_notnull", "true" }, { "_useStatus", "1" } };
188 result = (List<AuthorityOperationVO>) super.baseDAO.queryList(params);
189 return result;
190 }
191
192 @Override
193 public boolean deleteAuthorityByRoleId(String roleId) {
194 String hql = "delete from Authority where authForId=:roleId or authForId like '%." + roleId + "'";
195 Map<String, Object> params = new HashMap<String, Object>();
196 params.put("roleId", roleId);
197 super.baseDAO.deleteByQuery(hql, params);
198 return true;
199 }
200
201 @Override
202 public List<AuthorityResourceVO> getAuthorityByStatus(String status, String systemId) {
203 if (StringUtils.isEmpty(systemId)) {
204 return null;
205 }
206 Map<String, Object> params = new HashMap<String, Object>();
207 params.put(QuerySupport.PARAM_TABLENAME, AuthorityResourceVO.class);
208 params.put("_authForId", status);
209 params.put("_systemId", systemId);
210 params.put("_useStatus", "1");
211 List<AuthorityResourceVO> list = (List<AuthorityResourceVO>) super.query(params);
212 return list;
213 }
214
215 @Override
216 public String findDataLevelForRoleOfGroup(String groupId, String roleId, String resId) {
217 DataLevelAuthority auth = this.dataLevelAuthorityBusi.findDataLevelAuthority(groupId, roleId, resId);
218 if (auth != null) {
219 return auth.getDataLevel();
220 } else {
221 Group group = (Group) this.groupBusi.get(Group.class, groupId);
222 return group.getDefaultDataLevel();
223 }
224 }
225
226 @Override
227 public List<AuthorityOperationVO> findOperationsForRoleOfGroup(String groupId, String roleId, String resId) {
228 String[][] params = { { AuthorityOperationVO.class.getName() }, { QuerySupport.PARAM_DISTINCT, "true" },
229 { "_authForId", groupId + "." + roleId }, { "_resId", resId }, { "_useStatus", "1" }, { "_operationId_notnull", "true" } };
230 List<AuthorityOperationVO> operations = (List<AuthorityOperationVO>) baseDAO.queryList(params);
231 if (null == operations || 0 == operations.size()) {
232
233 Resource res = (Resource) super.get(Resource.class, resId);
234 List<Authority> auth = this.findAuthoritiesByAuthForId(groupId + "." + roleId, res.getSystemId());
235 if (auth == null || auth.size() == 0) {
236
237
238 operations = this.findOperationsForRole(resId, roleId);
239 }
240 }
241 return operations;
242 }
243
244 @Override
245 public List<AuthorityResourceVO> getTopLevelAuths(String authForId, String systemId) {
246 List<AuthorityResourceVO> auths = this.getAuthsByLevel(authForId, "0", systemId);
247 return auths;
248 }
249
250 @Override
251 public List<AuthorityOperationResourceVO> getAuthorityOperationResource(String authForId, String systemId) {
252 if (StringUtils.isEmpty(systemId)) {
253 return null;
254 }
255 Map<String, Object> params = new HashMap<String, Object>();
256 params.put(QuerySupport.PARAM_TABLENAME, AuthorityOperationResourceVO.class);
257 params.put("_authForId", authForId);
258 params.put("_systemId", systemId);
259 params.put("_useStatus", "1");
260 List<AuthorityOperationResourceVO> list = (List<AuthorityOperationResourceVO>) super.query(params);
261 return list;
262 }
263
264 @Override
265 public List<AuthorityDataLevelResourceVO> getAuthorityDataLevelResource(String groupId, String roleId, String systemId) {
266 if (StringUtils.isEmpty(systemId)) {
267 return null;
268 }
269 Map<String, Object> params = new HashMap<String, Object>();
270 params.put(QuerySupport.PARAM_TABLENAME, AuthorityDataLevelResourceVO.class);
271 params.put("_groupId", groupId);
272 params.put("_systemId", systemId);
273 params.put("_roleId", roleId);
274 List<AuthorityDataLevelResourceVO> list = (List<AuthorityDataLevelResourceVO>) super.query(params);
275 return list;
276 }
277
278 @Override
279 public AuthorityResourceVO findResourceTreeForRoleOfSystem(String roleId, String systemId) {
280 AuthorityResourceVO root = null;
281 root = this.findResourceTreeForRole(roleId, "0", systemId);
282 return root;
283 }
284
285
286 @Override
287 public List<AuthorityResourceVO> getAuthorityResourceByStatus(String authForId, String systemId) {
288 if (StringUtils.isEmpty(systemId)) {
289 return null;
290 }
291 Map<String, Object> params = new HashMap<String, Object>();
292 params.put(QuerySupport.PARAM_TABLENAME, AuthorityResourceVO.class);
293 params.put("_authForId", authForId);
294 params.put("_systemId", systemId);
295 List<AuthorityResourceVO> list = (List<AuthorityResourceVO>) super.query(params);
296 return list;
297 }
298
299 @Override
300 public boolean deleteAuthorityOfCustom(String authForId, String systemId) {
301 String hql = "delete from Authority where systemId='" + systemId + "' and authForId = :authForId";
302 Map<String, Object> params = new HashMap<String, Object>();
303 params.put("authForId", authForId);
304 super.baseDAO.deleteByQuery(hql, params);
305 return true;
306 }
307
308 @Override
309 public boolean setDataLevelAuthority(String groupId, String roleId, String resId, String dataLevel) {
310 return this.dataLevelAuthorityBusi.setDataLevelAuthority(groupId, roleId, resId, dataLevel);
311 }
312
313 @Override
314 public boolean deleteAuthorityByLikeGroupId(String groupId) {
315 String hql = "delete from Authority where authForId like '" + groupId + ".%'";
316 super.baseDAO.deleteByQuery(hql, null);
317 return true;
318 }
319
320 @SuppressWarnings("unchecked")
321 @Override
322 public List<AuthorityResourceVO> getAuthsByLevel(String authForId, String resId, String systemId) {
323 Map<String, Object> queryParams = new HashMap<String, Object>();
324 queryParams.put(QuerySupport.PARAM_TABLENAME, AuthorityResourceVO.class.getName());
325 queryParams.put(QuerySupport.PARAM_DISTINCT, true);
326 queryParams.put(QuerySupport.PARAM_ORDERBY, "dispOrder");
327 queryParams.put("_authForId", authForId);
328 queryParams.put("_parentId", resId);
329 queryParams.put("_useStatus", "1");
330
331 queryParams.put("_block", "RES_NAME not like '##%'");
332
333
334 if ("0".equals(resId) && systemId != null) {
335 queryParams.put("_systemId", systemId);
336 }
337 List<AuthorityResourceVO> auths = (List<AuthorityResourceVO>) baseDAO.queryList(queryParams);
338 return auths;
339 }
340
341 @Override
342 public AuthorityResourceVO findResourceTreeForRoleOfGroupOfSystem(String authForId, String systemId, boolean useCache) {
343 AuthorityResourceVO root = null;
344 if (useCache) {
345 root = (AuthorityResourceVO) this.cacheSer.get(Constants.MENU_TREE_CACHE_CATALOG, authForId + "-" + systemId);
346 if (null == root) {
347 root = this.findResourceTreeForRoleOfGroup(authForId, "0", systemId);
348
349 this.cacheSer.put(Constants.MENU_TREE_CACHE_CATALOG, authForId + "-" + systemId, root);
350 }
351 } else {
352 root = this.findResourceTreeForRoleOfGroup(authForId, "0", systemId);
353 }
354 return root;
355 }
356
357 @Override
358 public AuthorityResourceVO findResourceTreeForRoleOfGroupForRes(String authForId, String resId, boolean useCache) {
359 AuthorityResourceVO root = null;
360 Resource res = null;
361 if (StringUtils.isEmpty(resId)) {
362 return root;
363 }
364 if (resId.startsWith("##")) {
365
366 res = resourceBusi.getResourceByResname(resId);
367 } else {
368 res = (Resource) super.get(Resource.class, resId);
369 }
370 AuthorityResourceVO ret = new AuthorityResourceVO();
371 if (useCache) {
372 root = (AuthorityResourceVO) this.cacheSer.get(Constants.MENU_TREE_CACHE_CATALOG, authForId + "-" + res.getSystemId());
373 if (null == root) {
374 root = this.findResourceTreeForRoleOfGroup(authForId, "0", res.getSystemId());
375
376 this.cacheSer.put(Constants.MENU_TREE_CACHE_CATALOG, authForId + "-" + res.getSystemId(), root);
377 }
378 } else {
379 root = this.findResourceTreeForRoleOfGroup(authForId, "0", res.getSystemId());
380 }
381
382 root = this.getChildrenByResId(root, resId);
383
384
385 ret.setResName("root");
386 ret.setResId("0");
387 ret.setLeaf(false);
388 List<Resource> children = new ArrayList<Resource>();
389 children.add(root);
390 ret.setChildren(children);
391 return ret;
392 }
393
394 @Override
395 public List<String> findAllAuthForIdBySysIdAndRoleId(String systemId, String roleId) {
396 List<String> ret = null;
397 String sql = "select distinct auth_for_id from g2_t_authority where system_id='" + systemId + "' and auth_for_id like '%." + roleId + "'";
398 ret = (List<String>) super.baseDAO.queryListBySql(sql, null);
399 return ret;
400 }
401
402 @Override
403 public List<Resource> findAllAuthResOfParentByRoleCode(String parentCode, String roleCode) {
404 Role role = roleBusi.findRoleByRoleCode(roleCode);
405 Resource resource = resourceBusi.getResourceByCode(parentCode);
406 return findChildResourceListForRole(role.getRoleId(), resource.getResId(), resource.getSystemId());
407 }
408
409 @Override
410 public List<AuthorityOperationVO> findOperationsForUser(String groupId, String roleId, String userId, String resId) {
411 String[][] params = { { AuthorityOperationVO.class.getName() }, { QuerySupport.PARAM_DISTINCT, "true" },
412 { "_authForId", groupId + "." + roleId + "." + userId }, { "_resId", resId }, { "_useStatus", "1" },
413 { "_operationId_notnull", "true" } };
414 List<AuthorityOperationVO> operations = (List<AuthorityOperationVO>) baseDAO.queryList(params);
415 if (null == operations || 0 == operations.size()) {
416
417 Resource res = (Resource) super.get(Resource.class, resId);
418 List<Authority> auth = this.findAuthoritiesByAuthForId(groupId + "." + roleId + "." + userId, res.getSystemId());
419 if (auth == null || auth.size() == 0) {
420
421
422 operations = this.findOperationsForRoleOfGroup(groupId, roleId, resId);
423 }
424 }
425 return operations;
426 }
427
428 @Override
429 public List<AuthorityResourceVO> findResourceOfSystemByAuthForId(String authForId, String systemId) {
430 if (StringUtils.isBlank(authForId) || StringUtils.isBlank(systemId)) {
431 throw new NullPointerException("authForId or systemId is not null!");
432 } else {
433 String[] ids = authForId.split("\\.");
434 int len = ids.length;
435 switch(len){
436 case 1 : return this.findResourceForRole(authForId, systemId);
437 case 2 : return this.findResourceForGroupRole(authForId, systemId);
438 case 3 : return this.findResourceForGroupRoleUser(authForId, systemId);
439 default : throw new DefaultGboatNestedException("authForId=【" + authForId + "】格式错误,应该为:groupId.roleId.userId。");
440 }
441 }
442 }
443
444
445 @Override
446 public boolean setDataLevelAuthority(String groupId, String roleId, String userId, String resId, String dataLevel) {
447 boolean ret = this.dataLevelAuthorityBusi.setDataLevelAuthority(groupId, roleId, userId, resId, dataLevel);
448 return ret;
449 };
450
451
452
453
454
455
456
457
458
459 private List<AuthorityResourceVO> findResourceForRole(String roleId, String systemId) {
460 List<AuthorityResourceVO> ret = this.getResourceForRoleOfSystem(roleId, systemId);
461 return ret;
462 };
463
464
465
466
467
468
469
470
471 private List<AuthorityResourceVO> findResourceForGroupRole(String groupRoleId, String systemId) {
472
473 List<AuthorityResourceVO> result = null;
474
475 result = this.getResourceForRoleOfSystem(groupRoleId, systemId);
476
477
478
479
480
481
482
483 if (result == null || result.size() == 0) {
484
485 String[] splited = groupRoleId.split("\\.");
486 String roleId = splited[1];
487 result = this.getResourceForRoleOfSystem(roleId, systemId);
488 if (result != null && result.size() > 0) {
489
490 for (AuthorityResourceVO auRe : result) {
491 auRe.setInheritFlag("true");
492 auRe.setInheritFrom("role");
493 }
494 }
495 }
496 return result;
497 };
498
499
500
501
502
503
504
505
506 private List<AuthorityResourceVO> findResourceForGroupRoleUser(String groupRoleUserId, String systemId) {
507
508 List<AuthorityResourceVO> result = null;
509
510 result = this.getResourceForRoleOfSystem(groupRoleUserId, systemId);
511 if (null == result || result.size() == 0) {
512 String[] ids = groupRoleUserId.split("\\.");
513 String groupRoleId = ids[0] + "." + ids[1];
514 result = this.findResourceForGroupRole(groupRoleId, systemId);
515 if (result != null && result.size() > 0) {
516
517 for (AuthorityResourceVO auRe : result) {
518 String inheritFlag = auRe.getInheritFlag();
519 if (!"true".equals(inheritFlag)) {
520 auRe.setInheritFlag("true");
521 auRe.setInheritFrom("group");
522 }
523 }
524 }
525 }
526 return result;
527 }
528
529
530
531
532
533
534
535 private AuthorityResourceVO getChildrenByResId(AuthorityResourceVO root, String resId) {
536 AuthorityResourceVO ret = null;
537 if (resId.equals(root.getResId())) {
538 ret = root;
539 } else {
540 List<Resource> list = root.getChildren();
541 if (list != null && list.size() > 0) {
542 for (Resource r : list) {
543 ret = this.getChildrenByResId((AuthorityResourceVO) r, resId);
544 if (null != ret && resId.equals(ret.getResId())) {
545 break;
546 }
547 }
548 }
549 }
550 return ret;
551 }
552
553
554
555
556
557
558
559
560 private AuthorityResourceVO findResourceTreeForRoleOfGroup(String authForId, String resId, String systemId) {
561 AuthorityResourceVO root = null;
562
563 root = this.findResourceTreeForRole(authForId, resId, systemId);
564
565 if (null != root && 0 == root.getChildren().size() && authForId.indexOf(".") != -1) {
566
567 String[] splited = authForId.split("\\.");
568 String roleId = splited[1];
569 root = this.findResourceTreeForRole(roleId, resId, systemId);
570 }
571 return root;
572 }
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591 private AuthorityResourceVO findResourceTreeForRole(String authForId, String resId, String systemId) {
592 AuthorityResourceVO root = new AuthorityResourceVO();
593 root.setResName("root");
594 root.setResId("0");
595 root.setLeaf(false);
596
597 List<Resource> list = findChildResourceListForRole(authForId, resId, systemId);
598
599
600 if (!"0".equals(resId)) {
601 Resource curr = (Resource) this.resourceBusi.get(Resource.class, resId);
602 String resName = curr.getResName() + "";
603
604 if (!resName.startsWith("##")) {
605 if (curr != null) {
606
607 List<Resource> sub = new ArrayList<Resource>();
608
609 curr.setChildren(list);
610 curr.setLeaf(this.isLeaf(curr));
611
612
613 sub.add(curr);
614 root.setChildren(sub);
615 }
616 }
617 } else {
618 root.setChildren(list);
619 }
620 return root;
621 }
622
623
624
625
626
627
628
629
630 private boolean isLeaf(Resource res) {
631 return null == res.getChildren() || res.getChildren().size() == 0;
632 }
633
634
635
636
637
638
639
640
641 private List<Resource> getChildrenRecursively(Resource res, String roleId) {
642 List<AuthorityResourceVO> children = this.getResourceForRole(roleId, res.getResId());
643 List<Resource> list = new ArrayList<Resource>();
644 for (AuthorityResourceVO child : children) {
645 child.setChildren(this.getChildrenRecursively(child, roleId));
646 child.setLeaf(this.isLeaf(child));
647
648
649
650 list.add(child);
651 }
652 return list;
653 }
654
655
656
657
658
659
660
661
662 private List<AuthorityResourceVO> getResourceForRole(String roleId, String parentResId) {
663
664 String[][] params = { { AuthorityResourceVO.class.getName() }, { QuerySupport.PARAM_DISTINCT, "true" }, { "_authForId", roleId },
665 { "_parentId", parentResId }, { "_useStatus", "1" }, { "_block", "RES_NAME not like '##%'" },
666 { QuerySupport.PARAM_ORDERBY, "dispOrder" } };
667 return (List<AuthorityResourceVO>) baseDAO.queryList(params);
668 }
669
670
671
672
673
674
675
676
677 private List<Resource> findChildResourceListForRole(String authForId, String resId, String systemId) {
678
679 List<AuthorityResourceVO> tops = this.getAuthsByLevel(authForId, resId, systemId);
680
681
682 if ((!"0".equals(resId)) && (tops == null || tops.size() == 0) && (authForId.indexOf(".") != -1)) {
683
684 String[] splited = authForId.split("\\.");
685 authForId = splited[1];
686 tops = this.getAuthsByLevel(authForId, resId, systemId);
687 }
688
689
690 List<Resource> list = new ArrayList<Resource>();
691 for (AuthorityResourceVO authRes : tops) {
692 authRes.setChildren(this.getChildrenRecursively(authRes, authForId));
693 authRes.setLeaf(this.isLeaf(authRes));
694 list.add(authRes);
695 }
696 return list;
697 }
698
699
700
701
702
703
704
705
706 private List<AuthorityResourceVO> getResourceForRoleOfSystem(String authForId, String systemId) {
707 List<AuthorityResourceVO> result = null;
708 String[][] params = { { AuthorityResourceVO.class.getName() }, { QuerySupport.PARAM_DISTINCT, "true" }, { "_authForId", authForId },
709 { "_useStatus", "1" }, { QuerySupport.PARAM_ORDERBY, "dispOrder" }, { "_systemId", systemId } };
710 result = (List<AuthorityResourceVO>) super.baseDAO.queryList(params);
711 return result;
712 }
713
714
715
716
717
718
719
720
721
722
723 private boolean addResourceAuthority(String authForId, Resource res) {
724
725 if (!this.isAuthorityExist(authForId, res.getResId())) {
726
727 List<Operation> operations = null;
728 operations = this.operaBusi.getOperationsByResourceId(res.getResId());
729
730 for (Operation operation : operations) {
731 this.saveAuthority(authForId, res.getResId(), res.getSystemId(), operation.getOperationId(), "1");
732 }
733
734 this.saveAuthority(authForId, res.getResId(), res.getSystemId(), "", "1");
735
736
737 this.cascadeAuth(true, authForId, res.getResId(), null);
738 }
739 return true;
740 }
741
742
743
744
745
746
747
748
749
750
751 private boolean cascadeSetAuthAtFirstDel(String authForId, String resId, String systemId) {
752
753 String[] ids = authForId.split("\\.");
754 String gId = ids[0];
755 String rId = "";
756 String uId = "";
757 int len = ids.length;
758 switch(len){
759 case 1 : return true;
760 case 2 : rId = ids[1];uId = "";break;
761 case 3 : rId = ids[1];uId = ids[2];break;
762 default : throw new DefaultGboatNestedException("authForId=【" + authForId + "】格式错误,应该为:groupId.roleId.userId。");
763 }
764
765
766 List<Authority> auths = this.findAuthoritiesByAuthForId(authForId, systemId);
767
768 if (auths == null || auths.size() == 0) {
769 this.copyAuthorityFormRoleTo(rId, gId,uId, systemId);
770 }
771
772 return true;
773 }
774
775
776
777
778
779
780
781
782
783
784 private boolean copyAuthorityFormRoleTo(String roleId, String groupId, String userId, String systemId){
785 String authForId = "";
786 List<Authority> auths = null;
787 if(StringUtils.isNotEmpty(userId)){
788 authForId = groupId + "." + roleId;
789 auths = this.findAuthoritiesByAuthForId(authForId, systemId);
790 if (null == auths || auths.size() == 0) {
791 authForId = roleId;
792 auths = this.findAuthoritiesByAuthForId(authForId, systemId);
793 }
794 if (null == auths || auths.size() == 0) {
795 return true;
796 }
797 authForId = groupId + "." + roleId + "." + userId;
798
799 }else{
800 auths = this.findAuthoritiesByAuthForId(roleId, systemId);
801 if (null == auths || auths.size() == 0) {
802 return true;
803 }
804 authForId = groupId + "." + roleId;
805 }
806 for (Authority authority : auths) {
807 this.saveAuthority(authForId, authority.getResId(), systemId, authority.getOperaId(), "1");
808 }
809
810 return true;
811 }
812
813
814
815
816
817
818 private boolean addResourceAuthorityForParent(String authForId, Resource res) {
819
820 Resource parent = (Resource) this.resourceBusi.get(Resource.class, res.getParentId());
821 while(parent != null){
822 if (!this.isAuthorityExist(authForId, parent.getResId())) {
823 this.saveAuthority(authForId, parent.getResId(), res.getSystemId(), "", "1");
824 }
825 parent = (Resource) this.resourceBusi.get(Resource.class, parent.getParentId());
826 }
827 return true;
828 }
829
830
831
832
833
834
835 private boolean addResourceAuthorityForChild(String authForId, Resource res) {
836 List<Resource> children = this.resourceBusi.getChildrenByParentId(res.getResId());
837 for (Resource child : children) {
838 addResourceAuthority(authForId, child);
839 addResourceAuthorityForChild(authForId, child);
840 }
841 return true;
842 }
843
844
845
846
847
848
849
850 private boolean deleteResourceAuthorityOfChild(String authForId, String resId) {
851 List<Resource> children = this.resourceBusi.getChildrenByParentId(resId);
852 for (Resource child : children) {
853 this.deleteResourceAuthority(authForId, child.getResId());
854
855 deleteResourceAuthorityOfChild(authForId, child.getResId());
856 }
857 return true;
858 }
859
860
861
862
863
864
865
866 private boolean deleteResourceAuthority(String authForId, String resId) {
867 String hql = "delete from Authority where resId=:resId and authForId=:authForId";
868 Map<String, Object> params = new HashMap<String, Object>();
869 params.put("resId", resId);
870 params.put("authForId", authForId);
871 super.baseDAO.deleteByQuery(hql, params);
872
873 this.cascadeAuth(false, authForId, resId, null);
874 return true;
875 }
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891 private boolean cascadeAuth(boolean authStatus, String authForId, String resId,String operaId) {
892 String[] ids = authForId.split("\\.");
893 int len = ids.length;
894 String authForIdLike = "";
895 switch(len){
896 case 3 : return true;
897 case 2 : authForIdLike = authForId + ".%";break;
898 case 1 : authForIdLike = "%." + authForId + "%";break;
899 default : throw new DefaultGboatNestedException("authForId=【" + authForId + "】格式错误,应该为:groupId.roleId.userId。");
900 }
901 this.setAuthorityStatus(authStatus, authForIdLike, resId, operaId);
902 return true;
903 }
904
905
906
907
908
909
910
911
912
913 private void setAuthorityStatus(boolean authStatus,String authForIdLike,String resId,String operaId){
914 String useStatusFrom = "1";
915 String useStatusTo = "0";
916 if(authStatus){
917 useStatusFrom = "0";
918 useStatusTo = "1";
919 }
920
921 String hql = "update Authority set useStatus=:useStatusTo where authForId like:authForIdLike and resId=:resId and useStatus=:useStatusFrom";
922 Map<String, Object> params = new HashMap<String, Object>();
923 params.put("authForIdLike", authForIdLike);
924 params.put("resId", resId);
925 params.put("useStatusFrom", useStatusFrom);
926 params.put("useStatusTo", useStatusTo);
927
928 if(StringUtils.isNotEmpty(operaId)){
929 hql += " and operaId=:operaId";
930 params.put("operaId", operaId);
931 }
932 this.baseDAO.updateByQuery(hql, params);
933 }
934
935
936
937
938
939
940
941 private boolean isAuthorityExist(String authForId, String resId) {
942 String hql = "select count(*) from Authority where authForId=:authForId and resId=:resId";
943 Map<String, Object> params = new HashMap<String, Object>();
944 params.put("authForId", authForId);
945 params.put("resId", resId);
946 params.put("useStatus", "1");
947 List<?> result = super.baseDAO.queryListByHql(hql, params);
948 Long sum = (Long) result.get(0);
949 return sum.longValue() > 0;
950 }
951
952
953
954
955
956
957
958
959
960
961 private boolean saveAuthority(String authForId,String resId,String systemId,String operaId,String useStatus){
962 if(StringUtils.isNotEmpty(authForId) && StringUtils.isNotEmpty(resId) &&
963 StringUtils.isNotEmpty(systemId) && StringUtils.isNotEmpty(useStatus) ){
964 Authority auth = new Authority();
965 auth.setOperaId(operaId);
966 auth.setAuthForId(authForId);
967 auth.setResId(resId);
968 auth.setUseStatus(useStatus);
969 auth.setSystemId(systemId);
970 this.save(auth);
971 return true;
972 }
973 return false;
974 }
975
976 private boolean deleteOpertionAuthorityBy(String authForId,String resId,String operaId){
977 String hql = "delete from Authority where operaId=:operaId and authForId=:authForId and resId=:resId";
978 Map<String, Object> params = new HashMap<String, Object>();
979 params.put("authForId", authForId);
980 params.put("resId", resId);
981 params.put("operaId", operaId);
982 baseDAO.deleteByQuery(hql, params);
983 return true;
984 }
985
986
987 }