View Javadoc

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.method;
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.aopalliance.intercept.MethodInvocation;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.springframework.beans.factory.InitializingBean;
30  
31  import org.springframework.util.Assert;
32  
33  
34  /**
35   * Allows users to determine whether they have "before invocation" privileges for a given method invocation.<p>Of
36   * course, if an {@link org.acegisecurity.AfterInvocationManager} is used to authorize the <em>result</em> of a method
37   * invocation, this class cannot assist determine whether or not the <code>AfterInvocationManager</code> will enable
38   * access. Instead this class aims to allow applications to determine whether or not the current principal would be
39   * allowed to at least attempt to invoke the method, irrespective of the "after" invocation handling.</p>
40   *
41   * @author Ben Alex
42   * @version $Id: MethodInvocationPrivilegeEvaluator.java 1496 2006-05-23 13:38:33Z benalex $
43   */
44  public class MethodInvocationPrivilegeEvaluator implements InitializingBean {
45      //~ Static fields/initializers =====================================================================================
46  
47      protected static final Log logger = LogFactory.getLog(MethodInvocationPrivilegeEvaluator.class);
48  
49      //~ Instance fields ================================================================================================
50  
51      private AbstractSecurityInterceptor securityInterceptor;
52  
53      //~ Methods ========================================================================================================
54  
55      public void afterPropertiesSet() throws Exception {
56          Assert.notNull(securityInterceptor, "SecurityInterceptor required");
57      }
58  
59      public boolean isAllowed(MethodInvocation mi, Authentication authentication) {
60          Assert.notNull(mi, "MethodInvocation required");
61          Assert.notNull(mi.getMethod(), "MethodInvocation must provide a non-null getMethod()");
62  
63          ConfigAttributeDefinition attrs = securityInterceptor.obtainObjectDefinitionSource().getAttributes(mi);
64  
65          if (attrs == null) {
66              if (securityInterceptor.isRejectPublicInvocations()) {
67                  return false;
68              }
69  
70              return true;
71          }
72  
73          if ((authentication == null) || (authentication.getAuthorities() == null)
74              || (authentication.getAuthorities().length == 0)) {
75              return false;
76          }
77  
78          try {
79              securityInterceptor.getAccessDecisionManager().decide(authentication, mi, attrs);
80          } catch (AccessDeniedException unauthorized) {
81              if (logger.isDebugEnabled()) {
82                  logger.debug(mi.toString() + " denied for " + authentication.toString(), unauthorized);
83              }
84  
85              return false;
86          }
87  
88          return true;
89      }
90  
91      public void setSecurityInterceptor(AbstractSecurityInterceptor securityInterceptor) {
92          Assert.notNull(securityInterceptor, "AbstractSecurityInterceptor cannot be null");
93          Assert.isTrue(MethodInvocation.class.equals(securityInterceptor.getSecureObjectClass()),
94              "AbstractSecurityInterceptor does not support MethodInvocations");
95          Assert.notNull(securityInterceptor.getAccessDecisionManager(),
96              "AbstractSecurityInterceptor must provide a non-null AccessDecisionManager");
97          this.securityInterceptor = securityInterceptor;
98      }
99  }