Coverage Report - org.acegisecurity.afterinvocation.BasicAclEntryAfterInvocationProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicAclEntryAfterInvocationProvider
79% 
93% 
2.615
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.afterinvocation;
 17  
 
 18  
 import org.acegisecurity.AccessDeniedException;
 19  
 import org.acegisecurity.AcegiMessageSource;
 20  
 import org.acegisecurity.Authentication;
 21  
 import org.acegisecurity.ConfigAttribute;
 22  
 import org.acegisecurity.ConfigAttributeDefinition;
 23  
 
 24  
 import org.acegisecurity.acl.AclEntry;
 25  
 import org.acegisecurity.acl.AclManager;
 26  
 import org.acegisecurity.acl.basic.BasicAclEntry;
 27  
 import org.acegisecurity.acl.basic.SimpleAclEntry;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 import org.springframework.beans.factory.InitializingBean;
 33  
 
 34  
 import org.springframework.context.MessageSource;
 35  
 import org.springframework.context.MessageSourceAware;
 36  
 import org.springframework.context.i18n.LocaleContextHolder;
 37  
 import org.springframework.context.support.MessageSourceAccessor;
 38  
 
 39  
 import org.springframework.util.Assert;
 40  
 
 41  
 import java.util.Iterator;
 42  
 
 43  
 /**
 44  
  * <p>Given a domain object instance returned from a secure object invocation, ensures the principal has
 45  
  * appropriate permission as defined by the {@link AclManager}.</p>
 46  
  *  <p>The <code>AclManager</code> is used to retrieve the access control list (ACL) permissions associated with a
 47  
  * domain object instance for the current <code>Authentication</code> object. This class is designed to process {@link
 48  
  * AclEntry}s that are subclasses of {@link org.acegisecurity.acl.basic.BasicAclEntry} only. Generally these are
 49  
  * obtained by using the {@link org.acegisecurity.acl.basic.BasicAclProvider}.</p>
 50  
  *  <p>This after invocation provider will fire if any  {@link ConfigAttribute#getAttribute()} matches the {@link
 51  
  * #processConfigAttribute}. The provider will then lookup the ACLs from the <code>AclManager</code> and ensure the
 52  
  * principal is {@link org.acegisecurity.acl.basic.BasicAclEntry#isPermitted(int)} for at least one of the {@link
 53  
  * #requirePermission}s.</p>
 54  
  *  <p>Often users will setup a <code>BasicAclEntryAfterInvocationProvider</code> with a {@link
 55  
  * #processConfigAttribute} of <code>AFTER_ACL_READ</code> and a {@link #requirePermission} of
 56  
  * <code>SimpleAclEntry.READ</code>. These are also the defaults.</p>
 57  
  *  <p>If the principal does not have sufficient permissions, an <code>AccessDeniedException</code> will be thrown.</p>
 58  
  *  <p>The <code>AclManager</code> is allowed to return any implementations of <code>AclEntry</code> it wishes.
 59  
  * However, this provider will only be able to validate against <code>BasicAclEntry</code>s, and thus access will be
 60  
  * denied if no <code>AclEntry</code> is of type <code>BasicAclEntry</code>.</p>
 61  
  *  <p>If the provided <code>returnObject</code> is <code>null</code>, permission will always be granted and
 62  
  * <code>null</code> will be returned.</p>
 63  
  *  <p>All comparisons and prefixes are case sensitive.</p>
 64  
  */
 65  10
 public class BasicAclEntryAfterInvocationProvider implements AfterInvocationProvider, InitializingBean,
 66  
     MessageSourceAware {
 67  
     //~ Static fields/initializers =====================================================================================
 68  
 
 69  3
     protected static final Log logger = LogFactory.getLog(BasicAclEntryAfterInvocationProvider.class);
 70  
 
 71  
     //~ Instance fields ================================================================================================
 72  
 
 73  
     private AclManager aclManager;
 74  10
     private Class processDomainObjectClass = Object.class;
 75  10
     protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
 76  10
     private String processConfigAttribute = "AFTER_ACL_READ";
 77  10
     private int[] requirePermission = {SimpleAclEntry.READ};
 78  
 
 79  
     //~ Methods ========================================================================================================
 80  
 
 81  
     public void afterPropertiesSet() throws Exception {
 82  9
         Assert.notNull(processConfigAttribute, "A processConfigAttribute is mandatory");
 83  8
         Assert.notNull(aclManager, "An aclManager is mandatory");
 84  7
         Assert.notNull(messages, "A message source must be set");
 85  
 
 86  7
         if ((requirePermission == null) || (requirePermission.length == 0)) {
 87  1
             throw new IllegalArgumentException("One or more requirePermission entries is mandatory");
 88  
         }
 89  6
     }
 90  
 
 91  
     public Object decide(Authentication authentication, Object object, ConfigAttributeDefinition config,
 92  
         Object returnedObject) throws AccessDeniedException {
 93  7
         Iterator iter = config.getConfigAttributes();
 94  
 
 95  9
         while (iter.hasNext()) {
 96  8
             ConfigAttribute attr = (ConfigAttribute) iter.next();
 97  
 
 98  8
             if (this.supports(attr)) {
 99  
                 // Need to make an access decision on this invocation
 100  6
                 if (returnedObject == null) {
 101  
                     // AclManager interface contract prohibits nulls
 102  
                     // As they have permission to null/nothing, grant access
 103  1
                     if (logger.isDebugEnabled()) {
 104  0
                         logger.debug("Return object is null, skipping");
 105  
                     }
 106  
 
 107  1
                     return null;
 108  
                 }
 109  
 
 110  5
                 if (!processDomainObjectClass.isAssignableFrom(returnedObject.getClass())) {
 111  0
                     if (logger.isDebugEnabled()) {
 112  0
                         logger.debug("Return object is not applicable for this provider, skipping");
 113  
                     }
 114  
 
 115  0
                     return null;
 116  
                 }
 117  
 
 118  5
                 AclEntry[] acls = aclManager.getAcls(returnedObject, authentication);
 119  
 
 120  5
                 if ((acls == null) || (acls.length == 0)) {
 121  1
                     throw new AccessDeniedException(messages.getMessage(
 122  
                             "BasicAclEntryAfterInvocationProvider.noPermission",
 123  
                             new Object[] {authentication.getName(), returnedObject},
 124  
                             "Authentication {0} has NO permissions at all to the domain object {1}", LocaleContextHolder.getLocale()));
 125  
                 }
 126  
 
 127  7
                 for (int i = 0; i < acls.length; i++) {
 128  
                     // Locate processable AclEntrys
 129  6
                     if (acls[i] instanceof BasicAclEntry) {
 130  5
                         BasicAclEntry processableAcl = (BasicAclEntry) acls[i];
 131  
 
 132  
                         // See if principal has any of the required permissions
 133  7
                         for (int y = 0; y < requirePermission.length; y++) {
 134  5
                             if (processableAcl.isPermitted(requirePermission[y])) {
 135  3
                                 if (logger.isDebugEnabled()) {
 136  0
                                     logger.debug("Principal DOES have permission to return object: " + returnedObject
 137  
                                         + " due to ACL: " + processableAcl.toString());
 138  
                                 }
 139  
 
 140  3
                                 return returnedObject;
 141  
                             }
 142  
                         }
 143  
                     }
 144  
                 }
 145  
 
 146  
                 // No permissions match
 147  1
                 throw new AccessDeniedException(messages.getMessage(
 148  
                         "BasicAclEntryAfterInvocationProvider.insufficientPermission",
 149  
                         new Object[] {authentication.getName(), returnedObject},
 150  
                         "Authentication {0} has ACL permissions to the domain object, "
 151  
                         + "but not the required ACL permission to the domain object {1}", LocaleContextHolder.getLocale()));
 152  
             }
 153  2
         }
 154  
 
 155  1
         return returnedObject;
 156  
     }
 157  
 
 158  
     public AclManager getAclManager() {
 159  1
         return aclManager;
 160  
     }
 161  
 
 162  
     public String getProcessConfigAttribute() {
 163  10
         return processConfigAttribute;
 164  
     }
 165  
 
 166  
     public int[] getRequirePermission() {
 167  2
         return requirePermission;
 168  
     }
 169  
 
 170  
     public void setAclManager(AclManager aclManager) {
 171  8
         this.aclManager = aclManager;
 172  8
     }
 173  
 
 174  
     public void setMessageSource(MessageSource messageSource) {
 175  0
         this.messages = new MessageSourceAccessor(messageSource);
 176  0
     }
 177  
 
 178  
     public void setProcessConfigAttribute(String processConfigAttribute) {
 179  2
         this.processConfigAttribute = processConfigAttribute;
 180  2
     }
 181  
 
 182  
     public void setProcessDomainObjectClass(Class processDomainObjectClass) {
 183  0
         Assert.notNull(processDomainObjectClass, "processDomainObjectClass cannot be set to null");
 184  0
         this.processDomainObjectClass = processDomainObjectClass;
 185  0
     }
 186  
 
 187  
     public void setRequirePermission(int[] requirePermission) {
 188  2
         this.requirePermission = requirePermission;
 189  2
     }
 190  
 
 191  
     /**
 192  
      * Allow setting permissions with String literals instead of integers as {@link #setRequirePermission(int[])}
 193  
      *
 194  
      * @param requiredPermissions Permission literals
 195  
      * @see SimpleAclEntry#parsePermissions(String[]) for valid values
 196  
      */
 197  
     public void setRequirePermissionFromString(String[] requiredPermissions) {
 198  0
         setRequirePermission(SimpleAclEntry.parsePermissions(requiredPermissions));
 199  0
     }
 200  
 
 201  
     public boolean supports(ConfigAttribute attribute) {
 202  8
         if ((attribute.getAttribute() != null) && attribute.getAttribute().equals(getProcessConfigAttribute())) {
 203  6
             return true;
 204  
         } else {
 205  2
             return false;
 206  
         }
 207  
     }
 208  
 
 209  
     /**
 210  
      * This implementation supports any type of class, because it does not query the presented secure object.
 211  
      *
 212  
      * @param clazz the secure object
 213  
      *
 214  
      * @return always <code>true</code>
 215  
      */
 216  
     public boolean supports(Class clazz) {
 217  1
         return true;
 218  
     }
 219  
 }