001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.exception;
017
018import org.kuali.rice.core.api.exception.KualiException;
019import org.kuali.rice.core.api.util.RiceKeyConstants;
020
021import java.util.Collections;
022import java.util.Map;
023
024/**
025 * Represents an exception that is thrown when a given user is not authorized to take the given action on the given
026 * target type
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class AuthorizationException extends KualiException {
031    private static final long serialVersionUID = -3874239711783179351L;
032
033    protected final String userId;
034    protected final String action;
035    protected final String targetType;
036    protected final Map<String, Object> additionalDetails;
037
038    public AuthorizationException(String userId, String action, String targetType) {
039        this(userId, action, targetType, Collections.<String, Object>emptyMap());
040    }
041
042    /**
043     * Constructs a exception with a message from the passed in information.
044     *
045     * @param userId the userid of the user who failed authorization
046     * @param action the action the user was trying to take
047     * @param targetType what the user was trying to take action on
048     * @param additionalDetails additional details about the authorization failure to be passed in and added to the
049     * exception message (ex: permission name, qualifiers, etc.)
050     */
051    public AuthorizationException(String userId, String action, String targetType,
052            Map<String, Object> additionalDetails) {
053        this(userId, action, targetType, "user '" + userId + "' is not authorized to take action '" + action
054                + "' on targets of type '" + targetType + "'"
055                + (additionalDetails != null && !additionalDetails.isEmpty() ?
056                " Additional Details : " + additionalDetails : ""), additionalDetails);
057    }
058
059    /**
060     * Allows you to construct the exception message manually
061     */
062    public AuthorizationException(String userId, String action, String targetType, String message,
063            Map<String, Object> additionalDetails) {
064        super(message);
065
066        this.userId = userId;
067        this.action = action;
068        this.targetType = targetType;
069        this.additionalDetails = additionalDetails;
070    }
071
072    public String getUserId() {
073        return userId;
074    }
075
076    public String getAction() {
077        return action;
078    }
079
080    public String getTargetType() {
081        return targetType;
082    }
083
084    public Map<String, Object> getAdditionalDetails() {
085        return additionalDetails;
086    }
087
088    /**
089     * @return message key used by Struts to select the error message to be displayed
090     * @deprecated
091     */
092    @Deprecated
093    public String getErrorMessageKey() {
094        return RiceKeyConstants.AUTHORIZATION_ERROR_GENERAL;
095    }
096}