001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  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,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.auth.permissions;
020
021import java.io.Serializable;
022import java.security.Permission;
023import java.security.PermissionCollection;
024
025/**
026 * <p>
027 * Permission to perform all operations on a given wiki.
028 * </p>
029 * @since 2.3.80
030 */
031public final class AllPermission extends Permission implements Serializable {
032
033    private static final long serialVersionUID = 1L;
034
035    private static final String WILDCARD = "*";
036
037    private final String m_wiki;
038
039    /** For serialization purposes. */
040    AllPermission() {
041        this( null );
042    }
043
044    /**
045     * Creates a new AllPermission for the given wikis.
046     *
047     * @param wiki the wiki to which the permission should apply.  If null, will
048     *             apply to all wikis.
049     */
050    public AllPermission( final String wiki ) {
051        super( wiki );
052        m_wiki = ( wiki == null ) ? WILDCARD : wiki;
053    }
054
055    /**
056     * Two AllPermission objects are considered equal if their wikis are equal.
057     *
058     * @param obj {@inheritDoc}
059     * @return {@inheritDoc}
060     * @see java.lang.Object#equals(java.lang.Object)
061     */
062    public boolean equals( final Object obj ) {
063        if( !( obj instanceof AllPermission ) ) {
064            return false;
065        }
066        final AllPermission p = ( AllPermission )obj;
067        return p.m_wiki != null && p.m_wiki.equals( m_wiki );
068    }
069
070    /**
071     * No-op; always returns <code>null</code>
072     *
073     * @return Always null.
074     * @see java.security.Permission#getActions()
075     */
076    public String getActions() {
077        return null;
078    }
079
080    /**
081     * Returns the name of the wiki containing the page represented by this
082     * permission; may return the wildcard string.
083     *
084     * @return The wiki
085     */
086    public String getWiki() {
087        return m_wiki;
088    }
089
090    /**
091     * Returns the hash code for this WikiPermission.
092     *
093     * @return {@inheritDoc}
094     * @see java.lang.Object#hashCode()
095     */
096    public int hashCode() {
097        return m_wiki.hashCode();
098    }
099
100    /**
101     * WikiPermission can only imply other WikiPermissions; no other permission
102     * types are implied. One WikiPermission implies another if all of the other
103     * WikiPermission's actions are equal to, or a subset of, those for this
104     * permission.
105     *
106     * @param permission the permission which may (or may not) be implied by
107     *                   this instance
108     * @return <code>true</code> if the permission is implied,
109     * <code>false</code> otherwise
110     * @see java.security.Permission#implies(java.security.Permission)
111     */
112    public boolean implies( final Permission permission ) {
113        // Permission must be a JSPWiki permission, PagePermission or AllPermission
114        if( !PermissionChecks.isJSPWikiPermission( permission ) ) {
115            return false;
116        }
117        String wiki = null;
118        if( permission instanceof AllPermission ) {
119            wiki = ( ( AllPermission )permission ).getWiki();
120        } else if( permission instanceof PagePermission ) {
121            wiki = ( ( PagePermission )permission ).getWiki();
122        }
123        if( permission instanceof WikiPermission ) {
124            wiki = ( ( WikiPermission )permission ).getWiki();
125        }
126        if( permission instanceof GroupPermission ) {
127            wiki = ( ( GroupPermission )permission ).getWiki();
128        }
129
130        // If the wiki is implied, it's allowed
131        return PagePermission.isSubset( m_wiki, wiki );
132    }
133
134    /**
135     * Returns a new {@link AllPermissionCollection}.
136     *
137     * @return {@inheritDoc}
138     * @see java.security.Permission#newPermissionCollection()
139     */
140    public PermissionCollection newPermissionCollection() {
141        return new AllPermissionCollection();
142    }
143
144    /**
145     * Prints a human-readable representation of this permission.
146     *
147     * @return {@inheritDoc}
148     * @see java.lang.Object#toString()
149     */
150    public String toString() {
151        return "(\"" + this.getClass().getName() + "\",\"" + m_wiki + "\")";
152    }
153
154}