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:
025: /**
026: * <p>
027: * Permission to perform all operations on a given wiki.
028: * </p>
029: * @author Andrew Jaquith
030: * @since 2.3.80
031: */
032: public final class AllPermission extends Permission {
033: private static final long serialVersionUID = 1L;
034:
035: private static final String WILDCARD = "*";
036:
037: private final String m_wiki;
038:
039: /**
040: * Creates a new AllPermission for the given wikis.
041: *
042: * @param wiki the wiki to which the permission should apply. If null, will
043: * apply to all wikis.
044: */
045: public AllPermission(String wiki) {
046: super (wiki);
047: m_wiki = (wiki == null) ? WILDCARD : wiki;
048: }
049:
050: /**
051: * Two AllPermission objects are considered equal if their wikis are equal.
052: * @see java.lang.Object#equals(java.lang.Object)
053: */
054: public final boolean equals(Object obj) {
055: if (!(obj instanceof AllPermission)) {
056: return false;
057: }
058: AllPermission p = (AllPermission) obj;
059: return p.m_wiki != null && p.m_wiki.equals(m_wiki);
060: }
061:
062: /**
063: * No-op; always returns <code>null</code>
064: * @see java.security.Permission#getActions()
065: */
066: public final String getActions() {
067: return null;
068: }
069:
070: /**
071: * Returns the name of the wiki containing the page represented by this
072: * permission; may return the wildcard string.
073: * @return the wiki
074: */
075: public final String getWiki() {
076: return m_wiki;
077: }
078:
079: /**
080: * Returns the hash code for this WikiPermission.
081: * @see java.lang.Object#hashCode()
082: */
083: public final int hashCode() {
084: return m_wiki.hashCode();
085: }
086:
087: /**
088: * WikiPermission can only imply other WikiPermissions; no other permission
089: * types are implied. One WikiPermission implies another if all of the other
090: * WikiPermission's actions are equal to, or a subset of, those for this
091: * permission.
092: * @param permission the permission which may (or may not) be implied by
093: * this instance
094: * @return <code>true</code> if the permission is implied,
095: * <code>false</code> otherwise
096: * @see java.security.Permission#implies(java.security.Permission)
097: */
098: public final boolean implies(Permission permission) {
099: // Permission must be a JSPWiki permission, PagePermission or AllPermission
100: if (!isJSPWikiPermission(permission)) {
101: return false;
102: }
103: String wiki = null;
104: if (permission instanceof AllPermission) {
105: wiki = ((AllPermission) permission).getWiki();
106: } else if (permission instanceof PagePermission) {
107: wiki = ((PagePermission) permission).getWiki();
108: }
109: if (permission instanceof WikiPermission) {
110: wiki = ((WikiPermission) permission).getWiki();
111: }
112: if (permission instanceof GroupPermission) {
113: wiki = ((GroupPermission) permission).getWiki();
114: }
115:
116: // If the wiki is implied, it's allowed
117: return PagePermission.isSubset(m_wiki, wiki);
118: }
119:
120: /**
121: * Returns a new {@link AllPermissionCollection}.
122: * @see java.security.Permission#newPermissionCollection()
123: */
124: public PermissionCollection newPermissionCollection() {
125: return new AllPermissionCollection();
126: }
127:
128: /**
129: * Prints a human-readable representation of this permission.
130: * @see java.lang.Object#toString()
131: */
132: public final String toString() {
133: return "(\"" + this .getClass().getName() + "\",\"" + m_wiki
134: + "\")";
135: }
136:
137: protected static final boolean isJSPWikiPermission(
138: Permission permission) {
139: return permission instanceof WikiPermission
140: || permission instanceof PagePermission
141: || permission instanceof GroupPermission
142: || permission instanceof AllPermission;
143: }
144:
145: }
|