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, and if so, performs
030 * 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
050     *          if the calling <code>Subject</code> does not have the role(s) necessary to
051     *          proceed.
052     */
053    public void assertAuthorized(Annotation a) throws AuthorizationException {
054        if (!(a instanceof RequiresRoles)) return;
055
056        RequiresRoles rrAnnotation = (RequiresRoles) a;
057        String[] roles = rrAnnotation.value();
058
059        if (roles.length == 1) {
060            getSubject().checkRole(roles[0]);
061            return;
062        }
063        if (Logical.AND.equals(rrAnnotation.logical())) {
064            getSubject().checkRoles(Arrays.asList(roles));
065            return;
066        }
067        if (Logical.OR.equals(rrAnnotation.logical())) {
068            // Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
069            boolean hasAtLeastOneRole = false;
070            for (String role : roles) if (getSubject().hasRole(role)) hasAtLeastOneRole = true;
071            // Cause the exception if none of the role match, note that the exception message will be a bit misleading
072            if (!hasAtLeastOneRole) getSubject().checkRole(roles[0]);
073        }
074    }
075
076}