/* * JBoss, Home of Professional Open Source. * Copyright 2008, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.security.acl; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.Collection; import java.util.HashSet; import java.util.Map; import java.util.Set; import org.jboss.security.PicketBoxMessages; import org.jboss.security.authorization.AuthorizationException; import org.jboss.security.authorization.Resource; import org.jboss.security.authorization.ResourceKeys; import org.jboss.security.identity.Identity; /** *
* This class is the standard {@code ACLProvider} implementation. The access control decisions are based on the name of * the specified identity (that is, it assumes that entries in an ACL are keyed by the name of the identity and not by * other attributes, like the its roles). *
* * @author Stefan Guilhen */ public class ACLProviderImpl implements ACLProvider { private static final String PERSISTENCE_STRATEGY_OPTION = "persistenceStrategy"; private static final String CHECK_PARENT_ACL_OPTION = "checkParentACL"; /** persistence strategy used to retrieve the ACLs */ protected ACLPersistenceStrategy strategy; private boolean checkParentACL; /* * (non-Javadoc) * * @see org.jboss.security.acl.ACLProvider#initialize(java.util.Map, java.util.Map) */ public void initialize(Map* Helper method that populates the {@code entitlements} collection as it traverses through the resources. The * resources are visited using a depth-first search algorithm, and when each node is visited one of the following * happens: *
* This method retrieves the permissions the specified identity has over the specified resource. It starts by looking * for the resource's ACL. If one is found and if the ACL has entry for the identity, the respective permissions are * returned. If no entry is found, we assume the identity hasn't been assigned any permissions and {@code null} is * returned. *
** If the resource doesn't have an associated ACL, we start looking for an ACL in the parent resource recursively, * until an ACL is located or until no parent resource is found. In the first case, the algorithm described above is * used to return the identity's permissions. In the latter case, we return all permissions (lack of an ACL means * that the resource is not protected and the user should be granted all permissions). *
* * @param resource the {@code Resource} for which we want to discover the permissions that have been assigned to the * specified identity. * @param identityName a {@code String} representing the identity for which we want to discover the permissions * regarding the specified resource. * @return an {@code ACLPermission} containing the permissions that have been assigned to the identity with respect * to the specified resource, or {@code null} if the identity has no permissions at all. */ protected ACLPermission getInitialPermissions(Resource resource, String identityName) { ACL acl = this.strategy.getACL(resource); // if no ACL was found, try to find a parent ACL. if (acl == null) { Resource parent = (Resource) resource.getMap().get(ResourceKeys.PARENT_RESOURCE); if (parent != null) return getInitialPermissions(parent, identityName); // no ACL was found and no parent resource exists - identity has all permissions as resource is not protected. return new CompositeACLPermission(BasicACLPermission.values()); } // if an ACL was found, return the permissions associated with the specified identity. ACLEntry entry = acl.getEntry(identityName); if (entry != null) return entry.getPermission(); // the absence of an entry means that the identity has no permissions over the specified resource. return null; } /* * (non-Javadoc) * * @see org.jboss.security.acl.ACLProvider#getPersistenceStrategy() */ public ACLPersistenceStrategy getPersistenceStrategy() { return this.strategy; } /* * (non-Javadoc) * * @see org.jboss.security.acl.ACLProvider#setPersistenceStrategy(org.jboss.security.acl.ACLPersistenceStrategy) */ public void setPersistenceStrategy(ACLPersistenceStrategy persistenceStrategy) { if (persistenceStrategy == null) throw PicketBoxMessages.MESSAGES.invalidNullArgument("persistenceStrategy"); this.strategy = persistenceStrategy; } /* * (non-Javadoc) * * @see org.jboss.security.acl.ACLProvider#isAccessGranted(org.jboss.security.authorization.Resource, * org.jboss.security.identity.Identity, org.jboss.security.acl.ACLPermission) */ public boolean isAccessGranted(Resource resource, Identity identity, ACLPermission permission) throws AuthorizationException { ACL acl = this.retrieveACL(resource); if (acl != null) { ACLEntry entry = acl.getEntry(identity); if (entry != null) { // check the permission associated with the identity. return entry.checkPermission(permission); } // no entry for identity = deny access return false; } else throw new AuthorizationException(PicketBoxMessages.MESSAGES.unableToLocateACLForResourceMessage( resource != null ? resource.toString() : null)); } /** ** Retrieves the ACL that is to be used to perform authorization decisions on the specified resource. If an ACL * for the specified resource can be located by the strategy, this will be the returned ACL. On the other hand, * if no ACL can be located for the resource then the method verifies if the {@code checkParentACL} property has * been set: *
* Loads the specified class using a {@code PrivilegedExceptionAction}. *
* * @param name a {@code String} containing the fully-qualified name of the class to be loaded. * @return a reference to the loaded {@code Class}. * @throws PrivilegedActionException if an error occurs while loading the specified class. */ protected Class> loadClass(final String name) throws PrivilegedActionException { return AccessController.doPrivileged(new PrivilegedExceptionAction