001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.om.page;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.StringTokenizer;
023:
024: import org.apache.jetspeed.om.common.SecurityConstraint;
025: import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
026:
027: /**
028: * <p>
029: * SecurityConstraintImpl
030: * </p>
031: * <p>
032: *
033: * </p>
034: * @author <a href="mailto:rwatler@finali.com">Randy Watler</a>
035: * @version $Id: SecurityConstraintImpl.java 516448 2007-03-09 16:25:47Z ate $
036: *
037: */
038: public class SecurityConstraintImpl implements SecurityConstraint {
039: private int id;
040: private int applyOrder;
041: private List usersList;
042: private List rolesList;
043: private List groupsList;
044: private List permissionsList;
045:
046: private String users;
047: private String roles;
048: private String groups;
049: private String permissions;
050:
051: /**
052: * getApplyOrder
053: *
054: * @return apply order for constraints
055: */
056: public int getApplyOrder() {
057: return applyOrder;
058: }
059:
060: /**
061: * setApplyOrder
062: *
063: * @param order apply order for constraints
064: */
065: public void setApplyOrder(int order) {
066: applyOrder = order;
067: }
068:
069: /**
070: * getUsersAsString
071: *
072: * @return users CSV list
073: */
074: public String getUsersAsString() {
075: // get from users list if not immediately available
076: if ((users == null) && (usersList != null)
077: && !usersList.isEmpty()) {
078: users = formatCSVList(usersList);
079: }
080: return users;
081: }
082:
083: /**
084: * setUsersAsString
085: *
086: * @param users users CSV list
087: */
088: public void setUsersAsString(String users) {
089: // set and propagate to users list setting
090: this .users = users;
091: usersList = parseCSVList(users);
092: }
093:
094: /**
095: * getRolesAsString
096: *
097: * @return roles CSV list
098: */
099: public String getRolesAsString() {
100: // get from roles list if not immediately available
101: if ((roles == null) && (rolesList != null)
102: && !rolesList.isEmpty()) {
103: roles = formatCSVList(rolesList);
104: }
105: return roles;
106: }
107:
108: /**
109: * setRolesAsString
110: *
111: * @param roles roles CSV list
112: */
113: public void setRolesAsString(String roles) {
114: // set and propagate to roles list setting
115: this .roles = roles;
116: rolesList = parseCSVList(roles);
117: }
118:
119: /**
120: * getGroupsAsString
121: *
122: * @return groups CSV list
123: */
124: public String getGroupsAsString() {
125: // get from groups list if not immediately available
126: if ((groups == null) && (groupsList != null)
127: && !groupsList.isEmpty()) {
128: groups = formatCSVList(groupsList);
129: }
130: return groups;
131: }
132:
133: /**
134: * setGroupsAsString
135: *
136: * @param groups groups CSV list
137: */
138: public void setGroupsAsString(String groups) {
139: // set and propagate to groups list setting
140: this .groups = groups;
141: groupsList = parseCSVList(groups);
142: }
143:
144: /**
145: * getPermissionsAsString
146: *
147: * @return permissions CSV list
148: */
149: public String getPermissionsAsString() {
150: // get from permissions list if not immediately available
151: if ((permissions == null) && (permissionsList != null)
152: && !permissionsList.isEmpty()) {
153: permissions = formatCSVList(permissionsList);
154: }
155: return permissions;
156: }
157:
158: /**
159: * setPermissionsAsString
160: *
161: * @param permissions permissions CSV list
162: */
163: public void setPermissionsAsString(String permissions) {
164: // set and propagate to permissions list setting
165: this .permissions = permissions;
166: permissionsList = parseCSVList(permissions);
167: }
168:
169: /**
170: * <p>
171: * getUsers
172: * </p>
173: *
174: * @see org.apache.jetspeed.om.common.SecurityConstraint#getUsers()
175: * @return users list
176: */
177: public List getUsers() {
178: return usersList;
179: }
180:
181: /**
182: * <p>
183: * setUsers
184: * </p>
185: *
186: * @see org.apache.jetspeed.om.common.SecurityConstraint#setUsers(java.util.List)
187: * @param users users list
188: */
189: public void setUsers(List users) {
190: // set and clear potentially stale string representation
191: usersList = users;
192: this .users = null;
193: }
194:
195: /**
196: * <p>
197: * getRoles
198: * </p>
199: *
200: * @see org.apache.jetspeed.om.common.SecurityConstraint#getRoles()
201: * @return roles list
202: */
203: public List getRoles() {
204: return rolesList;
205: }
206:
207: /**
208: * <p>
209: * setRoles
210: * </p>
211: *
212: * @see org.apache.jetspeed.om.common.SecurityConstraint#setRoles(java.util.List)
213: * @param roles roles list
214: */
215: public void setRoles(List roles) {
216: // set and clear potentially stale string representation
217: rolesList = roles;
218: this .roles = null;
219: }
220:
221: /**
222: * <p>
223: * getGroups
224: * </p>
225: *
226: * @see org.apache.jetspeed.om.common.SecurityConstraint#getGroups()
227: * @return groups list
228: */
229: public List getGroups() {
230: return groupsList;
231: }
232:
233: /**
234: * <p>
235: * setGroups
236: * </p>
237: *
238: * @see org.apache.jetspeed.om.common.SecurityConstraint#setGroups(java.util.List)
239: * @param groups groups list
240: */
241: public void setGroups(List groups) {
242: // set and clear potentially stale string representation
243: groupsList = groups;
244: this .groups = null;
245: }
246:
247: /**
248: * <p>
249: * getPermissions
250: * </p>
251: *
252: * @see org.apache.jetspeed.om.common.SecurityConstraint#getPermissions()
253: * @return permissions list
254: */
255: public List getPermissions() {
256: return permissionsList;
257: }
258:
259: /**
260: * <p>
261: * setPermissions
262: * </p>
263: *
264: * @see org.apache.jetspeed.om.common.SecurityConstraint#setPermissions(java.util.List)
265: * @param permissions permissions list
266: */
267: public void setPermissions(List permissions) {
268: // set and clear potentially stale string representation
269: permissionsList = permissions;
270: this .permissions = null;
271: }
272:
273: /**
274: * <p>
275: * principalsMatch
276: * </p>
277: * <p>
278: * Test user/role/group names against principal names.
279: * </p>
280: *
281: * @param userPrincipals
282: * @param rolePrincipals
283: * @param groupPrincipals
284: * @param allowDefault
285: * @return match result
286: */
287: public boolean principalsMatch(List userPrincipals,
288: List rolePrincipals, List groupPrincipals,
289: boolean allowDefault) {
290: // test match using users, roles, and groups list members
291: // since these are the master representation in this impl
292: return ((allowDefault && (usersList == null)
293: && (rolesList == null) && (groupsList == null))
294: || ((usersList != null) && (userPrincipals != null) && (containsAny(
295: usersList, userPrincipals) || usersList
296: .contains(WILD_CHAR)))
297: || ((rolesList != null) && (rolePrincipals != null) && (containsAny(
298: rolesList, rolePrincipals) || rolesList
299: .contains(WILD_CHAR))) || ((groupsList != null)
300: && (groupPrincipals != null) && (containsAny(
301: groupsList, groupPrincipals) || groupsList
302: .contains(WILD_CHAR))));
303: }
304:
305: /**
306: * <p>
307: * actionMatch
308: * </p>
309: * <p>
310: * Test permission names against action name.
311: * </p>
312: *
313: * @param action
314: * @return match result
315: */
316: public boolean actionMatch(String action) {
317: // test match using permissions list member since
318: // this is the master representation in this impl
319: return ((permissionsList != null) && (permissionsList
320: .contains(action) || permissionsList
321: .contains(WILD_CHAR)));
322: }
323:
324: /**
325: * <p>
326: * parseCSVList
327: * </p>
328: * <p>
329: * Utility to parse CSV string values into Lists.
330: * </p>
331: *
332: * @param csv
333: * @return parsed values list.
334: */
335: public static List parseCSVList(String csv) {
336: if (csv != null) {
337: List csvList = DatabasePageManagerUtils.createList();
338: if (csv.indexOf(',') != -1) {
339: StringTokenizer csvTokens = new StringTokenizer(csv,
340: ",");
341: while (csvTokens.hasMoreTokens()) {
342: csvList.add(csvTokens.nextToken().trim());
343: }
344: } else {
345: csvList.add(csv);
346: }
347: return csvList;
348: }
349: return null;
350: }
351:
352: /**
353: * <p>
354: * formatCSVList
355: * </p>
356: * <p>
357: * Utility to format CSV List values into strings.
358: * </p>
359: *
360: * @param list
361: * @return formatted string value.
362: */
363: public static String formatCSVList(List list) {
364: if ((list != null) && !list.isEmpty()) {
365: StringBuffer csv = new StringBuffer();
366: Iterator listIter = list.iterator();
367: while (listIter.hasNext()) {
368: if (csv.length() > 0) {
369: csv.append(",");
370: }
371: csv.append((String) listIter.next());
372: }
373: return csv.toString();
374: }
375: return null;
376: }
377:
378: /**
379: * <p>
380: * containsAny
381: * </p>
382: * <p>
383: * Utility implementation for contains any test against two collections.
384: * </p>
385: *
386: * @param collection0
387: * @param collection1
388: * @return contains any result.
389: */
390: public static boolean containsAny(Collection collection0,
391: Collection collection1) {
392: if ((collection0 != null) && (collection1 != null)) {
393: Iterator containsIter = collection1.iterator();
394: while (containsIter.hasNext()) {
395: if (collection0.contains(containsIter.next())) {
396: return true;
397: }
398: }
399: }
400: return false;
401: }
402: }
|