View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.exception;
17  
18  import java.util.Collections;
19  import java.util.Map;
20  
21  import org.kuali.rice.kns.util.RiceKeyConstants;
22  
23  /**
24   * This class represents an exception that is thrown when a given user is not authorized to take the given action on the given
25   * target type.
26   */
27  public class AuthorizationException extends KualiException{
28      private static final long serialVersionUID = -3874239711783179351L;
29      protected final String userId;
30      protected final String action;
31      protected final String targetType;
32      protected final Map<String, Object> additionalDetails;
33  
34      public AuthorizationException(String userId, String action, String targetType) {
35          this(userId, action, targetType, Collections.<String, Object>emptyMap());
36      }
37      
38      /**
39       * Constructs a exception with a message from the passed in information.
40       * 
41       * @param userId the userid of the user who failed authorization
42       * @param action the action the user was trying to take
43       * @param targetType what the user was trying to take action on
44       * @param additionalDetails additional details about the authorization failure to be passed in and added to the exception message (ex: permission name, qualifiers, etc.)
45       */
46      public AuthorizationException(String userId, String action, String targetType, Map<String, Object> additionalDetails) {
47          this(userId, action, targetType,
48          		"user '" + userId + "' is not authorized to take action '" + action + "' on targets of type '" + targetType + "'" + (additionalDetails != null && !additionalDetails.isEmpty() ? " Additional Details : " + additionalDetails : ""),
49          		additionalDetails);
50      }
51  
52      /** allows you to construct the exception message manually. */
53      public AuthorizationException(String userId, String action, String targetType, String message, Map<String, Object> additionalDetails) {
54          super(message);
55  
56          this.userId = userId;
57          this.action = action;
58          this.targetType = targetType;
59          this.additionalDetails = additionalDetails;
60      }
61  
62      public Map<String, Object> getAdditionalDetails() {
63          return additionalDetails;
64      }
65  
66      public String getUserId() {
67          return userId;
68      }
69  
70      public String getAction() {
71          return action;
72      }
73  
74      public String getTargetType() {
75          return targetType;
76      }
77  
78      /**
79       * @return message key used by Struts to select the error message to be displayed
80       */
81      public String getErrorMessageKey() {
82          return RiceKeyConstants.AUTHORIZATION_ERROR_GENERAL;
83      }
84  }