1 package org.apache.turbine.services.security.torque; 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.Iterator; 23 24 import org.apache.turbine.om.security.Permission; 25 import org.apache.turbine.om.security.Role; 26 import org.apache.turbine.services.security.TurbineSecurity; 27 import org.apache.turbine.util.security.PermissionSet; 28 import org.apache.turbine.util.security.TurbineSecurityException; 29 30 import org.apache.torque.om.Persistent; 31 32 /** 33 * This class represents a role played by the User associated with the 34 * current Session. It is separated from the actual Torque peer object 35 * to be able to replace the Peer with an user supplied Peer (and Object) 36 * 37 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> 38 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> 39 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> 40 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> 41 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 42 * @version $Id: TorqueRole.java 534527 2007-05-02 16:10:59Z tv $ 43 */ 44 45 public class TorqueRole 46 extends TorqueObject 47 implements Role, 48 Comparable 49 { 50 51 private static final long serialVersionUID = -7774684697021445523L; 52 53 /** The permissions for this role. */ 54 private PermissionSet permissionSet = null; 55 56 /** 57 * Constructs a new Role 58 */ 59 public TorqueRole() 60 { 61 super(); 62 } 63 64 /** 65 * Constructs a new Role with the specified name. 66 * 67 * @param name The name of the new object. 68 */ 69 public TorqueRole(String name) 70 { 71 super(name); 72 } 73 74 /** 75 * The package private Constructor is used when the RolePeerManager 76 * has retrieved a list of Database Objects from the peer and 77 * must 'wrap' them into TorqueRole Objects. You should not use it directly! 78 * 79 * @param obj An Object from the peer 80 */ 81 public TorqueRole(Persistent obj) 82 { 83 super(obj); 84 } 85 86 /** 87 * Returns the underlying Object for the Peer 88 * 89 * Used in the RolePeerManager when building a new Criteria. 90 * 91 * @return The underlying persistent object 92 * 93 */ 94 95 public Persistent getPersistentObj() 96 { 97 if (obj == null) 98 { 99 obj = RolePeerManager.newPersistentInstance(); 100 } 101 return obj; 102 } 103 104 /** 105 * Returns the name of this role. 106 * 107 * @return The name of the role. 108 */ 109 public String getName() 110 { 111 return RolePeerManager.getRoleName(getPersistentObj()); 112 } 113 114 /** 115 * Sets the name of this Role 116 * 117 * @param name The name of the role. 118 */ 119 public void setName(String name) 120 { 121 RolePeerManager.setRoleName(getPersistentObj(), name); 122 } 123 124 /** 125 * Gets the Id of this object 126 * 127 * @return The Id of the object 128 */ 129 public int getId() 130 { 131 return RolePeerManager.getIdAsObj(getPersistentObj()).intValue(); 132 } 133 134 /** 135 * Gets the Id of this object 136 * 137 * @return The Id of the object 138 */ 139 public Integer getIdAsObj() 140 { 141 return RolePeerManager.getIdAsObj(getPersistentObj()); 142 } 143 144 /** 145 * Sets the Id of this object 146 * 147 * @param id The new Id 148 */ 149 public void setId(int id) 150 { 151 RolePeerManager.setId(getPersistentObj(), id); 152 } 153 /** 154 * Returns the set of Permissions associated with this Role. 155 * 156 * @return A PermissionSet. 157 * 158 * @exception Exception a generic exception. 159 */ 160 public PermissionSet getPermissions() 161 throws Exception 162 { 163 return permissionSet; 164 } 165 166 /** 167 * Sets the Permissions associated with this Role. 168 * 169 * @param permissionSet A PermissionSet. 170 */ 171 public void setPermissions(PermissionSet permissionSet) 172 { 173 this.permissionSet = permissionSet; 174 } 175 176 // These following methods are wrappers around TurbineSecurity 177 178 /** 179 * Creates a new Role in the system. 180 * 181 * @param name The name of the new Role. 182 * @return An object representing the new Role. 183 * @throws TurbineSecurityException if the Role could not be created. 184 */ 185 public Role create(String name) 186 throws TurbineSecurityException 187 { 188 return TurbineSecurity.createRole(name); 189 } 190 191 /** 192 * Makes changes made to the Role attributes permanent. 193 * 194 * @throws TurbineSecurityException if there is a problem while 195 * saving data. 196 */ 197 public void save() 198 throws TurbineSecurityException 199 { 200 TurbineSecurity.saveRole(this); 201 } 202 203 /** 204 * Removes a role from the system. 205 * 206 * @throws TurbineSecurityException if the Role could not be removed. 207 */ 208 public void remove() 209 throws TurbineSecurityException 210 { 211 TurbineSecurity.removeRole(this); 212 } 213 214 /** 215 * Renames the role. 216 * 217 * @param name The new Role name. 218 * @throws TurbineSecurityException if the Role could not be renamed. 219 */ 220 public void rename(String name) 221 throws TurbineSecurityException 222 { 223 TurbineSecurity.renameRole(this, name); 224 } 225 226 /** 227 * Grants a Permission to this Role. 228 * 229 * @param permission A Permission. 230 * @throws TurbineSecurityException if there is a problem while assigning 231 * the Permission. 232 */ 233 public void grant(Permission permission) 234 throws TurbineSecurityException 235 { 236 TurbineSecurity.grant(this, permission); 237 } 238 239 /** 240 * Grants Permissions from a PermissionSet to this Role. 241 * 242 * @param permissionSet A PermissionSet. 243 * @throws TurbineSecurityException if there is a problem while assigning 244 * the Permissions. 245 */ 246 public void grant(PermissionSet permissionSet) 247 throws TurbineSecurityException 248 { 249 Iterator permissions = permissionSet.iterator(); 250 while (permissions.hasNext()) 251 { 252 TurbineSecurity.grant(this, (Permission) permissions.next()); 253 } 254 } 255 256 /** 257 * Revokes a Permission from this Role. 258 * 259 * @param permission A Permission. 260 * @throws TurbineSecurityException if there is a problem while unassigning 261 * the Permission. 262 */ 263 public void revoke(Permission permission) 264 throws TurbineSecurityException 265 { 266 TurbineSecurity.revoke(this, permission); 267 } 268 269 /** 270 * Revokes Permissions from a PermissionSet from this Role. 271 * 272 * @param permissionSet A PermissionSet. 273 * @throws TurbineSecurityException if there is a problem while unassigning 274 * the Permissions. 275 */ 276 public void revoke(PermissionSet permissionSet) 277 throws TurbineSecurityException 278 { 279 Iterator permissions = permissionSet.iterator(); 280 while (permissions.hasNext()) 281 { 282 TurbineSecurity.revoke(this, (Permission) permissions.next()); 283 } 284 } 285 }