笔记2

JeeSite数据权限

Java代码  

  1. User user = UserUtils.getUser();  
  2. // 使用标准查询  
  3. DetachedCriteria dc = articleDao.createDetachedCriteria();  
  4. dc.createAlias("office", "office").createAlias("user", "user");  
  5. dc.add(dataScopeFilter(user, "office", "user"));  
  6. List<Entity> list = articleDao.find(page, dc);;  
  7. // 使用HQL查询  
  8. String hql = "select e from Entity e join e.office o join e.user u where 1=1 ";  
  9. hql += dataScopeFilterString(UserUtils.getUser(), "o", "u");  

10. List<Entity> list2 = articleDao.find(page, hql);  

 

Java代码  

  1. /** 
  2.  * 数据范围过滤 
  3.  * @param dc Hibernate标准查询对象 
  4.  * @param user 当前用户对象,通过“UserUtils.getUser()”获取 
  5.  * @param officeAlias 机构表别名,例如:dc.createAlias("office", "office"); 
  6.  * @param userAlias 用户表别名,传递空,忽略此参数 
  7.  * @return 标准连接条件对象 
  8.  */  
  9. protected static Junction dataScopeFilter(User user, String officeAlias, String userAlias) {  
  10. 10.   
  11. 11.     // 进行权限过滤,多个角色权限范围之间为或者关系。  
  12. 12.     List<String> dataScope = Lists.newArrayList();  
  13. 13.     Junction junction = Restrictions.disjunction();  
  14. 14.       
  15. 15.     // 超级管理员,跳过权限过滤  
  16. 16.     if (!user.isAdmin()){  
  17. 17.         for (Role r : user.getRoleList()){  
  18. 18.             if (!dataScope.contains(r.getDataScope()) && StringUtils.isNotBlank(officeAlias)){  
  19. 19.                 boolean isDataScopeAll = false;  
  20. 20.                 if (Role.DATA_SCOPE_ALL.equals(r.getDataScope())){  
  21. 21.                     isDataScopeAll = true;  
  22. 22.                 }  
  23. 23.                 else if (Role.DATA_SCOPE_COMPANY_AND_CHILD.equals(r.getDataScope())){  
  24. 24.                     junction.add(Restrictions.eq(officeAlias+".id", user.getCompany().getId()));  
  25. 25.                     junction.add(Restrictions.like(officeAlias+".parentIds", user.getCompany().getParentIds()+user.getCompany().getId()+",%"));  
  26. 26.                 }  
  27. 27.                 else if (Role.DATA_SCOPE_COMPANY.equals(r.getDataScope())){  
  28. 28.                     junction.add(Restrictions.eq(officeAlias+".id", user.getCompany().getId()));  
  29. 29.                     junction.add(Restrictions.and(Restrictions.eq(officeAlias+".parent.id", user.getCompany().getId()),  
  30. 30.                             Restrictions.eq(officeAlias+".type", "2"))); // 包括本公司下的部门  
  31. 31.                 }  
  32. 32.                 else if (Role.DATA_SCOPE_OFFICE_AND_CHILD.equals(r.getDataScope())){  
  33. 33.                     junction.add(Restrictions.eq(officeAlias+".id", user.getOffice().getId()));  
  34. 34.                     junction.add(Restrictions.like(officeAlias+".parentIds", user.getOffice().getParentIds()+user.getOffice().getId()+",%"));  
  35. 35.                 }  
  36. 36.                 else if (Role.DATA_SCOPE_OFFICE.equals(r.getDataScope())){  
  37. 37.                     junction.add(Restrictions.eq(officeAlias+".id", user.getOffice().getId()));  
  38. 38.                 }  
  39. 39.                 else if (Role.DATA_SCOPE_CUSTOM.equals(r.getDataScope())){  
  40. 40.                     junction.add(Restrictions.in(officeAlias+".id", r.getOfficeIdList()));  
  41. 41.                 }  
  42. 42.                 //else if (Role.DATA_SCOPE_SELF.equals(r.getDataScope())){  
  43. 43.                 if (!isDataScopeAll){  
  44. 44.                     if (StringUtils.isNotBlank(userAlias)){  
  45. 45.                         junction.add(Restrictions.eq(userAlias+".id", user.getId()));  
  46. 46.                     }else {  
  47. 47.                         junction.add(Restrictions.isNull(officeAlias+".id"));  
  48. 48.                     }  
  49. 49.                 }else{  
  50. 50.                     // 如果包含全部权限,则去掉之前添加的所有条件,并跳出循环。  
  51. 51.                     junction = Restrictions.disjunction();  
  52. 52.                     break;  
  53. 53.                 }  
  54. 54.                 dataScope.add(r.getDataScope());  
  55. 55.             }  
  56. 56.         }  
  57. 57.     }  
  58. 58.     return junction;  

59. }  

  1. 60.   

61. /** 

  1. 62.  * 数据范围过滤 
  2. 63.  * @param user 当前用户对象,通过“UserUtils.getUser()”获取 
  3. 64.  * @param officeAlias 机构表别名,例如:dc.createAlias("office", "office"); 
  4. 65.  * @param userAlias 用户表别名,传递空,忽略此参数 
  5. 66.  * @return ql查询字符串 
  6. 67.  */  

68. protected static String dataScopeFilterString(User user, String officeAlias, String userAlias) {  

  1. 69.     Junction junction = dataScopeFilter(user, officeAlias, userAlias);  
  2. 70.     Iterator<Criterion> it = junction.conditions().iterator();  
  3. 71.     StringBuilder ql = new StringBuilder();  
  4. 72.     ql.append(" and (");  
  5. 73.     if (it.hasNext()){  
  6. 74.         ql.append(it.next());  
  7. 75.     }  
  8. 76.     String[] strField = {".parentIds like ", ".type="}; // 需要给字段增加“单引号”的字段。  
  9. 77.     while (it.hasNext()) {  
  10. 78.         ql.append(" or (");  
  11. 79.         String s = it.next().toString();  
  12. 80.         for(String field : strField){  
  13. 81.             s = s.replaceAll(field + "(\\w.*)", field + "'$1'");  
  14. 82.         }  
  15. 83.         ql.append(s).append(")");  
  16. 84.     }  
  17. 85.     ql.append(")");  
  18. 86.     return ql.toString();  

87. }  

posted @ 2017-05-21 21:21  锦绣河山锦  阅读(202)  评论(0)    收藏  举报