View Javadoc
1   /*
2    * Copyright 2009 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.ole.fp.document.authorization;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.businessobject.AccountingLine;
24  import org.kuali.ole.sys.document.AccountingDocument;
25  import org.kuali.ole.sys.document.authorization.AccountingDocumentAuthorizerBase;
26  import org.kuali.ole.sys.identity.OleKimAttributes;
27  import org.kuali.rice.kim.api.KimConstants;
28  import org.kuali.rice.kim.api.identity.Person;
29  import org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase;
30  import org.kuali.rice.krad.document.Document;
31  import org.kuali.rice.krad.util.KRADConstants;
32  
33  /**
34   * The customized document authorizer for the Service Billing document
35   */
36  public class ServiceBillingDocumentAuthorizer extends AccountingDocumentAuthorizerBase {
37      protected static String serviceBillingDocumentTypeName;
38  
39      /**
40       * Overridden to only allow error correction and copy actions if the current user has Modify Accounting Document permission on every accounting line on the document
41       * @see org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentAuthorizerBase#getDocumentActions(org.kuali.rice.kns.document.Document, org.kuali.rice.kim.api.identity.Person, java.util.Set)
42       */
43      @Override
44      public Set<String> getDocumentActions(Document document, Person user, Set<String> documentActionsFromPresentationController) {
45          Set<String> documentActions = super.getDocumentActions(document, user, documentActionsFromPresentationController);
46  
47          boolean canCopyOrErrorCorrect = (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_COPY) || documentActions.contains(OLEConstants.KFS_ACTION_CAN_ERROR_CORRECT)) ? canModifyAllSourceAccountingLines(document, user) : true;
48  
49          if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_COPY)) {
50              if (!canCopyOrErrorCorrect) {
51                  documentActions.remove(KRADConstants.KUALI_ACTION_CAN_COPY);
52              }
53          }
54          if (documentActions.contains(OLEConstants.KFS_ACTION_CAN_ERROR_CORRECT)) {
55              if (!canCopyOrErrorCorrect) {
56                  documentActions.remove(OLEConstants.KFS_ACTION_CAN_ERROR_CORRECT);
57              }
58          }
59          return documentActions;
60      }
61  
62      /**
63       * Determines if the given user has permission to modify all accounting lines on the document
64       * @param document the document with source accounting lines to check
65       * @param user the user to check
66       * @return true if the user can modify all the accounting lines, false otherwise
67       */
68      protected boolean canModifyAllSourceAccountingLines(Document document, Person user) {
69          for (Object accountingLineAsObject : ((AccountingDocument)document).getSourceAccountingLines()) {
70              if (!canModifyAccountingLine(document, ((AccountingLine)accountingLineAsObject), user)) {
71                  return false;
72              }
73          }
74          return true;
75      }
76  
77      /**
78       * Determines if the given user can modify the given accounting line, which is a source line on the given document
79       * @param document a document with source accounting lines
80       * @param accountingLine the accounting line to check the modifyability of
81       * @param user the user being checked
82       * @return true if the user can modify the given accounting line, false otherwise
83       */
84      public boolean canModifyAccountingLine(Document document, AccountingLine accountingLine, Person user) {
85          return isAuthorizedByTemplate(document, OLEConstants.CoreModuleNamespaces.OLE, OLEConstants.PermissionTemplate.MODIFY_ACCOUNTING_LINES.name, user.getPrincipalId(), buildPermissionDetails(document), buildRoleQualifiers(accountingLine));
86      }
87  
88      /**
89       * Builds the permission details map for permission check
90       * @param document the document, which is used to find the real document type name
91       * @return a Map of permissionDetail values
92       */
93      protected Map<String, String> buildPermissionDetails(Document document) {
94          Map<String, String> permissionDetails = new HashMap<String, String>();
95          permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, getDocumentTypeName(document)); // document type name
96          permissionDetails.put(OleKimAttributes.ROUTE_NODE_NAME, DocumentAuthorizerBase.PRE_ROUTING_ROUTE_NAME); // route node = PreRoute
97          permissionDetails.put(KimConstants.AttributeConstants.PROPERTY_NAME, "sourceAccountingLines"); // property = sourceAccountingLines
98          return permissionDetails;
99      }
100 
101     /**
102      * Looks up in the data dictionary the document type name
103      * @param document the document to find a document type name for
104      * @return the document type name
105      */
106     protected String getDocumentTypeName(Document document) {
107         if (serviceBillingDocumentTypeName == null) {
108             serviceBillingDocumentTypeName = getDataDictionaryService().getDocumentTypeNameByClass(document.getClass());
109         }
110         return serviceBillingDocumentTypeName;
111     }
112 
113     /**
114      * Builds a map of role qualifiers, each containing the chart and account of the given accounting line
115      * @param accountingLine the accounting line to build role qualifiers for
116      * @return the Map of role qualifiers
117      */
118     protected Map<String, String> buildRoleQualifiers(AccountingLine accountingLine) {
119         Map<String, String> roleQualifiers = new HashMap<String, String>();
120         roleQualifiers.put(OleKimAttributes.CHART_OF_ACCOUNTS_CODE, accountingLine.getChartOfAccountsCode());
121         roleQualifiers.put(OleKimAttributes.ACCOUNT_NUMBER, accountingLine.getAccountNumber());
122         return roleQualifiers;
123     }
124 }