001: package org.apache.turbine.util.security;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import org.apache.commons.lang.StringUtils;
023:
024: import org.apache.turbine.om.security.Permission;
025:
026: /**
027: * This class represents a set of Permissions. It makes it easy to
028: * build a UI that would allow someone to add a group of Permissions
029: * to a Role. It enforces that only
030: * Permission objects are allowed in the set and only relevant methods
031: * are available.
032: *
033: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
034: * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
035: * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
036: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037: * @version $Id: PermissionSet.java 278822 2005-09-05 19:53:05Z henning $
038: */
039: public class PermissionSet extends SecuritySet {
040: /** Serial Version UID */
041: private static final long serialVersionUID = 8276935936763076884L;
042:
043: /**
044: * Constructs an empty PermissionSet
045: */
046: public PermissionSet() {
047: super ();
048: }
049:
050: /**
051: * Constructs a new PermissionSet with specified contents.
052: *
053: * If the given collection contains multiple objects that are
054: * identical WRT equals() method, some objects will be overwritten.
055: *
056: * @param permissions A collection of permissions to be contained in the set.
057: */
058: public PermissionSet(Collection permissions) {
059: super ();
060: add(permissions);
061: }
062:
063: /**
064: * Adds a Permission to this PermissionSet.
065: *
066: * @param permission A Permission.
067: * @return True if Permission was added; false if PermissionSet
068: * already contained the Permission.
069: */
070: public boolean add(Permission permission) {
071: boolean res = contains(permission);
072: nameMap.put(permission.getName(), permission);
073: idMap.put(permission.getIdAsObj(), permission);
074: return res;
075: }
076:
077: /**
078: * Adds the Permissions in a Collection to this PermissionSet.
079: *
080: * @param permissions A Collection of Permissions.
081: * @return True if this PermissionSet changed as a result; false
082: * if no change to this PermissionSet occurred (this PermissionSet
083: * already contained all members of the added PermissionSet).
084: */
085: public boolean add(Collection permissions) {
086: boolean res = false;
087: for (Iterator it = permissions.iterator(); it.hasNext();) {
088: Permission p = (Permission) it.next();
089: res |= add(p);
090: }
091: return res;
092: }
093:
094: /**
095: * Adds the Permissions in another PermissionSet to this
096: * PermissionSet.
097: *
098: * @param permissionSet A PermissionSet.
099: * @return True if this PermissionSet changed as a result; false
100: * if no change to this PermissionSet occurred (this PermissionSet
101: * already contained all members of the added PermissionSet).
102: */
103: public boolean add(PermissionSet permissionSet) {
104: boolean res = false;
105: for (Iterator it = permissionSet.iterator(); it.hasNext();) {
106: Permission p = (Permission) it.next();
107: res |= add(p);
108: }
109: return res;
110: }
111:
112: /**
113: * Removes a Permission from this PermissionSet.
114: *
115: * @param permission A Permission.
116: * @return True if this PermissionSet contained the Permission
117: * before it was removed.
118: */
119: public boolean remove(Permission permission) {
120: boolean res = contains(permission);
121: nameMap.remove(permission.getName());
122: idMap.remove(permission.getIdAsObj());
123: return res;
124: }
125:
126: /**
127: * Checks whether this PermissionSet contains a Permission.
128: *
129: * @param permission A Permission.
130: * @return True if this PermissionSet contains the Permission,
131: * false otherwise.
132: */
133: public boolean contains(Permission permission) {
134: return nameMap.containsValue((Object) permission);
135: }
136:
137: /**
138: * Returns a Permission with the given name, if it is contained in
139: * this PermissionSet.
140: *
141: * @param permissionName Name of Permission.
142: * @return Permission if argument matched a Permission in this
143: * PermissionSet; null if no match.
144: * @deprecated Use <a href="#getPermissionByName">getPermissionByName</a> instead.
145: */
146: public Permission getPermission(String permissionName) {
147: return getPermissionByName(permissionName);
148: }
149:
150: /**
151: * Returns a Permission with the given name, if it is contained in
152: * this PermissionSet.
153: *
154: * @param permissionName Name of Permission.
155: * @return Permission if argument matched a Permission in this
156: * PermissionSet; null if no match.
157: */
158: public Permission getPermissionByName(String permissionName) {
159: return (StringUtils.isNotEmpty(permissionName)) ? (Permission) nameMap
160: .get(permissionName)
161: : null;
162: }
163:
164: /**
165: * Returns a Permission with the given id, if it is contained in
166: * this PermissionSet.
167: *
168: * @param permissionId Id of the Permission.
169: * @return Permission if argument matched a Permission in this
170: * PermissionSet; null if no match.
171: */
172: public Permission getPermissionById(int permissionId) {
173: return (permissionId != 0) ? (Permission) idMap
174: .get(new Integer(permissionId)) : null;
175: }
176:
177: /**
178: * Returns an Array of Permissions in this PermissionSet.
179: *
180: * @return An Array of Permission Objects.
181: */
182: public Permission[] getPermissionsArray() {
183: return (Permission[]) getSet().toArray(new Permission[0]);
184: }
185:
186: /**
187: * Print out a PermissionSet as a String
188: *
189: * @returns The Permission Set as String
190: *
191: */
192: public String toString() {
193: StringBuffer sb = new StringBuffer();
194: sb.append("PermissionSet: ");
195:
196: for (Iterator it = iterator(); it.hasNext();) {
197: Permission p = (Permission) it.next();
198: sb.append('[');
199: sb.append(p.getName());
200: sb.append(" -> ");
201: sb.append(p.getIdAsObj());
202: sb.append(']');
203: if (it.hasNext()) {
204: sb.append(", ");
205: }
206: }
207:
208: return sb.toString();
209: }
210: }
|