View Javadoc

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