View Javadoc

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