1 package org.apache.turbine.om.security.peer; 2 3 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 50 public class PermissionPeer extends BasePeer 51 { 52 53 private static final TurbineMapBuilder MAP_BUILDER = (TurbineMapBuilder) 54 getMapBuilder(TurbineMapBuilder.class.getName()); 55 56 57 private static final String TABLE_NAME = MAP_BUILDER.getTablePermission(); 58 59 60 public static final String PERMISSION_ID 61 = MAP_BUILDER.getPermission_PermissionId(); 62 63 64 public static final String NAME = MAP_BUILDER.getPermission_Name(); 65 66 67 public static final String OBJECTDATA 68 = MAP_BUILDER.getPermission_ObjectData(); 69 70 77 public static PermissionSet retrieveSet(Criteria criteria) 78 throws Exception 79 { 80 List results = PermissionPeer.doSelect(criteria); 81 PermissionSet ps = new PermissionSet(); 82 for (int i = 0; i < results.size(); i++) 83 { 84 ps.add((Permission) results.get(i)); 85 } 86 return ps; 87 } 88 89 96 public static PermissionSet retrieveSet(Role role) 97 throws Exception 98 { 99 Criteria criteria = new Criteria(); 100 criteria.add(RolePermissionPeer.ROLE_ID, 101 ((TurbineRole) role).getPrimaryKey()); 102 criteria.addJoin(RolePermissionPeer.PERMISSION_ID, 103 PermissionPeer.PERMISSION_ID); 104 return retrieveSet(criteria); 105 } 106 107 115 public static List doSelect(Criteria criteria) 116 throws TorqueException 117 { 118 try 119 { 120 criteria.addSelectColumn(PERMISSION_ID) 121 .addSelectColumn(NAME) 122 .addSelectColumn(OBJECTDATA); 123 124 if (criteria.getOrderByColumns() == null 125 || criteria.getOrderByColumns().size() == 0) 126 { 127 criteria.addAscendingOrderByColumn(NAME); 128 } 129 130 139 List rows = BasePeer.doSelect(criteria); 143 List results = new ArrayList (); 144 145 for (int i = 0; i < rows.size(); i++) 147 { 148 Permission obj = TurbineSecurity.getPermissionInstance(null); 149 Record row = (Record) rows.get(i); 150 ((SecurityObject) obj).setPrimaryKey( 151 new NumberKey(row.getValue(1).asInt())); 152 ((SecurityObject) obj).setName(row.getValue(2).asString()); 153 byte[] objectData = row.getValue(3).asBytes(); 154 Map temp = (Map ) ObjectUtils.deserialize(objectData); 155 if (temp != null) 156 { 157 ((SecurityObject) obj).setAttributes(temp); 158 } 159 results.add(obj); 160 } 161 162 return results; 163 } 164 catch (Exception ex) 165 { 166 throw new TorqueException(ex); 167 } 168 } 169 170 176 public static Criteria buildCriteria(Permission permission) 177 { 178 Criteria criteria = new Criteria(); 179 if (!((BaseObject) permission).isNew()) 180 { 181 criteria.add(PERMISSION_ID, 182 ((BaseObject) permission).getPrimaryKey()); 183 } 184 criteria.add(NAME, ((SecurityObject) permission).getName()); 185 186 200 return criteria; 201 } 202 203 210 public static void doUpdate(Criteria criteria) 211 throws TorqueException 212 { 213 Criteria selectCriteria = new Criteria(2); 214 selectCriteria.put(PERMISSION_ID, criteria.remove(PERMISSION_ID)); 215 BasePeer.doUpdate(selectCriteria, criteria); 216 } 217 218 228 public static boolean checkExists(Permission permission) 229 throws DataBackendException, Exception 230 { 231 Criteria criteria = new Criteria(); 232 criteria.addSelectColumn(PERMISSION_ID); 233 criteria.add(NAME, ((SecurityObject) permission).getName()); 234 List results = BasePeer.doSelect(criteria); 235 if (results.size() > 1) 236 { 237 throw new DataBackendException("Multiple permissions named '" 238 + ((SecurityObject) permission).getName() + "' exist!"); 239 } 240 return (results.size() == 1); 241 } 242 243 248 public static String getTableName() 249 { 250 return TABLE_NAME; 251 } 252 253 259 public static String getColumnName(String name) 260 { 261 StringBuffer sb = new StringBuffer (); 262 sb.append(TABLE_NAME); 263 sb.append("."); 264 sb.append(name); 265 return sb.toString(); 266 } 267 268 276 public static final Vector getDifference(Vector some, Vector all) 277 { 278 Vector clone = (Vector ) all.clone(); 279 for (Enumeration e = some.elements(); e.hasMoreElements();) 280 { 281 Permission tmp = (Permission) e.nextElement(); 282 for (Enumeration f = clone.elements(); f.hasMoreElements();) 283 { 284 Permission tmp2 = (Permission) f.nextElement(); 285 if (((BaseObject) tmp).getPrimaryKey() 286 == ((BaseObject) tmp2).getPrimaryKey()) 287 { 288 clone.removeElement(tmp2); 289 break; 290 } 291 } 292 } 293 return clone; 294 } 295 } 296 | Popular Tags |