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