View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.sec.document.validation.impl;
17  
18  import org.kuali.ole.sec.SecKeyConstants;
19  import org.kuali.ole.sec.businessobject.AccessSecurityRestrictionInfo;
20  import org.kuali.ole.sec.service.AccessSecurityService;
21  import org.kuali.ole.sys.businessobject.AccountingLine;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.ole.sys.document.AccountingDocument;
24  import org.kuali.ole.sys.document.validation.event.AccountingLineEvent;
25  import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
26  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27  import org.kuali.ole.sys.document.validation.event.UpdateAccountingLineEvent;
28  import org.kuali.ole.sys.document.validation.impl.AccountingRuleEngineRuleBase;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  
32  /**
33   * Hooks into rules to make access security checks for accounting documents
34   */
35  public class AccessSecurityAccountingDocumentRuleBase extends AccountingRuleEngineRuleBase {
36  
37      /**
38       * For add or update accounting line events checks the given user has access permissions for the line
39       * 
40       * @see org.kuali.ole.sys.document.validation.impl.AccountingRuleEngineRuleBase#validateForEvent(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
41       */
42      @Override
43      public boolean validateForEvent(AttributedDocumentEvent event) {
44          boolean isValid = super.validateForEvent(event);
45  
46          if (isValid && (event instanceof AddAccountingLineEvent || event instanceof UpdateAccountingLineEvent)) {
47              AccountingLineEvent accountingLineEvent = (AccountingLineEvent) event;
48              isValid = checkEditAccessForAccountingLine((AccountingDocument) accountingLineEvent.getDocument(), accountingLineEvent.getAccountingLine());
49          }
50  
51          return isValid;
52      }
53  
54      /**
55       * Calls AccessSecurityService to check access edit permissions on accounting line for the current user
56       * 
57       * @param document AccountingDocument containing the line to check
58       * @param line AccountingLine to check access on
59       * @return boolean true if user is allowed to edit the accounting line, false if the user is not allowed to
60       */
61      protected boolean checkEditAccessForAccountingLine(AccountingDocument document, AccountingLine line) {
62          boolean editAccessAllowed = true;
63  
64          AccessSecurityRestrictionInfo restrictionInfo = new AccessSecurityRestrictionInfo();
65          boolean hasEditAccessPermission = getAccessSecurityService().canEditDocumentAccountingLine(document, line, GlobalVariables.getUserSession().getPerson(), restrictionInfo);
66  
67          if (!hasEditAccessPermission) {
68              GlobalVariables.getMessageMap().putError(restrictionInfo.getPropertyName(), SecKeyConstants.ERROR_ACCOUNTING_LINE_ADD_OR_UPDATE, restrictionInfo.getPropertyLabel(), restrictionInfo.getRetrictedValue());
69              editAccessAllowed = false;
70          }
71  
72          return editAccessAllowed;
73      }
74      private static AccessSecurityService accessSecurityService;
75      protected AccessSecurityService getAccessSecurityService() {
76          if ( accessSecurityService == null ) {
77              accessSecurityService = SpringContext.getBean(AccessSecurityService.class);
78          }
79          return accessSecurityService;
80      }
81  }