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 extends TorqueObject implements Role,
046: Comparable {
047:
048: private static final long serialVersionUID = -7774684697021445523L;
049:
050: /** The permissions for this role. */
051: private PermissionSet permissionSet = null;
052:
053: /**
054: * Constructs a new Role
055: */
056: public TorqueRole() {
057: super ();
058: }
059:
060: /**
061: * Constructs a new Role with the specified name.
062: *
063: * @param name The name of the new object.
064: */
065: public TorqueRole(String name) {
066: super (name);
067: }
068:
069: /**
070: * The package private Constructor is used when the RolePeerManager
071: * has retrieved a list of Database Objects from the peer and
072: * must 'wrap' them into TorqueRole Objects. You should not use it directly!
073: *
074: * @param obj An Object from the peer
075: */
076: public TorqueRole(Persistent obj) {
077: super (obj);
078: }
079:
080: /**
081: * Returns the underlying Object for the Peer
082: *
083: * Used in the RolePeerManager when building a new Criteria.
084: *
085: * @return The underlying persistent object
086: *
087: */
088:
089: public Persistent getPersistentObj() {
090: if (obj == null) {
091: obj = RolePeerManager.newPersistentInstance();
092: }
093: return obj;
094: }
095:
096: /**
097: * Returns the name of this role.
098: *
099: * @return The name of the role.
100: */
101: public String getName() {
102: return RolePeerManager.getRoleName(getPersistentObj());
103: }
104:
105: /**
106: * Sets the name of this Role
107: *
108: * @param name The name of the role.
109: */
110: public void setName(String name) {
111: RolePeerManager.setRoleName(getPersistentObj(), name);
112: }
113:
114: /**
115: * Gets the Id of this object
116: *
117: * @return The Id of the object
118: */
119: public int getId() {
120: return RolePeerManager.getIdAsObj(getPersistentObj())
121: .intValue();
122: }
123:
124: /**
125: * Gets the Id of this object
126: *
127: * @return The Id of the object
128: */
129: public Integer getIdAsObj() {
130: return RolePeerManager.getIdAsObj(getPersistentObj());
131: }
132:
133: /**
134: * Sets the Id of this object
135: *
136: * @param id The new Id
137: */
138: public void setId(int id) {
139: RolePeerManager.setId(getPersistentObj(), id);
140: }
141:
142: /**
143: * Returns the set of Permissions associated with this Role.
144: *
145: * @return A PermissionSet.
146: *
147: * @exception Exception a generic exception.
148: */
149: public PermissionSet getPermissions() throws Exception {
150: return permissionSet;
151: }
152:
153: /**
154: * Sets the Permissions associated with this Role.
155: *
156: * @param permissionSet A PermissionSet.
157: */
158: public void setPermissions(PermissionSet permissionSet) {
159: this .permissionSet = permissionSet;
160: }
161:
162: // These following methods are wrappers around TurbineSecurity
163:
164: /**
165: * Creates a new Role in the system.
166: *
167: * @param name The name of the new Role.
168: * @return An object representing the new Role.
169: * @throws TurbineSecurityException if the Role could not be created.
170: */
171: public Role create(String name) throws TurbineSecurityException {
172: return TurbineSecurity.createRole(name);
173: }
174:
175: /**
176: * Makes changes made to the Role attributes permanent.
177: *
178: * @throws TurbineSecurityException if there is a problem while
179: * saving data.
180: */
181: public void save() throws TurbineSecurityException {
182: TurbineSecurity.saveRole(this );
183: }
184:
185: /**
186: * Removes a role from the system.
187: *
188: * @throws TurbineSecurityException if the Role could not be removed.
189: */
190: public void remove() throws TurbineSecurityException {
191: TurbineSecurity.removeRole(this );
192: }
193:
194: /**
195: * Renames the role.
196: *
197: * @param name The new Role name.
198: * @throws TurbineSecurityException if the Role could not be renamed.
199: */
200: public void rename(String name) throws TurbineSecurityException {
201: TurbineSecurity.renameRole(this , name);
202: }
203:
204: /**
205: * Grants a Permission to this Role.
206: *
207: * @param permission A Permission.
208: * @throws TurbineSecurityException if there is a problem while assigning
209: * the Permission.
210: */
211: public void grant(Permission permission)
212: throws TurbineSecurityException {
213: TurbineSecurity.grant(this , permission);
214: }
215:
216: /**
217: * Grants Permissions from a PermissionSet to this Role.
218: *
219: * @param permissionSet A PermissionSet.
220: * @throws TurbineSecurityException if there is a problem while assigning
221: * the Permissions.
222: */
223: public void grant(PermissionSet permissionSet)
224: throws TurbineSecurityException {
225: Iterator permissions = permissionSet.iterator();
226: while (permissions.hasNext()) {
227: TurbineSecurity
228: .grant(this , (Permission) permissions.next());
229: }
230: }
231:
232: /**
233: * Revokes a Permission from this Role.
234: *
235: * @param permission A Permission.
236: * @throws TurbineSecurityException if there is a problem while unassigning
237: * the Permission.
238: */
239: public void revoke(Permission permission)
240: throws TurbineSecurityException {
241: TurbineSecurity.revoke(this , permission);
242: }
243:
244: /**
245: * Revokes Permissions from a PermissionSet from this Role.
246: *
247: * @param permissionSet A PermissionSet.
248: * @throws TurbineSecurityException if there is a problem while unassigning
249: * the Permissions.
250: */
251: public void revoke(PermissionSet permissionSet)
252: throws TurbineSecurityException {
253: Iterator permissions = permissionSet.iterator();
254: while (permissions.hasNext()) {
255: TurbineSecurity.revoke(this , (Permission) permissions
256: .next());
257: }
258: }
259: }
|