1 package org.apache.turbine.util.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Collection;
20 import java.util.Iterator;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import org.apache.turbine.om.security.Permission;
25
26 /***
27 * This class represents a set of Permissions. It makes it easy to
28 * build a UI that would allow someone to add a group of Permissions
29 * to a Role. It enforces that only
30 * Permission objects are allowed in the set and only relevant methods
31 * are available.
32 *
33 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
34 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
35 * @author <a href="mailto:marco@intermeta.de">Marco Knüttel</a>
36 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37 * @version $Id: PermissionSet.java 278822 2005-09-05 19:53:05Z henning $
38 */
39 public class PermissionSet
40 extends SecuritySet
41 {
42 /*** Serial Version UID */
43 private static final long serialVersionUID = 8276935936763076884L;
44
45 /***
46 * Constructs an empty PermissionSet
47 */
48 public PermissionSet()
49 {
50 super();
51 }
52
53 /***
54 * Constructs a new PermissionSet with specified contents.
55 *
56 * If the given collection contains multiple objects that are
57 * identical WRT equals() method, some objects will be overwritten.
58 *
59 * @param permissions A collection of permissions to be contained in the set.
60 */
61 public PermissionSet(Collection permissions)
62 {
63 super();
64 add(permissions);
65 }
66
67 /***
68 * Adds a Permission to this PermissionSet.
69 *
70 * @param permission A Permission.
71 * @return True if Permission was added; false if PermissionSet
72 * already contained the Permission.
73 */
74 public boolean add(Permission permission)
75 {
76 boolean res = contains(permission);
77 nameMap.put(permission.getName(), permission);
78 idMap.put(permission.getIdAsObj(), permission);
79 return res;
80 }
81
82 /***
83 * Adds the Permissions in a Collection to this PermissionSet.
84 *
85 * @param permissions A Collection of Permissions.
86 * @return True if this PermissionSet changed as a result; false
87 * if no change to this PermissionSet occurred (this PermissionSet
88 * already contained all members of the added PermissionSet).
89 */
90 public boolean add(Collection permissions)
91 {
92 boolean res = false;
93 for (Iterator it = permissions.iterator(); it.hasNext();)
94 {
95 Permission p = (Permission) it.next();
96 res |= add(p);
97 }
98 return res;
99 }
100
101 /***
102 * Adds the Permissions in another PermissionSet to this
103 * PermissionSet.
104 *
105 * @param permissionSet A PermissionSet.
106 * @return True if this PermissionSet changed as a result; false
107 * if no change to this PermissionSet occurred (this PermissionSet
108 * already contained all members of the added PermissionSet).
109 */
110 public boolean add(PermissionSet permissionSet)
111 {
112 boolean res = false;
113 for( Iterator it = permissionSet.iterator(); it.hasNext();)
114 {
115 Permission p = (Permission) it.next();
116 res |= add(p);
117 }
118 return res;
119 }
120
121 /***
122 * Removes a Permission from this PermissionSet.
123 *
124 * @param permission A Permission.
125 * @return True if this PermissionSet contained the Permission
126 * before it was removed.
127 */
128 public boolean remove(Permission permission)
129 {
130 boolean res = contains(permission);
131 nameMap.remove(permission.getName());
132 idMap.remove(permission.getIdAsObj());
133 return res;
134 }
135
136 /***
137 * Checks whether this PermissionSet contains a Permission.
138 *
139 * @param permission A Permission.
140 * @return True if this PermissionSet contains the Permission,
141 * false otherwise.
142 */
143 public boolean contains(Permission permission)
144 {
145 return nameMap.containsValue((Object) permission);
146 }
147
148 /***
149 * Returns a Permission with the given name, if it is contained in
150 * this PermissionSet.
151 *
152 * @param permissionName Name of Permission.
153 * @return Permission if argument matched a Permission in this
154 * PermissionSet; null if no match.
155 * @deprecated Use <a href="#getPermissionByName">getPermissionByName</a> instead.
156 */
157 public Permission getPermission(String permissionName)
158 {
159 return getPermissionByName(permissionName);
160 }
161
162 /***
163 * Returns a Permission with the given name, if it is contained in
164 * this PermissionSet.
165 *
166 * @param permissionName Name of Permission.
167 * @return Permission if argument matched a Permission in this
168 * PermissionSet; null if no match.
169 */
170 public Permission getPermissionByName(String permissionName)
171 {
172 return (StringUtils.isNotEmpty(permissionName))
173 ? (Permission) nameMap.get(permissionName) : null;
174 }
175
176 /***
177 * Returns a Permission with the given id, if it is contained in
178 * this PermissionSet.
179 *
180 * @param permissionId Id of the Permission.
181 * @return Permission if argument matched a Permission in this
182 * PermissionSet; null if no match.
183 */
184 public Permission getPermissionById(int permissionId)
185 {
186 return (permissionId != 0)
187 ? (Permission) idMap.get(new Integer(permissionId)) : null;
188 }
189
190 /***
191 * Returns an Array of Permissions in this PermissionSet.
192 *
193 * @return An Array of Permission Objects.
194 */
195 public Permission[] getPermissionsArray()
196 {
197 return (Permission[]) getSet().toArray(new Permission[0]);
198 }
199
200 /***
201 * Print out a PermissionSet as a String
202 *
203 * @returns The Permission Set as String
204 *
205 */
206 public String toString()
207 {
208 StringBuffer sb = new StringBuffer();
209 sb.append("PermissionSet: ");
210
211 for(Iterator it = iterator(); it.hasNext();)
212 {
213 Permission p = (Permission) it.next();
214 sb.append('[');
215 sb.append(p.getName());
216 sb.append(" -> ");
217 sb.append(p.getIdAsObj());
218 sb.append(']');
219 if (it.hasNext())
220 {
221 sb.append(", ");
222 }
223 }
224
225 return sb.toString();
226 }
227 }