View Javadoc

1   /**
2    * Copyright 2005-2012 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.bo.authorization;
17  
18  import org.kuali.rice.kns.authorization.BusinessObjectAuthorizer;
19  import org.kuali.rice.krad.bo.BusinessObject;
20  import org.kuali.rice.krad.bo.DataObjectAuthorizerBase;
21  import org.kuali.rice.krad.service.DataDictionaryService;
22  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23  import org.kuali.rice.krad.service.KualiModuleService;
24  import org.kuali.rice.krad.service.PersistenceStructureService;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  public class BusinessObjectAuthorizerBase extends DataObjectAuthorizerBase implements BusinessObjectAuthorizer {
31      private static final long serialVersionUID = -6315759348728853851L;
32  
33  	private static KualiModuleService kualiModuleService;
34  	private static DataDictionaryService dataDictionaryService;
35  	private static PersistenceStructureService persistenceStructureService;
36  
37      protected final boolean permissionExistsByTemplate(
38  			BusinessObject businessObject, String namespaceCode,
39  			String permissionTemplateName) {
40  		return getPermissionService()
41  				.isPermissionDefinedByTemplate(namespaceCode, permissionTemplateName, new HashMap<String, String>(
42                          getPermissionDetailValues(businessObject)));
43  	}
44  
45  	protected final boolean permissionExistsByTemplate(
46  			BusinessObject businessObject, String namespaceCode,
47  			String permissionTemplateName, Map<String, String> permissionDetails) {
48  		Map<String, String> combinedPermissionDetails = new HashMap<String, String>(
49  				getPermissionDetailValues(businessObject));
50  		combinedPermissionDetails.putAll(permissionDetails);
51  		return getPermissionService()
52  				.isPermissionDefinedByTemplate(namespaceCode, permissionTemplateName, combinedPermissionDetails);
53  	}
54  
55  	public final boolean isAuthorized(BusinessObject businessObject,
56  			String namespaceCode, String permissionName, String principalId) {
57  		return getPermissionService().isAuthorized(principalId,
58  				namespaceCode, permissionName,
59  				new HashMap<String, String>(getRoleQualification(businessObject, principalId)));
60  	}
61  
62  	public final boolean isAuthorizedByTemplate(BusinessObject dataObject,
63  			String namespaceCode, String permissionTemplateName,
64  			String principalId) {
65  		return getPermissionService().isAuthorizedByTemplate(principalId, namespaceCode, permissionTemplateName,
66                  new HashMap<String, String>(getPermissionDetailValues(dataObject)), new HashMap<String, String>(
67                  getRoleQualification(dataObject, principalId)));
68  	}
69  
70  	public final boolean isAuthorized(BusinessObject businessObject,
71  			String namespaceCode, String permissionName, String principalId,
72  			Map<String, String> collectionOrFieldLevelPermissionDetails,
73  			Map<String, String> collectionOrFieldLevelRoleQualification) {
74  		Map<String, String> roleQualifiers = null;
75  		Map<String, String> permissionDetails = null;
76  		if (collectionOrFieldLevelRoleQualification != null) {
77  			roleQualifiers = new HashMap<String, String>(
78  					getRoleQualification(businessObject, principalId));
79  			roleQualifiers.putAll(collectionOrFieldLevelRoleQualification);
80  		} else {
81  			roleQualifiers = new HashMap<String, String>(
82  					getRoleQualification(businessObject, principalId));
83  		}
84  		/*if (collectionOrFieldLevelPermissionDetails != null) {
85  			permissionDetails = new HashMap<String, String>(
86  					getPermissionDetailValues(businessObject));
87  			permissionDetails.putAll(collectionOrFieldLevelPermissionDetails);
88  		} else {
89  			permissionDetails = new HashMap<String, String>(
90  					getPermissionDetailValues(businessObject));
91  		}*/
92  		
93  		return getPermissionService().isAuthorized(principalId,
94  				namespaceCode, permissionName,
95  				roleQualifiers);
96  	}
97  
98  
99  	/**
100 	 * Returns a role qualification map based off data from the primary business
101 	 * object or the document. DO NOT MODIFY THE MAP RETURNED BY THIS METHOD
102 	 * 
103 	 * @param primaryBusinessObjectOrDocument
104 	 *            the primary business object (i.e. the main BO instance behind
105 	 *            the lookup result row or inquiry) or the document
106 	 * @return a Map containing role qualifications
107 	 */
108 	protected final Map<String, String> getRoleQualification(
109 			BusinessObject primaryBusinessObjectOrDocument) {
110 		return getRoleQualification(primaryBusinessObjectOrDocument, GlobalVariables
111 					.getUserSession().getPerson().getPrincipalId());
112 	}
113 
114 	/**
115 	 * @see org.kuali.rice.kns.authorization.BusinessObjectAuthorizer#getCollectionItemPermissionDetails(org.kuali.rice.krad.bo.BusinessObject)
116 	 */
117     @Override
118 	public Map<String, String> getCollectionItemPermissionDetails(
119 			BusinessObject collectionItemBusinessObject) {
120 		return new HashMap<String, String>();
121 	}
122 
123 	/**
124 	 * @see org.kuali.rice.kns.authorization.BusinessObjectAuthorizer#getCollectionItemRoleQualifications(org.kuali.rice.krad.bo.BusinessObject)
125 	 */
126     @Override
127 	public Map<String, String> getCollectionItemRoleQualifications(
128 			BusinessObject collectionItemBusinessObject) {
129 		return new HashMap<String, String>();
130 	}
131 
132 	protected static KualiModuleService getKualiModuleService() {
133 		if (kualiModuleService == null) {
134 			kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
135 		}
136 		return kualiModuleService;
137 	}
138 
139 	protected static DataDictionaryService getDataDictionaryService() {
140 		if (dataDictionaryService == null) {
141 			dataDictionaryService = KRADServiceLocatorWeb
142 					.getDataDictionaryService();
143 		}
144 		return dataDictionaryService;
145 	}
146 }