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.api.core.AclEntry; 022import org.apache.wiki.util.Synchronizer; 023 024import java.io.Serializable; 025import java.security.Permission; 026import java.security.Principal; 027import java.util.ArrayList; 028import java.util.Collections; 029import java.util.Enumeration; 030import java.util.List; 031import java.util.concurrent.CopyOnWriteArrayList; 032import java.util.concurrent.locks.ReentrantLock; 033 034 035/** 036 * JSPWiki implementation of an Access Control List. 037 * 038 * @since 2.3 039 */ 040public class AclImpl implements Acl, Serializable { 041 042 /** 043 * A lock used to ensure thread safety when accessing shared resources. 044 * This lock provides more flexibility and capabilities than the intrinsic locking mechanism, 045 * such as the ability to attempt to acquire a lock with a timeout, or to interrupt a thread 046 * waiting to acquire a lock. 047 * 048 * @see java.util.concurrent.locks.ReentrantLock 049 */ 050 private final ReentrantLock lock = new ReentrantLock(); 051 052 private static final long serialVersionUID = 1L; 053 private final List< AclEntry > m_entries = new CopyOnWriteArrayList<>(); 054 055 /** {@inheritDoc} */ 056 @Override 057 public Principal[] findPrincipals( final Permission permission ) { 058 final List< Principal > principals = new ArrayList<>(); 059 final Enumeration< AclEntry > entries = aclEntries(); 060 while( entries.hasMoreElements() ) { 061 final AclEntry entry = entries.nextElement(); 062 final Enumeration< Permission > permissions = entry.permissions(); 063 while( permissions.hasMoreElements() ) { 064 final Permission perm = permissions.nextElement(); 065 if( perm.implies( permission ) ) { 066 principals.add( entry.getPrincipal() ); 067 } 068 } 069 } 070 return principals.toArray( new Principal[0] ); 071 } 072 073 private boolean hasEntry( final AclEntry entry ) { 074 if( entry == null ) { 075 return false; 076 } 077 078 for( final AclEntry e : m_entries ) { 079 final Principal ep = e.getPrincipal(); 080 final Principal entryp = entry.getPrincipal(); 081 082 if( ep == null || entryp == null ) { 083 throw new IllegalArgumentException( "Entry is null; check code, please (entry=" + entry + "; e=" + e + ")" ); 084 } 085 086 if( ep.getName().equals( entryp.getName() ) ) { 087 return true; 088 } 089 } 090 091 return false; 092 } 093 094 /** {@inheritDoc} */ 095 @Override 096 public boolean addEntry( final AclEntry entry ) { 097 return Synchronizer.synchronize(lock, () -> { 098 if( entry.getPrincipal() == null ) { 099 throw new IllegalArgumentException( "Entry principal cannot be null" ); 100 } 101 102 if( hasEntry( entry ) ) { 103 return false; 104 } 105 106 m_entries.add( entry ); 107 108 return true; 109 }); 110 } 111 112 /** {@inheritDoc} */ 113 @Override 114 public boolean removeEntry( final AclEntry entry ) { 115 return Synchronizer.synchronize(lock, () -> m_entries.remove( entry )); 116 } 117 118 /** {@inheritDoc} */ 119 @Override 120 public Enumeration< AclEntry > aclEntries() { 121 return Collections.enumeration(m_entries); 122 } 123 124 /** {@inheritDoc} */ 125 @Override 126 public AclEntry getAclEntry( final Principal principal ) { 127 return m_entries.stream().filter(entry -> entry.getPrincipal().getName().equals(principal.getName())).findFirst().orElse(null); 128 129 } 130 131 /** {@inheritDoc} */ 132 @Override 133 public boolean isEmpty() { 134 return m_entries.isEmpty(); 135 } 136 137 /** {@inheritDoc} */ 138 @Override 139 public String toString() { 140 final StringBuilder sb = new StringBuilder(); 141 for( final AclEntry entry : m_entries ) { 142 final Principal pal = entry.getPrincipal(); 143 if( pal != null ) { 144 sb.append( " user = " ).append( pal.getName() ).append( ": " ); 145 } else { 146 sb.append( " user = null: " ); 147 } 148 sb.append( "(" ); 149 for( final Enumeration< Permission > perms = entry.permissions(); perms.hasMoreElements(); ) { 150 final Permission perm = perms.nextElement(); 151 sb.append( perm.toString() ); 152 } 153 sb.append( ")\n" ); 154 } 155 return sb.toString(); 156 } 157 158} 159