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  package org.acegisecurity.vote;
16  
17  import org.acegisecurity.AuthorizationServiceException;
18  
19  import org.aopalliance.intercept.MethodInvocation;
20  
21  import org.aspectj.lang.JoinPoint;
22  import org.aspectj.lang.reflect.CodeSignature;
23  
24  import org.springframework.util.Assert;
25  
26  
27  /**
28   * <p>Provides helper methods for writing domain object ACL voters. Is not bound to any particular ACL system.</p>
29   *
30   * @author Ben Alex
31   * @version $Id: AbstractAclVoter.java 1754 2006-11-17 02:01:21Z benalex $
32   */
33  public abstract class AbstractAclVoter implements AccessDecisionVoter {
34      //~ Instance fields ================================================================================================
35  
36      private Class processDomainObjectClass;
37  
38      //~ Methods ========================================================================================================
39  
40      protected Object getDomainObjectInstance(Object secureObject) {
41          Object[] args;
42          Class[] params;
43  
44          if (secureObject instanceof MethodInvocation) {
45              MethodInvocation invocation = (MethodInvocation) secureObject;
46              params = invocation.getMethod().getParameterTypes();
47              args = invocation.getArguments();
48          } else {
49              JoinPoint jp = (JoinPoint) secureObject;
50              params = ((CodeSignature) jp.getStaticPart().getSignature()).getParameterTypes();
51              args = jp.getArgs();
52          }
53  
54          for (int i = 0; i < params.length; i++) {
55              if (processDomainObjectClass.isAssignableFrom(params[i])) {
56                  return args[i];
57              }
58          }
59  
60          throw new AuthorizationServiceException("Secure object: " + secureObject
61              + " did not provide any argument of type: " + processDomainObjectClass);
62      }
63  
64      public Class getProcessDomainObjectClass() {
65          return processDomainObjectClass;
66      }
67  
68      public void setProcessDomainObjectClass(Class processDomainObjectClass) {
69          Assert.notNull(processDomainObjectClass, "processDomainObjectClass cannot be set to null");
70          this.processDomainObjectClass = processDomainObjectClass;
71      }
72  
73      /**
74       * This implementation supports only <code>MethodSecurityInterceptor</code>, because it queries the
75       * presented <code>MethodInvocation</code>.
76       *
77       * @param clazz the secure object
78       *
79       * @return <code>true</code> if the secure object is <code>MethodInvocation</code>, <code>false</code> otherwise
80       */
81      public boolean supports(Class clazz) {
82          if (MethodInvocation.class.isAssignableFrom(clazz)) {
83              return true;
84          } else if (JoinPoint.class.isAssignableFrom(clazz)) {
85              return true;
86          } else {
87              return false;
88          }
89      }
90  }