1 package org.apache.turbine.services.security.torque;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Iterator;
20
21 import org.apache.torque.om.Persistent;
22
23 import org.apache.turbine.om.security.Group;
24 import org.apache.turbine.om.security.Role;
25 import org.apache.turbine.om.security.User;
26 import org.apache.turbine.services.security.TurbineSecurity;
27 import org.apache.turbine.util.security.RoleSet;
28 import org.apache.turbine.util.security.TurbineSecurityException;
29
30 /***
31 * This class represents a Group of Users in the system that are associated
32 * with specific entity or resource. The users belonging to the Group may
33 * have various Roles. The Permissions to perform actions upon the resource
34 * depend on the Roles in the Group that they are assigned. It is separated
35 * from the actual Torque peer object to be able to replace the Peer with an
36 * user supplied Peer (and Object)
37 *
38 * <a name="global">
39 * <p> Certain Roles that the Users may have in the system are not related
40 * to any specific resource nor entity.
41 * They are assigned within a special group named 'global' that can be
42 * referenced in the code as {@link #GLOBAL_GROUP_NAME}.
43 * <br>
44 *
45 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
46 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
47 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
48 * @version $Id: TorqueGroup.java,v 1.4.2.2 2004/05/20 03:06:50 seade Exp $
49 */
50
51 public class TorqueGroup
52 extends TorqueObject
53 implements Group,
54 Comparable,
55 Persistent
56 {
57 /***
58 * Constructs a new Group.
59 */
60 public TorqueGroup()
61 {
62 super();
63 }
64
65 /***
66 * Constructs a new Group with the specified name.
67 *
68 * @param name The name of the new object.
69 */
70
71 public TorqueGroup(String name)
72 {
73 super(name);
74 }
75
76 /***
77 * The package private Constructor is used when the GroupPeerManager
78 * has retrieved a list of Database Objects from the peer and
79 * must 'wrap' them into TorqueGroup Objects.
80 * You should not use it directly!
81 *
82 * @param obj An Object from the peer
83 */
84 public TorqueGroup(Persistent obj)
85 {
86 super(obj);
87 }
88
89 /***
90 * Returns the underlying Object for the Peer
91 *
92 * Used in the GroupPeerManager when building a new Criteria.
93 *
94 * @return The underlying persistent object
95 *
96 */
97
98 public Persistent getPersistentObj()
99 {
100 if (obj == null)
101 {
102 obj = GroupPeerManager.newPersistentInstance();
103 }
104 return obj;
105 }
106
107 /***
108 * Returns the name of this object.
109 *
110 * @return The name of the object.
111 */
112 public String getName()
113 {
114 return GroupPeerManager.getGroupName(getPersistentObj());
115 }
116
117 /***
118 * Sets the name of this object.
119 *
120 * @param name The name of the object.
121 */
122 public void setName(String name)
123 {
124 GroupPeerManager.setGroupName(getPersistentObj(), name);
125 }
126
127 /***
128 * Gets the Id of this object
129 *
130 * @return The Id of the object
131 */
132 public int getId()
133 {
134 return GroupPeerManager.getIdAsObj(getPersistentObj()).intValue();
135 }
136
137 /***
138 * Gets the Id of this object
139 *
140 * @return The Id of the object
141 */
142 public Integer getIdAsObj()
143 {
144 return GroupPeerManager.getIdAsObj(getPersistentObj());
145 }
146
147 /***
148 * Sets the Id of this object
149 *
150 * @param id The new Id
151 */
152 public void setId(int id)
153 {
154 GroupPeerManager.setId(getPersistentObj(), id);
155 }
156
157 /***
158 * Provides a reference to the Group object that represents the
159 * <a href="#global">global group</a>.
160 *
161 * @return a Group object that represents the global group.
162 * @deprecated Please use the method in TurbineSecurity now.
163 */
164 public static Group getGlobalGroup()
165 {
166 return TurbineSecurity.getGlobalGroup();
167 }
168
169 /***
170 * Creates a new Group in the system.
171 *
172 * @param name The name of the new Group.
173 * @return An object representing the new Group.
174 * @throws TurbineSecurityException if the Group could not be created.
175 * @deprecated Please use the createGroup method in TurbineSecurity now.
176 */
177 public static Group create(String name)
178 throws TurbineSecurityException
179 {
180 return TurbineSecurity.createGroup(name);
181 }
182
183
184
185 /***
186 * Makes changes made to the Group 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.saveGroup(this);
195 }
196
197 /***
198 * Removes a group from the system.
199 *
200 * @throws TurbineSecurityException if the Group could not be removed.
201 */
202 public void remove()
203 throws TurbineSecurityException
204 {
205 TurbineSecurity.removeGroup(this);
206 }
207
208 /***
209 * Renames the role.
210 *
211 * @param name The new Group name.
212 * @throws TurbineSecurityException if the Group could not be renamed.
213 */
214 public void rename(String name)
215 throws TurbineSecurityException
216 {
217 TurbineSecurity.renameGroup(this, name);
218 }
219
220 /***
221 * Grants a Role in this Group to an User.
222 *
223 * @param user An User.
224 * @param role A Role.
225 * @throws TurbineSecurityException if there is a problem while assigning
226 * the Role.
227 */
228 public void grant(User user, Role role)
229 throws TurbineSecurityException
230 {
231 TurbineSecurity.grant(user, this, role);
232 }
233
234 /***
235 * Grants Roles in this Group to an User.
236 *
237 * @param user An User.
238 * @param roleSet A RoleSet.
239 * @throws TurbineSecurityException if there is a problem while assigning
240 * the Roles.
241 */
242 public void grant(User user, RoleSet roleSet)
243 throws TurbineSecurityException
244 {
245 Iterator roles = roleSet.iterator();
246 while (roles.hasNext())
247 {
248 TurbineSecurity.grant(user, this, (Role) roles.next());
249 }
250 }
251
252 /***
253 * Revokes a Role in this Group from an User.
254 *
255 * @param user An User.
256 * @param role A Role.
257 * @throws TurbineSecurityException if there is a problem while unassigning
258 * the Role.
259 */
260 public void revoke(User user, Role role)
261 throws TurbineSecurityException
262 {
263 TurbineSecurity.revoke(user, this, role);
264 }
265
266 /***
267 * Revokes Roles in this group from an User.
268 *
269 * @param user An User.
270 * @param roleSet a RoleSet.
271 * @throws TurbineSecurityException if there is a problem while unassigning
272 * the Roles.
273 */
274 public void revoke(User user, RoleSet roleSet)
275 throws TurbineSecurityException
276 {
277 Iterator roles = roleSet.iterator();
278 while (roles.hasNext())
279 {
280 TurbineSecurity.revoke(user, this, (Role) roles.next());
281 }
282 }
283
284 }
285