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 org.apache.shiro.authz.AuthorizationException;
022import org.apache.shiro.authz.annotation.Logical;
023import org.apache.shiro.authz.annotation.RequiresRoles;
024
025import java.lang.annotation.Annotation;
026import java.util.Arrays;
027
028/**
029 * Checks to see if a @{@link org.apache.shiro.authz.annotation.RequiresRoles RequiresRoles} annotation is declared,
030 * and if so, performs a role check to see if the calling <code>Subject</code> is allowed to proceed.
031 *
032 * @since 0.9.0
033 */
034public class RoleAnnotationHandler extends AuthorizingAnnotationHandler {
035
036    /**
037     * Default no-argument constructor that ensures this handler looks for
038     * {@link org.apache.shiro.authz.annotation.RequiresRoles RequiresRoles} annotations.
039     */
040    public RoleAnnotationHandler() {
041        super(RequiresRoles.class);
042    }
043
044    /**
045     * Ensures that the calling <code>Subject</code> has the Annotation's specified roles, and if not, throws an
046     * <code>AuthorizingException</code> indicating that access is denied.
047     *
048     * @param a the RequiresRoles annotation to use to check for one or more roles
049     * @throws org.apache.shiro.authz.AuthorizationException if the calling <code>Subject</code> does not have the role(s)
050     *                                                       necessary to proceed.
051     */
052    public void assertAuthorized(Annotation a) throws AuthorizationException {
053        if (!(a instanceof RequiresRoles)) {
054            return;
055        }
056
057        RequiresRoles rrAnnotation = (RequiresRoles) a;
058        String[] roles = rrAnnotation.value();
059
060        if (roles.length == 1) {
061            getSubject().checkRole(roles[0]);
062            return;
063        }
064        if (Logical.AND.equals(rrAnnotation.logical())) {
065            getSubject().checkRoles(Arrays.asList(roles));
066            return;
067        }
068        if (Logical.OR.equals(rrAnnotation.logical())) {
069            // Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
070            boolean hasAtLeastOneRole = false;
071            for (String role : roles) {
072                if (getSubject().hasRole(role)) {
073                    hasAtLeastOneRole = true;
074                }
075            }
076            // Cause the exception if none of the role match, note that the exception message will be a bit misleading
077            if (!hasAtLeastOneRole) {
078                getSubject().checkRole(roles[0]);
079            }
080        }
081    }
082
083}