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