View Javadoc
1 package org.apache.turbine.om.security.peer; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import com.workingdogs.village.Record; 58 import java.util.ArrayList; 59 import java.util.Enumeration; 60 import java.util.List; 61 import java.util.Map; 62 import java.util.Vector; 63 import org.apache.torque.TorqueException; 64 import org.apache.torque.om.BaseObject; 65 import org.apache.torque.util.BasePeer; 66 import org.apache.torque.util.Criteria; 67 import org.apache.turbine.om.security.Permission; 68 import org.apache.turbine.om.security.Role; 69 import org.apache.turbine.om.security.SecurityObject; 70 import org.apache.turbine.om.security.TurbineRole; 71 import org.apache.turbine.services.security.TurbineSecurity; 72 import org.apache.turbine.util.ObjectUtils; 73 import org.apache.turbine.util.db.map.TurbineMapBuilder; 74 import org.apache.turbine.util.security.DataBackendException; 75 import org.apache.turbine.util.security.PermissionSet; 76 77 /*** 78 * This class handles all the database access for the PERMISSION 79 * table. This table contains all the permissions that are used in 80 * the system. 81 * 82 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> 83 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 84 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> 85 * @version $Id: PermissionPeer.java,v 1.3 2002/07/11 07:34:30 mpoeschl Exp $ 86 */ 87 public class PermissionPeer extends BasePeer 88 { 89 private static final TurbineMapBuilder mapBuilder = 90 (TurbineMapBuilder) getMapBuilder("org.apache.turbine.util.db.map.TurbineMapBuilder"); 91 92 /*** The table name for this peer. */ 93 private static final String TABLE_NAME = mapBuilder.getTablePermission(); 94 95 /*** The column name for the permission id field. */ 96 public static final String PERMISSION_ID = 97 mapBuilder.getPermission_PermissionId(); 98 99 /*** The column name for the name field. */ 100 public static final String NAME = mapBuilder.getPermission_Name(); 101 102 /*** The column name for the ObjectData field */ 103 public static final String OBJECTDATA = 104 mapBuilder.getPermission_ObjectData(); 105 106 /*** The Oracle sequence name for this peer. */ 107 private static final String SEQUENCE_NAME = 108 mapBuilder.getSequencePermission(); 109 110 111 /*** 112 * Retrieves/assembles a PermissionSet 113 * 114 * @param criteria The criteria to use. 115 * @return A PermissionSet. 116 * @exception Exception, a generic exception. 117 */ 118 public static PermissionSet retrieveSet(Criteria criteria) 119 throws Exception 120 { 121 List results = PermissionPeer.doSelect(criteria); 122 PermissionSet ps = new PermissionSet(); 123 for (int i = 0; i < results.size(); i++) 124 { 125 ps.add((Permission) results.get(i)); 126 } 127 return ps; 128 } 129 130 /*** 131 * Retrieves a set of Permissions associated with a particular Role. 132 * 133 * @param role The role to query permissions of. 134 * @return A set of permissions associated with the Role. 135 * @exception Exception, a generic exception. 136 */ 137 public static PermissionSet retrieveSet( Role role ) 138 throws Exception 139 { 140 Criteria criteria = new Criteria(); 141 criteria.add(RolePermissionPeer.ROLE_ID, 142 ((TurbineRole)role).getPrimaryKey()); 143 criteria.addJoin(RolePermissionPeer.PERMISSION_ID, 144 PermissionPeer.PERMISSION_ID); 145 return retrieveSet(criteria); 146 } 147 148 /*** 149 * Issues a select based on a criteria. 150 * 151 * @param criteria Object containing data that is used to create 152 * the SELECT statement. 153 * @return Vector containing Permission objects. 154 * @exception Exception, a generic exception. 155 */ 156 public static List doSelect(Criteria criteria) 157 throws TorqueException 158 { 159 try 160 { 161 criteria.addSelectColumn(PERMISSION_ID) 162 .addSelectColumn(NAME) 163 .addSelectColumn(OBJECTDATA); 164 165 if (criteria.getOrderByColumns() == null || 166 criteria.getOrderByColumns().size() == 0) 167 { 168 criteria.addAscendingOrderByColumn(NAME); 169 } 170 171 // Place any checks here to intercept criteria which require 172 // custom SQL. For example: 173 // if ( criteria.containsKey("SomeTable.SomeColumn") ) 174 // { 175 // String whereSql = "SomeTable.SomeColumn IN (Select ..."; 176 // criteria.add("SomeTable.SomeColumn", 177 // whereSQL, criteria.CUSTOM); 178 // } 179 180 // BasePeer returns a Vector of Value (Village) arrays. The 181 // array order follows the order columns were placed in the 182 // Select clause. 183 List rows = BasePeer.doSelect(criteria); 184 List results = new ArrayList(); 185 186 // Populate the object(s). 187 for ( int i=0; i<rows.size(); i++ ) 188 { 189 Permission obj = TurbineSecurity.getNewPermission(null); 190 Record row = (Record) rows.get(i); 191 ((SecurityObject) obj).setPrimaryKey( row.getValue(1).asInt() ); 192 ((SecurityObject) obj).setName( row.getValue(2).asString() ); 193 byte[] objectData = (byte[]) row.getValue(3).asBytes(); 194 Map temp = (Map) ObjectUtils.deserialize(objectData); 195 if (temp != null) 196 { 197 ((SecurityObject) obj).setAttributes(temp); 198 } 199 results.add(obj); 200 } 201 202 return results; 203 } 204 catch (Exception ex) 205 { 206 throw new TorqueException(ex); 207 } 208 } 209 210 /*** 211 * Builds a criteria object based upon an Permission object 212 */ 213 public static Criteria buildCriteria( Permission permission ) 214 { 215 Criteria criteria = new Criteria(); 216 if ( !((BaseObject)permission).isNew() ) 217 { 218 criteria.add(PERMISSION_ID, 219 ((BaseObject)permission).getPrimaryKey()); 220 } 221 criteria.add(NAME, ((SecurityObject)permission).getName()); 222 223 /* 224 * This is causing the the removal and updating of 225 * a permission to crap out. This addition to the 226 * criteria produces something like: 227 * 228 * where OBJECTDATA = {} 229 * 230 * Is the NAME even necessary. Wouldn't 231 * criteria.add(PERMISSION_ID, N) be enough to 232 * generate a where clause that would remove the 233 * permission? 234 * 235 * criteria.add(OBJECTDATA, permission.getAttributes()); 236 */ 237 return criteria; 238 } 239 240 /*** 241 * Issues an update based on a criteria. 242 * 243 * @param criteria Object containing data that is used to create 244 * the UPDATE statement. 245 * @exception Exception, a generic exception. 246 */ 247 public static void doUpdate(Criteria criteria) 248 throws TorqueException 249 { 250 Criteria selectCriteria = new Criteria(2); 251 selectCriteria.put( PERMISSION_ID, 252 criteria.remove(PERMISSION_ID) ); 253 BasePeer.doUpdate( selectCriteria, criteria ); 254 } 255 256 /*** 257 * Checks if a Permission is defined in the system. The name 258 * is used as query criteria. 259 * 260 * @param permission The Permission to be checked. 261 * @return <code>true</code> if given Permission exists in the system. 262 * @throws DataBackendException when more than one Permission with 263 * the same name exists. 264 * @throws Exception, a generic exception. 265 */ 266 public static boolean checkExists( Permission permission ) 267 throws DataBackendException, Exception 268 { 269 Criteria criteria = new Criteria(); 270 criteria.addSelectColumn(PERMISSION_ID); 271 criteria.add(NAME, ((SecurityObject)permission).getName()); 272 List results = BasePeer.doSelect(criteria); 273 if(results.size() > 1) 274 { 275 throw new DataBackendException("Multiple permissions named '" + 276 ((SecurityObject)permission).getName() + "' exist!"); 277 } 278 return (results.size()==1); 279 } 280 281 /*** 282 * Get the name of this table. 283 * 284 * @return A String with the name of the table. 285 */ 286 public static String getTableName() 287 { 288 return TABLE_NAME; 289 } 290 291 /*** 292 * Returns the full name of a column. 293 * 294 * @return A String with the full name of the column. 295 */ 296 public static String getColumnName (String name) 297 { 298 StringBuffer sb = new StringBuffer(); 299 sb.append (TABLE_NAME); 300 sb.append ("."); 301 sb.append (name); 302 return sb.toString(); 303 } 304 305 /*** 306 * Pass in two Vector's of Permission Objects. It will return a 307 * new Vector with the difference of the two Vectors: C = (A - B). 308 * 309 * @param some Vector B in C = (A - B). 310 * @param all Vector A in C = (A - B). 311 * @return Vector C in C = (A - B). 312 */ 313 public static final Vector getDifference(Vector some, 314 Vector all) 315 { 316 Vector clone = (Vector)all.clone(); 317 for (Enumeration e = some.elements() ; e.hasMoreElements() ;) 318 { 319 Permission tmp = (Permission) e.nextElement(); 320 for (Enumeration f = clone.elements() ; f.hasMoreElements() ;) 321 { 322 Permission tmp2 = (Permission) f.nextElement(); 323 if (((BaseObject)tmp).getPrimaryKey() == 324 ((BaseObject)tmp2).getPrimaryKey()) 325 { 326 clone.removeElement(tmp2); 327 break; 328 } 329 } 330 } 331 return clone; 332 } 333 }

This page was automatically generated by Maven