001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2007 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.auth.permissions;
021:
022: import java.security.Permission;
023: import java.security.PermissionCollection;
024: import java.util.Enumeration;
025: import java.util.Hashtable;
026:
027: /**
028: * A collection of AllPermission objects.
029: * @author Andrew R. Jaquith
030: */
031: public class AllPermissionCollection extends PermissionCollection {
032:
033: private static final long serialVersionUID = 1L;
034:
035: private boolean m_notEmpty = false;
036:
037: private boolean m_readOnly = false;
038:
039: protected final Hashtable m_permissions = new Hashtable();
040:
041: /**
042: * Adds an AllPermission object to this AllPermissionCollection. If this
043: * collection was previously marked read-only, or if the permission supplied
044: * is not of type {@link AllPermission}, a {@link SecurityException} is
045: * thrown.
046: * @see java.security.PermissionCollection#add(java.security.Permission)
047: */
048: public void add(Permission permission) {
049: if (!AllPermission.isJSPWikiPermission(permission)) {
050: throw new IllegalArgumentException(
051: "Permission must be of type com.ecyrd.jspwiki.permissions.*Permission.");
052: }
053:
054: if (m_readOnly) {
055: throw new SecurityException(
056: "attempt to add a Permission to a readonly PermissionCollection");
057: }
058:
059: m_notEmpty = true;
060:
061: // This is a filthy hack, but it keeps us from having to write our own
062: // Enumeration implementation
063: m_permissions.put(permission, permission);
064: }
065:
066: /**
067: * Returns an enumeration of all AllPermission objects stored in this
068: * collection.
069: * @see java.security.PermissionCollection#elements()
070: */
071: public Enumeration elements() {
072: return m_permissions.elements();
073: }
074:
075: /**
076: * Iterates through the AllPermission objects stored by this
077: * AllPermissionCollection and determines if any of them imply a supplied
078: * Permission. If the Permission is not of type {@link AllPermission},
079: * {@link PagePermission} or {@link WikiPermission}, this method will
080: * return <code>false</code>. If none of the AllPermissions stored in
081: * this collection imply the permission, the method returns
082: * <code>false</code>; conversely, if one of the AllPermission objects
083: * implies the permission, the method returns <code>true</code>.
084: * @param permission the Permission to test. It may be any Permission type,
085: * but only the AllPermission, PagePermission or WikiPermission types are
086: * actually evaluated.
087: * @see java.security.PermissionCollection#implies(java.security.Permission)
088: */
089: public boolean implies(Permission permission) {
090: // If nothing in the collection yet, fail fast
091: if (!m_notEmpty) {
092: return false;
093: }
094:
095: // If not one of our permission types, it's not implied
096: if (!AllPermission.isJSPWikiPermission(permission)) {
097: return false;
098: }
099:
100: // Step through each AllPermission
101: Enumeration permEnum = m_permissions.elements();
102: while (permEnum.hasMoreElements()) {
103: Permission storedPermission = (Permission) permEnum
104: .nextElement();
105: if (storedPermission.implies(permission)) {
106: return true;
107: }
108: }
109: return false;
110: }
111:
112: /**
113: * @see java.security.PermissionCollection#isReadOnly()
114: */
115: public boolean isReadOnly() {
116: return m_readOnly;
117: }
118:
119: /**
120: * @see java.security.PermissionCollection#setReadOnly()
121: */
122: public void setReadOnly() {
123: m_readOnly = true;
124: }
125: }
|