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.document.authorization;
17  
18  
19  import org.kuali.rice.kim.api.KimConstants;
20  import org.kuali.rice.kim.api.identity.Person;
21  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
22  import org.kuali.rice.krad.service.DocumentDictionaryService;
23  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24  import org.kuali.rice.krad.util.KRADConstants;
25  import org.kuali.rice.krad.util.KRADUtils;
26  
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.Map;
30  import java.util.Set;
31  
32  public class MaintenanceDocumentAuthorizerBase extends DocumentAuthorizerBase implements MaintenanceDocumentAuthorizer {
33  	// private static final org.apache.log4j.Logger LOG =
34  	// org.apache.log4j.Logger.getLogger(MaintenanceDocumentAuthorizerBase.class);
35  
36  	transient protected static DocumentDictionaryService documentDictionaryService;
37  
38  	public final boolean canCreate(Class boClass, Person user) {
39  		Map<String, String> permissionDetails = new HashMap<String, String>();
40  		permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME,
41  				getDocumentDictionaryService().getMaintenanceDocumentTypeName(
42  						boClass));
43  		permissionDetails.put(KRADConstants.MAINTENANCE_ACTN,
44  				KRADConstants.MAINTENANCE_NEW_ACTION);
45  		return !permissionExistsByTemplate(KRADConstants.KNS_NAMESPACE,
46  				KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS,
47  				permissionDetails)
48  				|| getPermissionService()
49  						.isAuthorizedByTemplate(user.getPrincipalId(), KRADConstants.KNS_NAMESPACE,
50                                  KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS, permissionDetails,
51                                  new HashMap<String, String>());
52  	}
53  
54  	public final boolean canMaintain(Object dataObject, Person user) {
55  		Map<String, String> permissionDetails = new HashMap<String, String>(2);
56  		permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME,
57  				getDocumentDictionaryService().getMaintenanceDocumentTypeName(
58  						dataObject.getClass()));
59  		permissionDetails.put(KRADConstants.MAINTENANCE_ACTN,
60  				KRADConstants.MAINTENANCE_EDIT_ACTION);
61  		return !permissionExistsByTemplate(KRADConstants.KNS_NAMESPACE,
62  				KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS,
63  				permissionDetails)
64  				|| isAuthorizedByTemplate(
65  						dataObject,
66  						KRADConstants.KNS_NAMESPACE,
67  						KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS,
68  						user.getPrincipalId(), permissionDetails, null);
69  	}
70  
71  	public final boolean canCreateOrMaintain(
72  			MaintenanceDocument maintenanceDocument, Person user) {
73  		return !permissionExistsByTemplate(maintenanceDocument,
74  				KRADConstants.KNS_NAMESPACE,
75  				KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS)
76  				|| isAuthorizedByTemplate(
77  						maintenanceDocument,
78  						KRADConstants.KNS_NAMESPACE,
79  						KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS,
80  						user.getPrincipalId());
81  	}
82  
83  	public Set<String> getSecurePotentiallyHiddenSectionIds() {
84  		return new HashSet<String>();
85  	}
86  
87  	public Set<String> getSecurePotentiallyReadOnlySectionIds() {
88  		return new HashSet<String>();
89  	}
90  
91  	@SuppressWarnings("unchecked")
92  	@Override
93  	protected void addRoleQualification(Object dataObject, Map<String, String> attributes) {
94  		super.addRoleQualification(dataObject, attributes);
95  		if (dataObject instanceof MaintenanceDocument) {
96  			MaintenanceDocument maintDoc = (MaintenanceDocument)dataObject;
97  			if ( maintDoc.getNewMaintainableObject() != null ) {			
98  				attributes.putAll(
99                          KRADUtils.getNamespaceAndComponentSimpleName(maintDoc.getNewMaintainableObject().getDataObjectClass()));
100 			}
101 		}
102 	}
103 
104 	@SuppressWarnings("unchecked")
105 	@Override
106 	protected void addPermissionDetails(Object dataObject, Map<String, String> attributes) {
107 		super.addPermissionDetails(dataObject, attributes);
108 		if (dataObject instanceof MaintenanceDocument) {
109 			MaintenanceDocument maintDoc = (MaintenanceDocument)dataObject;
110 			if ( maintDoc.getNewMaintainableObject() != null ) {			
111 				attributes.putAll(
112                         KRADUtils.getNamespaceAndComponentSimpleName(maintDoc.getNewMaintainableObject().getDataObjectClass()));
113 				attributes.put(KRADConstants.MAINTENANCE_ACTN,maintDoc.getNewMaintainableObject().getMaintenanceAction());
114 			}
115 		}
116 	}
117 
118     protected static DocumentDictionaryService getDocumentDictionaryService() {
119         if (documentDictionaryService == null) {
120             documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
121         }
122         return documentDictionaryService;
123     }
124 
125 }