View Javadoc

1   /**
2    * Copyright 2005-2013 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.lookup;
17  
18  import org.kuali.rice.core.api.exception.RiceRuntimeException;
19  import org.kuali.rice.kim.api.KimConstants;
20  import org.kuali.rice.kim.api.identity.Person;
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.uif.view.View;
23  import org.kuali.rice.krad.uif.view.ViewAuthorizerBase;
24  import org.kuali.rice.krad.uif.view.ViewModel;
25  import org.kuali.rice.krad.util.KRADConstants;
26  import org.kuali.rice.krad.util.KRADUtils;
27  import org.kuali.rice.krad.web.form.LookupForm;
28  
29  import java.util.Map;
30  
31  /**
32   * Implementation of {@link org.kuali.rice.krad.uif.view.ViewAuthorizer} for
33   * {@link org.kuali.rice.krad.uif.view.LookupView} instances
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class LookupViewAuthorizerBase extends ViewAuthorizerBase {
38      private static final long serialVersionUID = 3755133641536256283L;
39      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupViewAuthorizerBase.class);
40  
41      /**
42       * Override to check the for permissions of type 'Look Up Records' in addition to the open view check
43       * done in super
44       */
45      @Override
46      public boolean canOpenView(View view, ViewModel model, Person user) {
47          boolean canOpen = super.canOpenView(view, model, user);
48  
49          if (canOpen) {
50              LookupForm lookupForm = (LookupForm) model;
51  
52              Map<String, String> additionalPermissionDetails;
53              try {
54                  additionalPermissionDetails = KRADUtils.getNamespaceAndComponentSimpleName(Class.forName(
55                          lookupForm.getDataObjectClassName()));
56              } catch (ClassNotFoundException e) {
57                  throw new RiceRuntimeException(
58                          "Unable to create class for lookup class name: " + lookupForm.getDataObjectClassName());
59              }
60  
61              if (permissionExistsByTemplate(model, KRADConstants.KNS_NAMESPACE,
62                      KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, additionalPermissionDetails)) {
63                  canOpen = isAuthorizedByTemplate(model, KRADConstants.KNS_NAMESPACE,
64                          KimConstants.PermissionTemplateNames.LOOK_UP_RECORDS, user.getPrincipalId(),
65                          additionalPermissionDetails, null);
66              }
67          }
68  
69          return canOpen;
70      }
71  
72      /**
73       * Check if user is allowed to initiate the document
74       *
75       * @param lookupForm - The lookup form of the document
76       * @param user - user we are authorizing the actions for
77       * @return true if user is authorized to initiate the document, false otherwise
78       */
79      public boolean canInitiateDocument(LookupForm lookupForm, Person user) {
80          boolean canInitiateDocument = false;
81  
82          try {
83              Class<?> dataObjectClass = Class.forName(lookupForm.getDataObjectClassName());
84              // check if creating documents is allowed
85              String documentTypeName = KRADServiceLocatorWeb.getDocumentDictionaryService()
86                      .getMaintenanceDocumentTypeName(dataObjectClass);
87              if ((documentTypeName != null) &&
88                      KRADServiceLocatorWeb.getDocumentDictionaryService().getDocumentAuthorizer(documentTypeName)
89                              .canInitiate(documentTypeName, user)) {
90                  canInitiateDocument = true;
91              }
92          } catch (ClassNotFoundException e) {
93              LOG.warn("Unable to load Data Object Class: " + lookupForm.getDataObjectClassName(), e);
94          }
95  
96          return canInitiateDocument;
97      }
98  }