View Javadoc

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