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.torque.om.Persistent;
025:
026: import org.apache.turbine.om.security.Group;
027: import org.apache.turbine.om.security.Role;
028: import org.apache.turbine.om.security.User;
029: import org.apache.turbine.services.security.TurbineSecurity;
030: import org.apache.turbine.util.security.RoleSet;
031: import org.apache.turbine.util.security.TurbineSecurityException;
032:
033: /**
034: * This class represents a Group of Users in the system that are associated
035: * with specific entity or resource. The users belonging to the Group may
036: * have various Roles. The Permissions to perform actions upon the resource
037: * depend on the Roles in the Group that they are assigned. It is separated
038: * from the actual Torque peer object to be able to replace the Peer with an
039: * user supplied Peer (and Object)
040: *
041: * <a name="global">
042: * <p> Certain Roles that the Users may have in the system are not related
043: * to any specific resource nor entity.
044: * They are assigned within a special group named 'global' that can be
045: * referenced in the code as {@link #GLOBAL_GROUP_NAME}.
046: * <br>
047: *
048: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
049: * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
050: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
051: * @version $Id: TorqueGroup.java 534527 2007-05-02 16:10:59Z tv $
052: */
053:
054: public class TorqueGroup extends TorqueObject implements Group,
055: Comparable {
056:
057: /** Serial Version UID */
058: private static final long serialVersionUID = -2034684697021752888L;
059:
060: /**
061: * Constructs a new Group.
062: */
063: public TorqueGroup() {
064: super ();
065: }
066:
067: /**
068: * Constructs a new Group with the specified name.
069: *
070: * @param name The name of the new object.
071: */
072:
073: public TorqueGroup(String name) {
074: super (name);
075: }
076:
077: /**
078: * The package private Constructor is used when the GroupPeerManager
079: * has retrieved a list of Database Objects from the peer and
080: * must 'wrap' them into TorqueGroup Objects.
081: * You should not use it directly!
082: *
083: * @param obj An Object from the peer
084: */
085: public TorqueGroup(Persistent obj) {
086: super (obj);
087: }
088:
089: /**
090: * Returns the underlying Object for the Peer
091: *
092: * Used in the GroupPeerManager when building a new Criteria.
093: *
094: * @return The underlying persistent object
095: *
096: */
097:
098: public Persistent getPersistentObj() {
099: if (obj == null) {
100: obj = GroupPeerManager.newPersistentInstance();
101: }
102: return obj;
103: }
104:
105: /**
106: * Returns the name of this object.
107: *
108: * @return The name of the object.
109: */
110: public String getName() {
111: return GroupPeerManager.getGroupName(getPersistentObj());
112: }
113:
114: /**
115: * Sets the name of this object.
116: *
117: * @param name The name of the object.
118: */
119: public void setName(String name) {
120: GroupPeerManager.setGroupName(getPersistentObj(), name);
121: }
122:
123: /**
124: * Gets the Id of this object
125: *
126: * @return The Id of the object
127: */
128: public int getId() {
129: return GroupPeerManager.getIdAsObj(getPersistentObj())
130: .intValue();
131: }
132:
133: /**
134: * Gets the Id of this object
135: *
136: * @return The Id of the object
137: */
138: public Integer getIdAsObj() {
139: return GroupPeerManager.getIdAsObj(getPersistentObj());
140: }
141:
142: /**
143: * Sets the Id of this object
144: *
145: * @param id The new Id
146: */
147: public void setId(int id) {
148: GroupPeerManager.setId(getPersistentObj(), id);
149: }
150:
151: /**
152: * Provides a reference to the Group object that represents the
153: * <a href="#global">global group</a>.
154: *
155: * @return a Group object that represents the global group.
156: * @deprecated Please use the method in TurbineSecurity now.
157: */
158: public static Group getGlobalGroup() {
159: return TurbineSecurity.getGlobalGroup();
160: }
161:
162: /**
163: * Creates a new Group in the system.
164: *
165: * @param name The name of the new Group.
166: * @return An object representing the new Group.
167: * @throws TurbineSecurityException if the Group could not be created.
168: * @deprecated Please use the createGroup method in TurbineSecurity now.
169: */
170: public static Group create(String name)
171: throws TurbineSecurityException {
172: return TurbineSecurity.createGroup(name);
173: }
174:
175: // These following methods are wrappers around TurbineSecurity
176:
177: /**
178: * Makes changes made to the Group attributes permanent.
179: *
180: * @throws TurbineSecurityException if there is a problem while
181: * saving data.
182: */
183: public void save() throws TurbineSecurityException {
184: TurbineSecurity.saveGroup(this );
185: }
186:
187: /**
188: * Removes a group from the system.
189: *
190: * @throws TurbineSecurityException if the Group could not be removed.
191: */
192: public void remove() throws TurbineSecurityException {
193: TurbineSecurity.removeGroup(this );
194: }
195:
196: /**
197: * Renames the role.
198: *
199: * @param name The new Group name.
200: * @throws TurbineSecurityException if the Group could not be renamed.
201: */
202: public void rename(String name) throws TurbineSecurityException {
203: TurbineSecurity.renameGroup(this , name);
204: }
205:
206: /**
207: * Grants a Role in this Group to an User.
208: *
209: * @param user An User.
210: * @param role A Role.
211: * @throws TurbineSecurityException if there is a problem while assigning
212: * the Role.
213: */
214: public void grant(User user, Role role)
215: throws TurbineSecurityException {
216: TurbineSecurity.grant(user, this , role);
217: }
218:
219: /**
220: * Grants Roles in this Group to an User.
221: *
222: * @param user An User.
223: * @param roleSet A RoleSet.
224: * @throws TurbineSecurityException if there is a problem while assigning
225: * the Roles.
226: */
227: public void grant(User user, RoleSet roleSet)
228: throws TurbineSecurityException {
229: Iterator roles = roleSet.iterator();
230: while (roles.hasNext()) {
231: TurbineSecurity.grant(user, this , (Role) roles.next());
232: }
233: }
234:
235: /**
236: * Revokes a Role in this Group from an User.
237: *
238: * @param user An User.
239: * @param role A Role.
240: * @throws TurbineSecurityException if there is a problem while unassigning
241: * the Role.
242: */
243: public void revoke(User user, Role role)
244: throws TurbineSecurityException {
245: TurbineSecurity.revoke(user, this , role);
246: }
247:
248: /**
249: * Revokes Roles in this group from an User.
250: *
251: * @param user An User.
252: * @param roleSet a RoleSet.
253: * @throws TurbineSecurityException if there is a problem while unassigning
254: * the Roles.
255: */
256: public void revoke(User user, RoleSet roleSet)
257: throws TurbineSecurityException {
258: Iterator roles = roleSet.iterator();
259: while (roles.hasNext()) {
260: TurbineSecurity.revoke(user, this , (Role) roles.next());
261: }
262: }
263:
264: }
|