View Javadoc
1   /*
2    * Copyright 2007-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.coa.document.authorization;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.ole.coa.businessobject.Organization;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.document.authorization.FinancialSystemMaintenanceDocumentAuthorizerBase;
28  import org.kuali.ole.sys.identity.OleKimAttributes;
29  import org.kuali.rice.kim.api.KimConstants;
30  import org.kuali.rice.kim.api.identity.Person;
31  import org.kuali.rice.kim.api.services.IdentityManagementService;
32  import org.kuali.rice.kns.document.MaintenanceDocument;
33  import org.kuali.rice.krad.document.Document;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  import org.kuali.rice.krad.util.KRADConstants;
36  
37  /**
38   * Document Authorizer for the Organization document.
39   */
40  public class OrganizationDocumentAuthorizer extends FinancialSystemMaintenanceDocumentAuthorizerBase {
41      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationDocumentAuthorizer.class);
42      
43      @Override
44      public Set<String> getDocumentActions(Document document, Person user, Set<String> documentActions) {
45          Set<String> myDocumentActions = super.getDocumentActions(document, user, documentActions);
46  
47          if (checkPlantAttributes(document)) {
48              myDocumentActions.remove(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE);
49          }
50  
51          return myDocumentActions;
52      }
53  
54      /**
55       * This checks to see if a user is authorized for plant fields modification. If not then it returns true (without activating
56       * fields). If the org does not have to report to itself then it checks to see if the plant fields have been filled out
57       * correctly and fails if they haven't
58       * 
59       * @return false if user can edit plant fields but they have not been filled out correctly
60       */
61      protected boolean checkPlantAttributes(Document document) {
62          // get user
63          Person user = GlobalVariables.getUserSession().getPerson();
64  
65          // if not authorized to edit plant fields, exit with true
66          if (isPlantAuthorized(user, document) == false) {
67              return true;
68          }
69  
70          return false;
71      }
72  
73      /**
74       * This method tests whether the specified user is part of the group that grants authorization to the Plant fields.
75       * 
76       * @param user - the user to test, document to get plant fund account
77       * @return true if user is part of the group, false otherwise
78       */
79      protected boolean isPlantAuthorized(Person user, Document document) {
80          String principalId = user.getPrincipalId();
81          String namespaceCode = OLEConstants.ParameterNamespaces.KNS;
82          String permissionTemplateName = KimConstants.PermissionTemplateNames.MODIFY_FIELD;
83          
84          Map<String,String> roleQualifiers = new HashMap<String,String>();
85  
86          Map<String,String> permissionDetails = new HashMap<String,String>();
87          permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, Organization.class.getSimpleName());
88          permissionDetails.put(KimConstants.AttributeConstants.PROPERTY_NAME, OLEPropertyConstants.ORGANIZATION_PLANT_ACCOUNT_NUMBER);
89  
90          IdentityManagementService identityManagementService = SpringContext.getBean(IdentityManagementService.class);
91          Boolean isAuthorized = identityManagementService.isAuthorizedByTemplateName(principalId, namespaceCode, permissionTemplateName, permissionDetails, roleQualifiers);
92          if (!isAuthorized) {
93              if (LOG.isDebugEnabled()) {
94                  LOG.debug("User '" + user.getPrincipalName() + "' has no access to the Plant Chart.");
95              }
96          }
97          else {
98              if (LOG.isDebugEnabled()) {
99                  LOG.debug("User '" + user.getPrincipalName() + "' has access to the Plant fields.");
100             }
101         }
102 
103         return isAuthorized;
104     }
105     
106     @SuppressWarnings("unchecked")
107     @Override
108     protected void addRoleQualification(Object dataObject, Map<String, String> attributes) {
109         super.addRoleQualification(dataObject, attributes);
110 
111         if (dataObject instanceof MaintenanceDocument) {
112             MaintenanceDocument maintDoc = (MaintenanceDocument)dataObject;
113             if ( maintDoc.getNewMaintainableObject() != null ) {
114                 Organization newOrg = (Organization) maintDoc.getNewMaintainableObject().getBusinessObject();
115                 if (!StringUtils.isBlank(newOrg.getChartOfAccountsCode())) {
116                     attributes.put(OleKimAttributes.CHART_OF_ACCOUNTS_CODE, newOrg.getChartOfAccountsCode());
117                 }
118             }
119         }
120         else if (dataObject instanceof Organization) {
121             Organization newOrg = (Organization) dataObject;
122             if (!StringUtils.isBlank(newOrg.getChartOfAccountsCode())) {
123                 attributes.put(OleKimAttributes.CHART_OF_ACCOUNTS_CODE, newOrg.getChartOfAccountsCode());
124             }
125         }  
126     } 
127 }