View Javadoc
1   /**
2    * Copyright (c) 2009-2010 by Glodon
3    * All rights reserved.
4    */
5   package gboat2.base.bridge.model;
6   
7   import java.io.Serializable;
8   import java.util.HashMap;
9   import java.util.HashSet;
10  import java.util.LinkedList;
11  import java.util.List;
12  import java.util.Map;
13  import java.util.Set;
14  
15  /**
16   * 该类主要是对权限进行管理操作
17   * @author lysming
18   * @since 1.0
19   */
20  public class Priority implements Serializable {
21  
22      private static final long serialVersionUID = 1620492035834167297L;
23  
24      /** 表示具action所有操作权限的标记 */
25      private static final String ALL_OPERATIONS = "#all";
26  
27      /** 具有权限的Actions,只包含权限信息,格式为 "resourceActionClass.operationCode" */
28      private List<String> operationPriority = new LinkedList<String>();
29  
30      /** 格式为 actionClass => dataLevelCode */
31      private Map<String, String> dataLevelPriority = new HashMap<String, String>();
32  
33      /** 默认数据级别 */
34      private String defaultDataLevel;
35  
36      /**
37       * 构造方法
38       * @param defaultDataLevel 数据级别
39       */
40      public Priority(String defaultDataLevel) {
41          this.defaultDataLevel = defaultDataLevel;
42      }
43  
44      /**
45       * add an operation priority
46       * 
47       * @param actionClass Action 类的名称
48       * @param operationCode 操作的 code
49       */
50      public void addOperationPriority(String actionClass, String operationCode) {
51      	String allOperations = actionClass + "." +ALL_OPERATIONS;
52      	if(operationPriority.contains(allOperations)){
53      		operationPriority.remove(allOperations);
54      	}
55          operationPriority.add(actionClass + "." + operationCode);
56      }
57  
58      /**
59       * add an operation priority, means that add all operation priorities of
60       * actionClass
61       * 
62       * @param actionClass Action 类的名称
63       */
64      public void addAllOperationPriorityOf(String actionClass) {
65          operationPriority.add(actionClass + "." + ALL_OPERATIONS);
66      }
67      
68      /**
69       * delete actionClass's all operation priorities
70       * @param actionClass
71       */
72      public void removeAllOperationProrityOf(String actionClass) {
73      	operationPriority.add(actionClass + "." + ALL_OPERATIONS);
74      }
75  
76      /**
77       * 添加数据权限
78       * 
79       * @param actionClass 相应的action class
80       * @param dataLevelCode 数据权限代码
81       */
82      public void addDataPriority(String actionClass, String dataLevelCode) {
83          dataLevelPriority.put(actionClass, dataLevelCode);
84      }
85  
86      /**
87       * 判断是否具有 Operation 的权限
88       * 
89       * @param res 访问的操作
90       * @return boolean
91       */
92      public boolean havePriority(Resource res) {
93          if (operationPriority.contains(res.getActionClass() + "." + res.getOperationCode())) {
94              return true;
95          } else {
96              return operationPriority.contains(res.getActionClass() + "." + ALL_OPERATIONS);
97          }
98      }
99  
100     /**
101      * @param resourceCode the actionClass name
102      * @return
103      */
104     public String getDataLevelCode(String resourceCode) {
105         if (dataLevelPriority.containsKey(resourceCode)) {
106             return dataLevelPriority.get(resourceCode);
107         }
108         return defaultDataLevel;
109     }
110 
111     public String getDefaultDataLevel() {
112         return defaultDataLevel;
113     }
114 
115     /**
116      * 获取当前用户所拥有权限的所有action类集合
117      * 
118      * @return action类全名组成的集合
119      */
120     public Set<String> getPriorityActionNames() {
121         Set<String> actions = new HashSet<String>();
122         for (String priority : operationPriority) {
123             actions.add(priority.substring(0, priority.lastIndexOf('.')));
124         }
125         return actions;
126     }
127     
128     public List<String> getOperationPriorities(){
129     	return this.operationPriority;
130     }
131     
132     public Map<String,String> getDataLevelPriorities(){
133     	return this.dataLevelPriority;
134     }
135     
136     public void appendPriority(Priority priority){
137     	operationPriority.addAll(priority.getOperationPriorities());
138     	//TODO 检查权限级别冲突的问题
139     	dataLevelPriority.putAll(priority.getDataLevelPriorities());
140     }
141 }