    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    @Override
    077    public String getActions() {
    078        return null;
    079    }
    080
    081    /**
    082     * Returns the name of the wiki containing the page represented by this
    083     * permission; may return the wildcard string.
    084     *
    085     * @return The wiki
    086     */
    087    public String getWiki() {
    088        return m_wiki;
    089    }
    090
    091    /**
    092     * Returns the hash code for this WikiPermission.
    093     *
    094     * @return {@inheritDoc}
    095     * @see java.lang.Object#hashCode()
    096     */
    097    public int hashCode() {
    098        return m_wiki.hashCode();
    099    }
    100
    101    /**
    102     * WikiPermission can only imply other WikiPermissions; no other permission
    103     * types are implied. One WikiPermission implies another if all of the other
    104     * WikiPermission's actions are equal to, or a subset of, those for this
    105     * permission.
    106     *
    107     * @param permission the permission which may (or may not) be implied by
    108     *                   this instance
    109     * @return <code>true</code> if the permission is implied,
    110     * <code>false</code> otherwise
    111     * @see java.security.Permission#implies(java.security.Permission)
    112     */
    113    @Override
    114    public boolean implies(final Permission permission ) {
    115        // Permission must be a JSPWiki permission, PagePermission or AllPermission
    116        if( !PermissionChecks.isJSPWikiPermission( permission ) ) {
    117            return false;
    118        }
    119        String wiki = null;
    120        if( permission instanceof AllPermission ) {
    121            wiki = ( ( AllPermission )permission ).getWiki();
    122        } else if( permission instanceof PagePermission ) {
    123            wiki = ( ( PagePermission )permission ).getWiki();
    124        }
    125        if( permission instanceof WikiPermission ) {
    126            wiki = ( ( WikiPermission )permission ).getWiki();
    127        }
    128        if( permission instanceof GroupPermission ) {
    129            wiki = ( ( GroupPermission )permission ).getWiki();
    130        }
    131
    132        // If the wiki is implied, it's allowed
    133        return PagePermission.isSubset( m_wiki, wiki );
    134    }
    135
    136    /**
    137     * Returns a new {@link AllPermissionCollection}.
    138     *
    139     * @return {@inheritDoc}
    140     * @see java.security.Permission#newPermissionCollection()
    141     */
    142    @Override
    143    public PermissionCollection newPermissionCollection() {
    144        return new AllPermissionCollection();
    145    }
    146
    147    /**
    148     * Prints a human-readable representation of this permission.
    149     *
    150     * @return {@inheritDoc}
    151     * @see java.lang.Object#toString()
    152     */
    153    public String toString() {
    154        return "(\"" + this.getClass().getName() + "\",\"" + m_wiki + "\")";
    155    }
    156
    157}