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.acl;
020
021import org.apache.wiki.auth.permissions.PagePermission;
022
023import java.io.Serializable;
024import java.security.Permission;
025import java.security.Principal;
026import java.util.Enumeration;
027import java.util.Vector;
028
029
030/**
031 * Implementation of a JSPWiki AclEntry.
032 *
033 * @since 2.3
034 */
035public class AclEntryImpl implements AclEntry, Serializable {
036
037    private static final long serialVersionUID = 1L;
038    private final Vector< Permission > m_permissions = new Vector<>();
039    private Principal m_principal;
040
041    /**
042     * Constructs a new AclEntryImpl instance.
043     */
044    public AclEntryImpl() {
045    }
046
047    /**
048     * Adds the specified permission to this ACL entry. The permission <em>must</em> be of type
049     * {@link org.apache.wiki.auth.permissions.PagePermission}. Note: An entry can have multiple permissions.
050     *
051     * @param permission the permission to be associated with the principal in this entry
052     * @return <code>true</code> if the permission was added, <code>false</code> if the permission was
053     * already part of this entry's permission set, and <code>false</code> if the permission is not of type PagePermission
054     */
055    @Override
056    public synchronized boolean addPermission(final Permission permission ) {
057        if( permission instanceof PagePermission && findPermission( permission ) == null ) {
058            m_permissions.add( permission );
059            return true;
060        }
061
062        return false;
063    }
064
065    /**
066     * Checks if the specified permission is part of the permission set in this entry.
067     *
068     * @param permission the permission to be checked for.
069     * @return true if the permission is part of the permission set in this entry, false otherwise.
070     */
071    @Override
072    public boolean checkPermission(final Permission permission ) {
073        return findPermission( permission ) != null;
074    }
075
076    /**
077     * Returns the principal for which permissions are granted by this ACL entry. Returns null if there is no principal set for this
078     * entry yet.
079     *
080     * @return the principal associated with this entry.
081     */
082    @Override
083    public synchronized Principal getPrincipal() {
084        return m_principal;
085    }
086
087    /**
088     * Returns an enumeration of the permissions in this ACL entry.
089     *
090     * @return an enumeration of the permissions
091     */
092    @Override
093    public Enumeration< Permission > permissions() {
094        return m_permissions.elements();
095    }
096
097    /**
098     * Removes the specified permission from this ACL entry.
099     *
100     * @param permission the permission to be removed from this entry.
101     * @return true if the permission is removed, false if the permission was not part of this entry's permission set.
102     */
103    @Override
104    public synchronized boolean removePermission(final Permission permission ) {
105        final Permission p = findPermission( permission );
106        if( p != null ) {
107            m_permissions.remove( p );
108            return true;
109        }
110
111        return false;
112    }
113
114    /**
115     * Specifies the principal for which permissions are granted or denied by this ACL entry. If a principal was already set for this ACL
116     * entry, false is returned, otherwise true is returned.
117     *
118     * @param user the principal to be set for this entry
119     * @return true if the principal is set, false if there was already a
120     * principal set for this entry
121     */
122    @Override
123    public synchronized boolean setPrincipal(final Principal user ) {
124        if( m_principal != null || user == null ) {
125            return false;
126        }
127        m_principal = user;
128        return true;
129    }
130
131    /**
132     * Returns a string representation of the contents of this ACL entry.
133     *
134     * @return a string representation of the contents.
135     */
136    public String toString() {
137        final Principal p = getPrincipal();
138        final StringBuilder sb = new StringBuilder();
139        sb.append( "[AclEntry ALLOW " )
140          .append( p != null ? p.getName() : "null" )
141          .append( " " );
142
143        for( final Permission pp : m_permissions ) {
144            sb.append( pp.toString() );
145            sb.append( "," );
146        }
147
148        sb.append( "]" );
149
150        return sb.toString();
151    }
152
153    /**
154     * Looks through the permission list and finds a permission that matches the
155     * permission.
156     */
157    private Permission findPermission( final Permission p ) {
158        for( final Permission pp : m_permissions ) {
159            if( pp.implies( p ) ) {
160                return pp;
161            }
162        }
163        return null;
164    }
165
166}
167