%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.om.security.peer.PermissionPeer |
|
|
1 | package org.apache.turbine.om.security.peer; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2004 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,v 1.11.2.2 2004/05/20 03:05:17 seade Exp $ |
|
49 | */ |
|
50 | 0 | public class PermissionPeer extends BasePeer |
51 | { |
|
52 | /** The map builder for this Peer. */ |
|
53 | 0 | private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) |
54 | 0 | getMapBuilder(TurbineMapBuilder.class.getName()); |
55 | ||
56 | /** The table name for this peer. */ |
|
57 | 0 | private static final String TABLE_NAME = MAP_BUILDER.getTablePermission(); |
58 | ||
59 | /** The column name for the permission id field. */ |
|
60 | 0 | public static final String PERMISSION_ID |
61 | = MAP_BUILDER.getPermission_PermissionId(); |
|
62 | ||
63 | /** The column name for the name field. */ |
|
64 | 0 | public static final String NAME = MAP_BUILDER.getPermission_Name(); |
65 | ||
66 | /** The column name for the ObjectData field */ |
|
67 | 0 | public static final String OBJECTDATA |
68 | = MAP_BUILDER.getPermission_ObjectData(); |
|
69 | ||
70 | /** |
|
71 | * Retrieves/assembles a PermissionSet |
|
72 | * |
|
73 | * @param criteria The criteria to use. |
|
74 | * @return A PermissionSet. |
|
75 | * @exception Exception a generic exception. |
|
76 | */ |
|
77 | public static PermissionSet retrieveSet(Criteria criteria) |
|
78 | throws Exception |
|
79 | { |
|
80 | 0 | List results = PermissionPeer.doSelect(criteria); |
81 | 0 | PermissionSet ps = new PermissionSet(); |
82 | 0 | for (int i = 0; i < results.size(); i++) |
83 | { |
|
84 | 0 | ps.add((Permission) results.get(i)); |
85 | } |
|
86 | 0 | return ps; |
87 | } |
|
88 | ||
89 | /** |
|
90 | * Retrieves a set of Permissions associated with a particular Role. |
|
91 | * |
|
92 | * @param role The role to query permissions of. |
|
93 | * @return A set of permissions associated with the Role. |
|
94 | * @exception Exception a generic exception. |
|
95 | */ |
|
96 | public static PermissionSet retrieveSet(Role role) |
|
97 | throws Exception |
|
98 | { |
|
99 | 0 | Criteria criteria = new Criteria(); |
100 | 0 | criteria.add(RolePermissionPeer.ROLE_ID, |
101 | ((TurbineRole) role).getPrimaryKey()); |
|
102 | 0 | criteria.addJoin(RolePermissionPeer.PERMISSION_ID, |
103 | PermissionPeer.PERMISSION_ID); |
|
104 | 0 | return retrieveSet(criteria); |
105 | } |
|
106 | ||
107 | /** |
|
108 | * Issues a select based on a criteria. |
|
109 | * |
|
110 | * @param criteria Object containing data that is used to create |
|
111 | * the SELECT statement. |
|
112 | * @return Vector containing Permission objects. |
|
113 | * @exception TorqueException a generic exception. |
|
114 | */ |
|
115 | public static List doSelect(Criteria criteria) |
|
116 | throws TorqueException |
|
117 | { |
|
118 | try |
|
119 | { |
|
120 | 0 | criteria.addSelectColumn(PERMISSION_ID) |
121 | .addSelectColumn(NAME) |
|
122 | .addSelectColumn(OBJECTDATA); |
|
123 | ||
124 | 0 | if (criteria.getOrderByColumns() == null |
125 | || criteria.getOrderByColumns().size() == 0) |
|
126 | { |
|
127 | 0 | criteria.addAscendingOrderByColumn(NAME); |
128 | } |
|
129 | ||
130 | // Place any checks here to intercept criteria which require |
|
131 | // custom SQL. For example: |
|
132 | // if ( criteria.containsKey("SomeTable.SomeColumn") ) |
|
133 | // { |
|
134 | // String whereSql = "SomeTable.SomeColumn IN (Select ..."; |
|
135 | // criteria.add("SomeTable.SomeColumn", |
|
136 | // whereSQL, criteria.CUSTOM); |
|
137 | // } |
|
138 | ||
139 | // BasePeer returns a Vector of Value (Village) arrays. The |
|
140 | // array order follows the order columns were placed in the |
|
141 | // Select clause. |
|
142 | 0 | List rows = BasePeer.doSelect(criteria); |
143 | 0 | List results = new ArrayList(); |
144 | ||
145 | // Populate the object(s). |
|
146 | 0 | for (int i = 0; i < rows.size(); i++) |
147 | { |
|
148 | 0 | Permission obj = TurbineSecurity.getPermissionInstance(null); |
149 | 0 | Record row = (Record) rows.get(i); |
150 | 0 | ((SecurityObject) obj).setPrimaryKey( |
151 | new NumberKey(row.getValue(1).asInt())); |
|
152 | 0 | ((SecurityObject) obj).setName(row.getValue(2).asString()); |
153 | 0 | byte[] objectData = row.getValue(3).asBytes(); |
154 | 0 | Map temp = (Map) ObjectUtils.deserialize(objectData); |
155 | 0 | if (temp != null) |
156 | { |
|
157 | 0 | ((SecurityObject) obj).setAttributes(temp); |
158 | } |
|
159 | 0 | results.add(obj); |
160 | } |
|
161 | ||
162 | 0 | return results; |
163 | } |
|
164 | 0 | catch (Exception ex) |
165 | { |
|
166 | 0 | throw new TorqueException(ex); |
167 | } |
|
168 | } |
|
169 | ||
170 | /** |
|
171 | * Builds a criteria object based upon an Permission object |
|
172 | * |
|
173 | * @param permission object to build the criteria |
|
174 | * @return the Criteria |
|
175 | */ |
|
176 | public static Criteria buildCriteria(Permission permission) |
|
177 | { |
|
178 | 0 | Criteria criteria = new Criteria(); |
179 | 0 | if (!((BaseObject) permission).isNew()) |
180 | { |
|
181 | 0 | criteria.add(PERMISSION_ID, |
182 | ((BaseObject) permission).getPrimaryKey()); |
|
183 | } |
|
184 | 0 | criteria.add(NAME, ((SecurityObject) permission).getName()); |
185 | ||
186 | /* |
|
187 | * This is causing the the removal and updating of |
|
188 | * a permission to crap out. This addition to the |
|
189 | * criteria produces something like: |
|
190 | * |
|
191 | * where OBJECTDATA = {} |
|
192 | * |
|
193 | * Is the NAME even necessary. Wouldn't |
|
194 | * criteria.add(PERMISSION_ID, N) be enough to |
|
195 | * generate a where clause that would remove the |
|
196 | * permission? |
|
197 | * |
|
198 | * criteria.add(OBJECTDATA, permission.getAttributes()); |
|
199 | */ |
|
200 | 0 | return criteria; |
201 | } |
|
202 | ||
203 | /** |
|
204 | * Issues an update based on a criteria. |
|
205 | * |
|
206 | * @param criteria Object containing data that is used to create |
|
207 | * the UPDATE statement. |
|
208 | * @exception TorqueException a generic exception. |
|
209 | */ |
|
210 | public static void doUpdate(Criteria criteria) |
|
211 | throws TorqueException |
|
212 | { |
|
213 | 0 | Criteria selectCriteria = new Criteria(2); |
214 | 0 | selectCriteria.put(PERMISSION_ID, criteria.remove(PERMISSION_ID)); |
215 | 0 | BasePeer.doUpdate(selectCriteria, criteria); |
216 | 0 | } |
217 | ||
218 | /** |
|
219 | * Checks if a Permission is defined in the system. The name |
|
220 | * is used as query criteria. |
|
221 | * |
|
222 | * @param permission The Permission to be checked. |
|
223 | * @return <code>true</code> if given Permission exists in the system. |
|
224 | * @throws DataBackendException when more than one Permission with |
|
225 | * the same name exists. |
|
226 | * @throws Exception a generic exception. |
|
227 | */ |
|
228 | public static boolean checkExists(Permission permission) |
|
229 | throws DataBackendException, Exception |
|
230 | { |
|
231 | 0 | Criteria criteria = new Criteria(); |
232 | 0 | criteria.addSelectColumn(PERMISSION_ID); |
233 | 0 | criteria.add(NAME, ((SecurityObject) permission).getName()); |
234 | 0 | List results = BasePeer.doSelect(criteria); |
235 | 0 | if (results.size() > 1) |
236 | { |
|
237 | 0 | throw new DataBackendException("Multiple permissions named '" |
238 | + ((SecurityObject) permission).getName() + "' exist!"); |
|
239 | } |
|
240 | 0 | return (results.size() == 1); |
241 | } |
|
242 | ||
243 | /** |
|
244 | * Get the name of this table. |
|
245 | * |
|
246 | * @return A String with the name of the table. |
|
247 | */ |
|
248 | public static String getTableName() |
|
249 | { |
|
250 | 0 | return TABLE_NAME; |
251 | } |
|
252 | ||
253 | /** |
|
254 | * Returns the full name of a column. |
|
255 | * |
|
256 | * @param name name of a column |
|
257 | * @return A String with the full name of the column. |
|
258 | */ |
|
259 | public static String getColumnName(String name) |
|
260 | { |
|
261 | 0 | StringBuffer sb = new StringBuffer(); |
262 | 0 | sb.append(TABLE_NAME); |
263 | 0 | sb.append("."); |
264 | 0 | sb.append(name); |
265 | 0 | return sb.toString(); |
266 | } |
|
267 | ||
268 | /** |
|
269 | * Pass in two Vector's of Permission Objects. It will return a |
|
270 | * new Vector with the difference of the two Vectors: C = (A - B). |
|
271 | * |
|
272 | * @param some Vector B in C = (A - B). |
|
273 | * @param all Vector A in C = (A - B). |
|
274 | * @return Vector C in C = (A - B). |
|
275 | */ |
|
276 | public static final Vector getDifference(Vector some, Vector all) |
|
277 | { |
|
278 | 0 | Vector clone = (Vector) all.clone(); |
279 | 0 | for (Enumeration e = some.elements(); e.hasMoreElements();) |
280 | { |
|
281 | 0 | Permission tmp = (Permission) e.nextElement(); |
282 | 0 | for (Enumeration f = clone.elements(); f.hasMoreElements();) |
283 | { |
|
284 | 0 | Permission tmp2 = (Permission) f.nextElement(); |
285 | 0 | if (((BaseObject) tmp).getPrimaryKey() |
286 | == ((BaseObject) tmp2).getPrimaryKey()) |
|
287 | { |
|
288 | 0 | clone.removeElement(tmp2); |
289 | 0 | break; |
290 | } |
|
291 | } |
|
292 | } |
|
293 | 0 | return clone; |
294 | } |
|
295 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |