%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.om.security.peer.PermissionPeer |
|
|
1 | package org.apache.turbine.om.security.peer; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.ArrayList; |
|
20 | import java.util.Enumeration; |
|
21 | import java.util.List; |
|
22 | import java.util.Map; |
|
23 | import java.util.Vector; |
|
24 | import org.apache.torque.TorqueException; |
|
25 | import org.apache.torque.om.BaseObject; |
|
26 | import org.apache.torque.om.NumberKey; |
|
27 | import org.apache.torque.util.BasePeer; |
|
28 | import org.apache.torque.util.Criteria; |
|
29 | import org.apache.turbine.om.security.Permission; |
|
30 | import org.apache.turbine.om.security.Role; |
|
31 | import org.apache.turbine.om.security.SecurityObject; |
|
32 | import org.apache.turbine.om.security.TurbineRole; |
|
33 | import org.apache.turbine.services.security.TurbineSecurity; |
|
34 | import org.apache.turbine.util.ObjectUtils; |
|
35 | import org.apache.turbine.util.db.map.TurbineMapBuilder; |
|
36 | import org.apache.turbine.util.security.DataBackendException; |
|
37 | import org.apache.turbine.util.security.PermissionSet; |
|
38 | import com.workingdogs.village.Record; |
|
39 | ||
40 | /** |
|
41 | * This class handles all the database access for the PERMISSION |
|
42 | * table. This table contains all the permissions that are used in |
|
43 | * the system. |
|
44 | * |
|
45 | * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> |
|
46 | * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> |
|
47 | * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> |
|
48 | * @version $Id: PermissionPeer.java 264148 2005-08-29 14:21:04Z henning $ |
|
49 | */ |
|
50 | 0 | public class PermissionPeer extends BasePeer |
51 | { |
|
52 | /** Serial Version UID */ |
|
53 | private static final long serialVersionUID = 2762005892291909743L; |
|
54 | ||
55 | /** The map builder for this Peer. */ |
|
56 | 0 | private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) |
57 | 0 | getMapBuilder(TurbineMapBuilder.class.getName()); |
58 | ||
59 | /** The table name for this peer. */ |
|
60 | 0 | private static final String TABLE_NAME = MAP_BUILDER.getTablePermission(); |
61 | ||
62 | /** The column name for the permission id field. */ |
|
63 | 0 | public static final String PERMISSION_ID |
64 | = MAP_BUILDER.getPermission_PermissionId(); |
|
65 | ||
66 | /** The column name for the name field. */ |
|
67 | 0 | public static final String NAME = MAP_BUILDER.getPermission_Name(); |
68 | ||
69 | /** The column name for the ObjectData field */ |
|
70 | 0 | public static final String OBJECTDATA |
71 | = MAP_BUILDER.getPermission_ObjectData(); |
|
72 | ||
73 | /** |
|
74 | * Retrieves/assembles a PermissionSet |
|
75 | * |
|
76 | * @param criteria The criteria to use. |
|
77 | * @return A PermissionSet. |
|
78 | * @exception Exception a generic exception. |
|
79 | */ |
|
80 | public static PermissionSet retrieveSet(Criteria criteria) |
|
81 | throws Exception |
|
82 | { |
|
83 | 0 | List results = PermissionPeer.doSelect(criteria); |
84 | 0 | PermissionSet ps = new PermissionSet(); |
85 | 0 | for (int i = 0; i < results.size(); i++) |
86 | { |
|
87 | 0 | ps.add((Permission) results.get(i)); |
88 | } |
|
89 | 0 | return ps; |
90 | } |
|
91 | ||
92 | /** |
|
93 | * Retrieves a set of Permissions associated with a particular Role. |
|
94 | * |
|
95 | * @param role The role to query permissions of. |
|
96 | * @return A set of permissions associated with the Role. |
|
97 | * @exception Exception a generic exception. |
|
98 | */ |
|
99 | public static PermissionSet retrieveSet(Role role) |
|
100 | throws Exception |
|
101 | { |
|
102 | 0 | Criteria criteria = new Criteria(); |
103 | 0 | criteria.add(RolePermissionPeer.ROLE_ID, |
104 | ((TurbineRole) role).getPrimaryKey()); |
|
105 | 0 | criteria.addJoin(RolePermissionPeer.PERMISSION_ID, |
106 | PermissionPeer.PERMISSION_ID); |
|
107 | 0 | return retrieveSet(criteria); |
108 | } |
|
109 | ||
110 | /** |
|
111 | * Issues a select based on a criteria. |
|
112 | * |
|
113 | * @param criteria Object containing data that is used to create |
|
114 | * the SELECT statement. |
|
115 | * @return Vector containing Permission objects. |
|
116 | * @exception TorqueException a generic exception. |
|
117 | */ |
|
118 | public static List doSelect(Criteria criteria) |
|
119 | throws TorqueException |
|
120 | { |
|
121 | try |
|
122 | { |
|
123 | 0 | criteria.addSelectColumn(PERMISSION_ID) |
124 | .addSelectColumn(NAME) |
|
125 | .addSelectColumn(OBJECTDATA); |
|
126 | ||
127 | 0 | if (criteria.getOrderByColumns() == null |
128 | || criteria.getOrderByColumns().size() == 0) |
|
129 | { |
|
130 | 0 | criteria.addAscendingOrderByColumn(NAME); |
131 | } |
|
132 | ||
133 | // Place any checks here to intercept criteria which require |
|
134 | // custom SQL. For example: |
|
135 | // if ( criteria.containsKey("SomeTable.SomeColumn") ) |
|
136 | // { |
|
137 | // String whereSql = "SomeTable.SomeColumn IN (Select ..."; |
|
138 | // criteria.add("SomeTable.SomeColumn", |
|
139 | // whereSQL, criteria.CUSTOM); |
|
140 | // } |
|
141 | ||
142 | // BasePeer returns a Vector of Value (Village) arrays. The |
|
143 | // array order follows the order columns were placed in the |
|
144 | // Select clause. |
|
145 | 0 | List rows = BasePeer.doSelect(criteria); |
146 | 0 | List results = new ArrayList(); |
147 | ||
148 | // Populate the object(s). |
|
149 | 0 | for (int i = 0; i < rows.size(); i++) |
150 | { |
|
151 | 0 | Permission obj = TurbineSecurity.getPermissionInstance(null); |
152 | 0 | Record row = (Record) rows.get(i); |
153 | 0 | ((SecurityObject) obj).setPrimaryKey( |
154 | new NumberKey(row.getValue(1).asInt())); |
|
155 | 0 | ((SecurityObject) obj).setName(row.getValue(2).asString()); |
156 | 0 | byte[] objectData = row.getValue(3).asBytes(); |
157 | 0 | Map temp = (Map) ObjectUtils.deserialize(objectData); |
158 | 0 | if (temp != null) |
159 | { |
|
160 | 0 | ((SecurityObject) obj).setAttributes(temp); |
161 | } |
|
162 | 0 | results.add(obj); |
163 | } |
|
164 | ||
165 | 0 | return results; |
166 | } |
|
167 | 0 | catch (Exception ex) |
168 | { |
|
169 | 0 | throw new TorqueException(ex); |
170 | } |
|
171 | } |
|
172 | ||
173 | /** |
|
174 | * Builds a criteria object based upon an Permission object |
|
175 | * |
|
176 | * @param permission object to build the criteria |
|
177 | * @return the Criteria |
|
178 | */ |
|
179 | public static Criteria buildCriteria(Permission permission) |
|
180 | { |
|
181 | 0 | Criteria criteria = new Criteria(); |
182 | 0 | if (!((BaseObject) permission).isNew()) |
183 | { |
|
184 | 0 | criteria.add(PERMISSION_ID, |
185 | ((BaseObject) permission).getPrimaryKey()); |
|
186 | } |
|
187 | 0 | criteria.add(NAME, ((SecurityObject) permission).getName()); |
188 | ||
189 | /* |
|
190 | * This is causing the the removal and updating of |
|
191 | * a permission to crap out. This addition to the |
|
192 | * criteria produces something like: |
|
193 | * |
|
194 | * where OBJECTDATA = {} |
|
195 | * |
|
196 | * Is the NAME even necessary. Wouldn't |
|
197 | * criteria.add(PERMISSION_ID, N) be enough to |
|
198 | * generate a where clause that would remove the |
|
199 | * permission? |
|
200 | * |
|
201 | * criteria.add(OBJECTDATA, permission.getAttributes()); |
|
202 | */ |
|
203 | 0 | return criteria; |
204 | } |
|
205 | ||
206 | /** |
|
207 | * Issues an update based on a criteria. |
|
208 | * |
|
209 | * @param criteria Object containing data that is used to create |
|
210 | * the UPDATE statement. |
|
211 | * @exception TorqueException a generic exception. |
|
212 | */ |
|
213 | public static void doUpdate(Criteria criteria) |
|
214 | throws TorqueException |
|
215 | { |
|
216 | 0 | Criteria selectCriteria = new Criteria(2); |
217 | 0 | selectCriteria.put(PERMISSION_ID, criteria.remove(PERMISSION_ID)); |
218 | 0 | BasePeer.doUpdate(selectCriteria, criteria); |
219 | 0 | } |
220 | ||
221 | /** |
|
222 | * Checks if a Permission is defined in the system. The name |
|
223 | * is used as query criteria. |
|
224 | * |
|
225 | * @param permission The Permission to be checked. |
|
226 | * @return <code>true</code> if given Permission exists in the system. |
|
227 | * @throws DataBackendException when more than one Permission with |
|
228 | * the same name exists. |
|
229 | * @throws Exception a generic exception. |
|
230 | */ |
|
231 | public static boolean checkExists(Permission permission) |
|
232 | throws DataBackendException, Exception |
|
233 | { |
|
234 | 0 | Criteria criteria = new Criteria(); |
235 | 0 | criteria.addSelectColumn(PERMISSION_ID); |
236 | 0 | criteria.add(NAME, ((SecurityObject) permission).getName()); |
237 | 0 | List results = BasePeer.doSelect(criteria); |
238 | 0 | if (results.size() > 1) |
239 | { |
|
240 | 0 | throw new DataBackendException("Multiple permissions named '" |
241 | + ((SecurityObject) permission).getName() + "' exist!"); |
|
242 | } |
|
243 | 0 | return (results.size() == 1); |
244 | } |
|
245 | ||
246 | /** |
|
247 | * Get the name of this table. |
|
248 | * |
|
249 | * @return A String with the name of the table. |
|
250 | */ |
|
251 | public static String getTableName() |
|
252 | { |
|
253 | 0 | return TABLE_NAME; |
254 | } |
|
255 | ||
256 | /** |
|
257 | * Returns the full name of a column. |
|
258 | * |
|
259 | * @param name name of a column |
|
260 | * @return A String with the full name of the column. |
|
261 | */ |
|
262 | public static String getColumnName(String name) |
|
263 | { |
|
264 | 0 | StringBuffer sb = new StringBuffer(); |
265 | 0 | sb.append(TABLE_NAME); |
266 | 0 | sb.append("."); |
267 | 0 | sb.append(name); |
268 | 0 | return sb.toString(); |
269 | } |
|
270 | ||
271 | /** |
|
272 | * Pass in two Vector's of Permission Objects. It will return a |
|
273 | * new Vector with the difference of the two Vectors: C = (A - B). |
|
274 | * |
|
275 | * @param some Vector B in C = (A - B). |
|
276 | * @param all Vector A in C = (A - B). |
|
277 | * @return Vector C in C = (A - B). |
|
278 | */ |
|
279 | public static final Vector getDifference(Vector some, Vector all) |
|
280 | { |
|
281 | 0 | Vector clone = (Vector) all.clone(); |
282 | 0 | for (Enumeration e = some.elements(); e.hasMoreElements();) |
283 | { |
|
284 | 0 | Permission tmp = (Permission) e.nextElement(); |
285 | 0 | for (Enumeration f = clone.elements(); f.hasMoreElements();) |
286 | { |
|
287 | 0 | Permission tmp2 = (Permission) f.nextElement(); |
288 | 0 | if (((BaseObject) tmp).getPrimaryKey() |
289 | == ((BaseObject) tmp2).getPrimaryKey()) |
|
290 | { |
|
291 | 0 | clone.removeElement(tmp2); |
292 | 0 | break; |
293 | } |
|
294 | } |
|
295 | } |
|
296 | 0 | return clone; |
297 | } |
|
298 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |