Coverage Report - org.acegisecurity.intercept.web.WebInvocationPrivilegeEvaluator
 
Classes in this File Line Coverage Branch Coverage Complexity
WebInvocationPrivilegeEvaluator
79% 
80% 
4
 
 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.intercept.web;
 17  
 
 18  
 import org.acegisecurity.AccessDeniedException;
 19  
 import org.acegisecurity.Authentication;
 20  
 import org.acegisecurity.ConfigAttributeDefinition;
 21  
 
 22  
 import org.acegisecurity.intercept.AbstractSecurityInterceptor;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 import org.springframework.beans.factory.InitializingBean;
 28  
 
 29  
 import org.springframework.util.Assert;
 30  
 
 31  
 
 32  
 /**
 33  
  * Allows users to determine whether they have privileges for a given web URI.
 34  
  *
 35  
  * @author Ben Alex
 36  
  * @version $Id: WebInvocationPrivilegeEvaluator.java 1496 2006-05-23 13:38:33Z benalex $
 37  
  */
 38  3
 public class WebInvocationPrivilegeEvaluator implements InitializingBean {
 39  
     //~ Static fields/initializers =====================================================================================
 40  
 
 41  3
     protected static final Log logger = LogFactory.getLog(WebInvocationPrivilegeEvaluator.class);
 42  
 
 43  
     //~ Instance fields ================================================================================================
 44  
 
 45  
     private AbstractSecurityInterceptor securityInterceptor;
 46  
 
 47  
     //~ Methods ========================================================================================================
 48  
 
 49  
     public void afterPropertiesSet() throws Exception {
 50  3
         Assert.notNull(securityInterceptor, "SecurityInterceptor required");
 51  3
     }
 52  
 
 53  
     public boolean isAllowed(FilterInvocation fi, Authentication authentication) {
 54  3
         Assert.notNull(fi, "FilterInvocation required");
 55  
 
 56  3
         ConfigAttributeDefinition attrs = securityInterceptor.obtainObjectDefinitionSource().getAttributes(fi);
 57  
 
 58  3
         if (attrs == null) {
 59  0
             if (securityInterceptor.isRejectPublicInvocations()) {
 60  0
                 return false;
 61  
             }
 62  
 
 63  0
             return true;
 64  
         }
 65  
 
 66  3
         if ((authentication == null) || (authentication.getAuthorities() == null)
 67  
             || (authentication.getAuthorities().length == 0)) {
 68  0
             return false;
 69  
         }
 70  
 
 71  
         try {
 72  3
             securityInterceptor.getAccessDecisionManager().decide(authentication, fi, attrs);
 73  1
         } catch (AccessDeniedException unauthorized) {
 74  1
             if (logger.isDebugEnabled()) {
 75  0
                 logger.debug(fi.toString() + " denied for " + authentication.toString(), unauthorized);
 76  
             }
 77  
 
 78  1
             return false;
 79  2
         }
 80  
 
 81  2
         return true;
 82  
     }
 83  
 
 84  
     public void setSecurityInterceptor(AbstractSecurityInterceptor securityInterceptor) {
 85  3
         Assert.notNull(securityInterceptor, "AbstractSecurityInterceptor cannot be null");
 86  3
         Assert.isTrue(FilterInvocation.class.equals(securityInterceptor.getSecureObjectClass()),
 87  
             "AbstractSecurityInterceptor does not support FilterInvocations");
 88  3
         Assert.notNull(securityInterceptor.getAccessDecisionManager(),
 89  
             "AbstractSecurityInterceptor must provide a non-null AccessDecisionManager");
 90  3
         this.securityInterceptor = securityInterceptor;
 91  3
     }
 92  
 }