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