001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.authz.aop;
020
021import java.util.ArrayList;
022import java.util.Collection;
023
024import org.apache.shiro.aop.MethodInvocation;
025import org.apache.shiro.authz.AuthorizationException;
026
027/**
028 * An <tt>AnnotationsAuthorizingMethodInterceptor</tt> is a MethodInterceptor that asserts a given method is authorized
029 * to execute based on one or more configured <tt>AuthorizingAnnotationMethodInterceptor</tt>s.
030 *
031 * <p>This allows multiple annotations on a method to be processed before the method
032 * executes, and if any of the <tt>AuthorizingAnnotationMethodInterceptor</tt>s indicate that the method should not be
033 * executed, an <tt>AuthorizationException</tt> will be thrown, otherwise the method will be invoked as expected.
034 *
035 * <p>It is essentially a convenience mechanism to allow multiple annotations to be processed in a single method
036 * interceptor.
037 *
038 * @since 0.2
039 */
040public abstract class AnnotationsAuthorizingMethodInterceptor extends AuthorizingMethodInterceptor {
041
042    /**
043     * The method interceptors to execute for the annotated method.
044     */
045    protected Collection<AuthorizingAnnotationMethodInterceptor> methodInterceptors;
046
047    /**
048     * Default no-argument constructor that defaults the 
049     * {@link #methodInterceptors methodInterceptors} attribute to contain two interceptors by default - the
050     * {@link RoleAnnotationMethodInterceptor RoleAnnotationMethodInterceptor} and the
051     * {@link PermissionAnnotationMethodInterceptor PermissionAnnotationMethodInterceptor} to
052     * support role and permission annotations.
053     */
054    public AnnotationsAuthorizingMethodInterceptor() {
055        methodInterceptors = new ArrayList<AuthorizingAnnotationMethodInterceptor>(5);
056        methodInterceptors.add(new RoleAnnotationMethodInterceptor());
057        methodInterceptors.add(new PermissionAnnotationMethodInterceptor());
058        methodInterceptors.add(new AuthenticatedAnnotationMethodInterceptor());
059        methodInterceptors.add(new UserAnnotationMethodInterceptor());
060        methodInterceptors.add(new GuestAnnotationMethodInterceptor());
061    }
062
063    /**
064     * Returns the method interceptors to execute for the annotated method.
065     * <p/>
066     * Unless overridden by the {@link #setMethodInterceptors(java.util.Collection)} method, the default collection
067     * contains a
068     * {@link RoleAnnotationMethodInterceptor RoleAnnotationMethodInterceptor} and a
069     * {@link PermissionAnnotationMethodInterceptor PermissionAnnotationMethodInterceptor} to
070     * support role and permission annotations automatically.
071     * @return the method interceptors to execute for the annotated method.
072     */
073    public Collection<AuthorizingAnnotationMethodInterceptor> getMethodInterceptors() {
074        return methodInterceptors;
075    }
076
077    /**
078     * Sets the method interceptors to execute for the annotated method.
079     * @param methodInterceptors the method interceptors to execute for the annotated method.
080     * @see #getMethodInterceptors()
081     */
082    public void setMethodInterceptors(Collection<AuthorizingAnnotationMethodInterceptor> methodInterceptors) {
083        this.methodInterceptors = methodInterceptors;
084    }
085
086    /**
087     * Iterates over the internal {@link #getMethodInterceptors() methodInterceptors} collection, and for each one,
088     * ensures that if the interceptor
089     * {@link AuthorizingAnnotationMethodInterceptor#supports(org.apache.shiro.aop.MethodInvocation) supports}
090     * the invocation, that the interceptor
091     * {@link AuthorizingAnnotationMethodInterceptor#assertAuthorized(org.apache.shiro.aop.MethodInvocation) asserts}
092     * that the invocation is authorized to proceed.
093     */
094    protected void assertAuthorized(MethodInvocation methodInvocation) throws AuthorizationException {
095        //default implementation just ensures no deny votes are cast:
096        Collection<AuthorizingAnnotationMethodInterceptor> aamis = getMethodInterceptors();
097        if (aamis != null && !aamis.isEmpty()) {
098            for (AuthorizingAnnotationMethodInterceptor aami : aamis) {
099                if (aami.supports(methodInvocation)) {
100                    aami.assertAuthorized(methodInvocation);
101                }
102            }
103        }
104    }
105}