001 package org.apache.turbine.services.security.torque;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.util.Iterator;
023
024 import org.apache.turbine.om.security.Permission;
025 import org.apache.turbine.om.security.Role;
026 import org.apache.turbine.services.security.TurbineSecurity;
027 import org.apache.turbine.util.security.PermissionSet;
028 import org.apache.turbine.util.security.TurbineSecurityException;
029
030 import org.apache.torque.om.Persistent;
031
032 /**
033 * This class represents a role played by the User associated with the
034 * current Session. It is separated from the actual Torque peer object
035 * to be able to replace the Peer with an user supplied Peer (and Object)
036 *
037 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
038 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
039 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
040 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
041 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
042 * @version $Id: TorqueRole.java 534527 2007-05-02 16:10:59Z tv $
043 */
044
045 public class TorqueRole
046 extends TorqueObject
047 implements Role,
048 Comparable
049 {
050
051 private static final long serialVersionUID = -7774684697021445523L;
052
053 /** The permissions for this role. */
054 private PermissionSet permissionSet = null;
055
056 /**
057 * Constructs a new Role
058 */
059 public TorqueRole()
060 {
061 super();
062 }
063
064 /**
065 * Constructs a new Role with the specified name.
066 *
067 * @param name The name of the new object.
068 */
069 public TorqueRole(String name)
070 {
071 super(name);
072 }
073
074 /**
075 * The package private Constructor is used when the RolePeerManager
076 * has retrieved a list of Database Objects from the peer and
077 * must 'wrap' them into TorqueRole Objects. You should not use it directly!
078 *
079 * @param obj An Object from the peer
080 */
081 public TorqueRole(Persistent obj)
082 {
083 super(obj);
084 }
085
086 /**
087 * Returns the underlying Object for the Peer
088 *
089 * Used in the RolePeerManager when building a new Criteria.
090 *
091 * @return The underlying persistent object
092 *
093 */
094
095 public Persistent getPersistentObj()
096 {
097 if (obj == null)
098 {
099 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 }