Coverage Report - org.kuali.rice.kns.service.impl.BusinessObjectAuthorizationServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
BusinessObjectAuthorizationServiceImpl
0%
0/219
0%
0/124
3.607
 
 1  
 /*
 2  
  * Copyright 2007-2008 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.kns.service.impl;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.beanutils.PropertyUtils;
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 27  
 import org.kuali.rice.core.xml.dto.AttributeSet;
 28  
 import org.kuali.rice.kim.bo.Person;
 29  
 import org.kuali.rice.kim.bo.impl.KimAttributes;
 30  
 import org.kuali.rice.kim.service.IdentityManagementService;
 31  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 32  
 import org.kuali.rice.kim.util.KimConstants;
 33  
 import org.kuali.rice.kns.authorization.BusinessObjectAuthorizer;
 34  
 import org.kuali.rice.kns.authorization.BusinessObjectRestrictions;
 35  
 import org.kuali.rice.kns.authorization.BusinessObjectRestrictionsBase;
 36  
 import org.kuali.rice.kns.authorization.InquiryOrMaintenanceDocumentAuthorizer;
 37  
 import org.kuali.rice.kns.authorization.InquiryOrMaintenanceDocumentPresentationController;
 38  
 import org.kuali.rice.kns.authorization.InquiryOrMaintenanceDocumentRestrictions;
 39  
 import org.kuali.rice.kns.authorization.InquiryOrMaintenanceDocumentRestrictionsBase;
 40  
 import org.kuali.rice.kns.bo.BusinessObject;
 41  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 42  
 import org.kuali.rice.kns.datadictionary.AttributeSecurity;
 43  
 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
 44  
 import org.kuali.rice.kns.datadictionary.DataObjectEntry;
 45  
 import org.kuali.rice.kns.datadictionary.FieldDefinition;
 46  
 import org.kuali.rice.kns.datadictionary.InquiryCollectionDefinition;
 47  
 import org.kuali.rice.kns.datadictionary.InquirySectionDefinition;
 48  
 import org.kuali.rice.kns.datadictionary.MaintainableCollectionDefinition;
 49  
 import org.kuali.rice.kns.datadictionary.MaintainableItemDefinition;
 50  
 import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
 51  
 import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
 52  
 import org.kuali.rice.kns.document.Document;
 53  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 54  
 import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
 55  
 import org.kuali.rice.kns.document.authorization.DocumentPresentationController;
 56  
 import org.kuali.rice.kns.document.authorization.MaintenanceDocumentAuthorizer;
 57  
 import org.kuali.rice.kns.document.authorization.MaintenanceDocumentPresentationController;
 58  
 import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions;
 59  
 import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictionsBase;
 60  
 import org.kuali.rice.kns.inquiry.InquiryAuthorizer;
 61  
 import org.kuali.rice.kns.inquiry.InquiryPresentationController;
 62  
 import org.kuali.rice.kns.inquiry.InquiryRestrictions;
 63  
 import org.kuali.rice.kns.service.BusinessObjectAuthorizationService;
 64  
 import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
 65  
 import org.kuali.rice.kns.service.DataDictionaryService;
 66  
 import org.kuali.rice.kns.service.DocumentHelperService;
 67  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 68  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 69  
 import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
 70  
 import org.kuali.rice.kns.util.KNSConstants;
 71  
 import org.kuali.rice.kns.util.KNSUtils;
 72  
 import org.kuali.rice.kns.util.ObjectUtils;
 73  
 
 74  0
 public class BusinessObjectAuthorizationServiceImpl implements
 75  
                 BusinessObjectAuthorizationService {
 76  
         private DataDictionaryService dataDictionaryService;
 77  
         private IdentityManagementService identityManagementService;
 78  
         private BusinessObjectDictionaryService businessObjectDictionaryService;
 79  
         private DocumentHelperService documentHelperService;
 80  
         private MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService;
 81  
         private ConfigurationService kualiConfigurationService;
 82  
         
 83  
         public BusinessObjectRestrictions getLookupResultRestrictions(
 84  
                         Object dataObject, Person user) {
 85  0
                 BusinessObjectRestrictions businessObjectRestrictions = new BusinessObjectRestrictionsBase();
 86  0
                 considerBusinessObjectFieldUnmaskAuthorization(dataObject, user,
 87  
                                 businessObjectRestrictions, "", null);
 88  0
                 return businessObjectRestrictions;
 89  
         }
 90  
 
 91  
         public InquiryRestrictions getInquiryRestrictions(
 92  
                         BusinessObject businessObject, Person user) {
 93  0
                 InquiryRestrictions inquiryRestrictions = new InquiryOrMaintenanceDocumentRestrictionsBase();
 94  0
                 BusinessObjectEntry businessObjectEntry = getDataDictionaryService()
 95  
                                 .getDataDictionary().getBusinessObjectEntry(
 96  
                                                 businessObject.getClass().getName());
 97  0
                 InquiryPresentationController inquiryPresentationController = getBusinessObjectDictionaryService()
 98  
                                 .getInquiryPresentationController(businessObject.getClass());
 99  0
                 InquiryAuthorizer inquiryAuthorizer = getBusinessObjectDictionaryService()
 100  
                                 .getInquiryAuthorizer(businessObject.getClass());
 101  0
                 considerBusinessObjectFieldUnmaskAuthorization(businessObject, user,
 102  
                                 inquiryRestrictions, "", null);
 103  0
                 considerBusinessObjectFieldViewAuthorization(businessObjectEntry,
 104  
                                 businessObject, null, user, inquiryAuthorizer, inquiryRestrictions,
 105  
                                 "");
 106  0
                 considerInquiryOrMaintenanceDocumentPresentationController(
 107  
                                 inquiryPresentationController, businessObject,
 108  
                                 inquiryRestrictions);
 109  0
                 considerInquiryOrMaintenanceDocumentAuthorizer(inquiryAuthorizer,
 110  
                                 businessObject, user, inquiryRestrictions);
 111  0
                 for (InquirySectionDefinition inquirySectionDefinition : businessObjectEntry.getInquiryDefinition().getInquirySections()) {
 112  0
                         if (inquirySectionDefinition.getInquiryCollections() != null) {
 113  0
                                 addInquirableItemRestrictions(inquirySectionDefinition.getInquiryCollections().values(), inquiryAuthorizer, 
 114  
                                                 inquiryRestrictions, businessObject, businessObject, "", user);
 115  
                         }
 116  
                         // Collections may also be stored in the inquiry fields, so we need to parse through that
 117  0
                         List<FieldDefinition> inquiryFields = inquirySectionDefinition.getInquiryFields();
 118  0
                         if (inquiryFields != null) {
 119  0
                                 for (FieldDefinition fieldDefinition : inquiryFields) {
 120  0
                                         addInquirableItemRestrictions(inquiryFields, inquiryAuthorizer, 
 121  
                                                         inquiryRestrictions, businessObject, businessObject, "", user);
 122  
                                 }
 123  
                         }
 124  0
                 }
 125  
                 
 126  0
                 return inquiryRestrictions;
 127  
         }
 128  
 
 129  
         public MaintenanceDocumentRestrictions getMaintenanceDocumentRestrictions(
 130  
                         MaintenanceDocument maintenanceDocument, Person user) {
 131  
 
 132  0
                 MaintenanceDocumentRestrictions maintenanceDocumentRestrictions = new MaintenanceDocumentRestrictionsBase();
 133  0
                 DataObjectEntry dataObjectEntry = getDataDictionaryService()
 134  
                                 .getDataDictionary().getDataObjectEntry(
 135  
                                                 maintenanceDocument.getNewMaintainableObject()
 136  
                                                                 .getDataObject().getClass().getName());
 137  0
                 MaintenanceDocumentPresentationController maintenanceDocumentPresentationController = (MaintenanceDocumentPresentationController) getDocumentHelperService()
 138  
                                 .getDocumentPresentationController(maintenanceDocument);
 139  0
                 MaintenanceDocumentAuthorizer maintenanceDocumentAuthorizer = (MaintenanceDocumentAuthorizer) getDocumentHelperService()
 140  
                                 .getDocumentAuthorizer(maintenanceDocument);
 141  0
                 considerBusinessObjectFieldUnmaskAuthorization(maintenanceDocument
 142  
                                 .getNewMaintainableObject().getDataObject(), user,
 143  
                                 maintenanceDocumentRestrictions, "", maintenanceDocument );
 144  0
                 considerBusinessObjectFieldViewAuthorization(dataObjectEntry,
 145  
                                 maintenanceDocument.getNewMaintainableObject().getDataObject(),
 146  
                                 null, user, maintenanceDocumentAuthorizer,
 147  
                                 maintenanceDocumentRestrictions, "");
 148  0
                 considerBusinessObjectFieldModifyAuthorization(dataObjectEntry,
 149  
                                 maintenanceDocument.getNewMaintainableObject().getDataObject(),
 150  
                                 null, user, maintenanceDocumentAuthorizer,
 151  
                                 maintenanceDocumentRestrictions, "");
 152  0
                 considerCustomButtonFieldAuthorization(dataObjectEntry,
 153  
                                 maintenanceDocument.getNewMaintainableObject().getDataObject(),
 154  
                                 null, user, maintenanceDocumentAuthorizer,
 155  
                                 maintenanceDocumentRestrictions, "");
 156  0
                 considerInquiryOrMaintenanceDocumentPresentationController(
 157  
                                 maintenanceDocumentPresentationController, maintenanceDocument,
 158  
                                 maintenanceDocumentRestrictions);
 159  0
                 considerInquiryOrMaintenanceDocumentAuthorizer(
 160  
                                 maintenanceDocumentAuthorizer, maintenanceDocument, user,
 161  
                                 maintenanceDocumentRestrictions);
 162  0
                 considerMaintenanceDocumentPresentationController(
 163  
                                 maintenanceDocumentPresentationController, maintenanceDocument,
 164  
                                 maintenanceDocumentRestrictions);
 165  0
                 considerMaintenanceDocumentAuthorizer(maintenanceDocumentAuthorizer,
 166  
                                 maintenanceDocument, user, maintenanceDocumentRestrictions);
 167  
                 
 168  0
                 MaintenanceDocumentEntry maintenanceDocumentEntry = getMaintenanceDocumentDictionaryService().getMaintenanceDocumentEntry(maintenanceDocument
 169  
                                 .getDocumentHeader().getWorkflowDocument().getDocumentType());
 170  0
                 for (MaintainableSectionDefinition maintainableSectionDefinition : maintenanceDocumentEntry.getMaintainableSections()) {
 171  0
                         addMaintainableItemRestrictions(maintainableSectionDefinition.getMaintainableItems(), maintenanceDocumentAuthorizer, maintenanceDocumentRestrictions,
 172  
                                         maintenanceDocument, maintenanceDocument.getNewMaintainableObject().getBusinessObject(), "", user);
 173  
                 }
 174  0
                 return maintenanceDocumentRestrictions;
 175  
         }
 176  
 
 177  
         protected void considerBusinessObjectFieldUnmaskAuthorization(Object dataObject, Person user, BusinessObjectRestrictions businessObjectRestrictions, String propertyPrefix, Document document) {
 178  0
                 DataObjectEntry objectEntry = getDataDictionaryService().getDataDictionary().getDataObjectEntry(dataObject.getClass().getName());
 179  0
                 for (String attributeName : objectEntry.getAttributeNames()) {
 180  0
                         AttributeDefinition attributeDefinition = objectEntry.getAttributeDefinition(attributeName);
 181  0
                         if (attributeDefinition.getAttributeSecurity() != null) {
 182  0
                                 if (attributeDefinition.getAttributeSecurity().isMask() && 
 183  
                                                 !canFullyUnmaskField(user, dataObject.getClass(), attributeName, document)) {
 184  0
                                         businessObjectRestrictions.addFullyMaskedField(propertyPrefix + attributeName, attributeDefinition.getAttributeSecurity().getMaskFormatter());
 185  
                                 }
 186  0
                                 if (attributeDefinition.getAttributeSecurity().isPartialMask() && 
 187  
                                                 !canPartiallyUnmaskField(user, dataObject.getClass(), attributeName, document)) {
 188  0
                                         businessObjectRestrictions.addPartiallyMaskedField(propertyPrefix + attributeName, attributeDefinition.getAttributeSecurity().getPartialMaskFormatter());
 189  
                                 }
 190  
                         }
 191  0
                 }
 192  0
         }
 193  
 
 194  
         /**
 195  
          * @param dataObjectEntry if collectionItemBusinessObject is not null, then it is the DD entry for collectionItemBusinessObject.
 196  
          * Otherwise, it is the entry for primaryBusinessObject
 197  
          * @param primaryDataObject the top-level BO that is being inquiried or maintained
 198  
          * @param collectionItemBusinessObject an element of a collection under the primaryBusinessObject that we are evaluating view auths for
 199  
          * @param user the logged in user
 200  
          * @param businessObjectAuthorizer
 201  
          * @param inquiryOrMaintenanceDocumentRestrictions
 202  
          * @param propertyPrefix
 203  
          */
 204  
         protected void considerBusinessObjectFieldViewAuthorization(
 205  
                         DataObjectEntry dataObjectEntry,
 206  
                         Object primaryDataObject,
 207  
                         BusinessObject collectionItemBusinessObject,
 208  
                         Person user,
 209  
                         BusinessObjectAuthorizer businessObjectAuthorizer,
 210  
                         InquiryOrMaintenanceDocumentRestrictions inquiryOrMaintenanceDocumentRestrictions,
 211  
                         String propertyPrefix) {
 212  0
                 for (String attributeName : dataObjectEntry.getAttributeNames()) {
 213  0
                         AttributeDefinition attributeDefinition = dataObjectEntry
 214  
                                         .getAttributeDefinition(attributeName);
 215  0
                         if (attributeDefinition.getAttributeSecurity() != null) {
 216  0
                                 if (attributeDefinition.getAttributeSecurity().isHide()) {
 217  0
                                         AttributeSet collectionItemPermissionDetails = new AttributeSet();
 218  0
                                         AttributeSet collectionItemRoleQualifications = null;
 219  0
                                         if (ObjectUtils.isNotNull(collectionItemBusinessObject)) {
 220  0
                                                 collectionItemPermissionDetails.putAll(getFieldPermissionDetails(collectionItemBusinessObject, attributeName));
 221  0
                                                 collectionItemPermissionDetails.putAll(businessObjectAuthorizer.
 222  
                                                                 getCollectionItemPermissionDetails(collectionItemBusinessObject));
 223  0
                                                 collectionItemRoleQualifications = new AttributeSet(businessObjectAuthorizer.
 224  
                                                                 getCollectionItemRoleQualifications(collectionItemBusinessObject));
 225  
                                         }
 226  
                                         else {
 227  0
                                                 collectionItemPermissionDetails.putAll(getFieldPermissionDetails(primaryDataObject, attributeName));
 228  
                                         }
 229  0
                                         if (!businessObjectAuthorizer
 230  
                                                         .isAuthorizedByTemplate(
 231  
                                                                         primaryDataObject,
 232  
                                                                         KNSConstants.KNS_NAMESPACE,
 233  
                                                                         KimConstants.PermissionTemplateNames.VIEW_FIELD,
 234  
                                                                         user.getPrincipalId(),
 235  
                                                                         collectionItemPermissionDetails,
 236  
                                                                         collectionItemRoleQualifications)) {
 237  0
                                                 inquiryOrMaintenanceDocumentRestrictions
 238  
                                                                 .addHiddenField(propertyPrefix + attributeName);
 239  
                                         }
 240  
                                 }
 241  
                         }
 242  0
                 }
 243  0
         }
 244  
 
 245  
         /**
 246  
          * @param dataObjectEntry if collectionItemBusinessObject is not null, then it is the DD entry for collectionItemBusinessObject.
 247  
          * Otherwise, it is the entry for primaryBusinessObject
 248  
          * @param primaryDataObject the top-level BO that is being inquiried or maintained
 249  
          * @param collectionItemBusinessObject an element of a collection under the primaryBusinessObject that we are evaluating view auths for
 250  
          * @param user the logged in user
 251  
          * @param businessObjectAuthorizer
 252  
          * @param inquiryOrMaintenanceDocumentRestrictions
 253  
          * @param propertyPrefix
 254  
          */
 255  
         protected void considerBusinessObjectFieldModifyAuthorization(
 256  
                         DataObjectEntry dataObjectEntry,
 257  
                         Object primaryDataObject,
 258  
                         BusinessObject collectionItemBusinessObject, Person user,
 259  
                         BusinessObjectAuthorizer businessObjectAuthorizer,
 260  
                         MaintenanceDocumentRestrictions maintenanceDocumentRestrictions,
 261  
                         String propertyPrefix) {
 262  0
                 for (String attributeName : dataObjectEntry.getAttributeNames()) {
 263  0
                         AttributeDefinition attributeDefinition = dataObjectEntry
 264  
                                         .getAttributeDefinition(attributeName);
 265  0
                         if (attributeDefinition.getAttributeSecurity() != null) {
 266  0
                                 AttributeSet collectionItemPermissionDetails = new AttributeSet();
 267  0
                                 AttributeSet collectionItemRoleQualifications = null;
 268  0
                                 if (ObjectUtils.isNotNull(collectionItemBusinessObject)) {
 269  0
                                         collectionItemPermissionDetails.putAll(getFieldPermissionDetails(collectionItemBusinessObject, attributeName));
 270  0
                                         collectionItemPermissionDetails.putAll(businessObjectAuthorizer.
 271  
                                                         getCollectionItemPermissionDetails(collectionItemBusinessObject));
 272  0
                                         collectionItemRoleQualifications = new AttributeSet(businessObjectAuthorizer.
 273  
                                                         getCollectionItemRoleQualifications(collectionItemBusinessObject));
 274  
                                 }
 275  
                                 else {
 276  0
                                         collectionItemPermissionDetails.putAll(getFieldPermissionDetails(primaryDataObject, attributeName));
 277  
                                 }
 278  0
                                 if (attributeDefinition.getAttributeSecurity().isReadOnly()) {
 279  0
                                         if (!businessObjectAuthorizer
 280  
                                                                 .isAuthorizedByTemplate(
 281  
                                                                                 primaryDataObject,
 282  
                                                                                 KNSConstants.KNS_NAMESPACE,
 283  
                                                                                 KimConstants.PermissionTemplateNames.MODIFY_FIELD,
 284  
                                                                                 user.getPrincipalId(),
 285  
                                                                                 collectionItemPermissionDetails,
 286  
                                                                                 collectionItemRoleQualifications)) {
 287  0
                                                 maintenanceDocumentRestrictions
 288  
                                                                 .addReadOnlyField(propertyPrefix + attributeName);
 289  
                                         }
 290  
                                 }
 291  
                         }
 292  0
                 }
 293  0
         }
 294  
         
 295  
         /**
 296  
          * @param dataObjectEntry if collectionItemBusinessObject is not null, then it is the DD entry for collectionItemBusinessObject.
 297  
          * Otherwise, it is the entry for primaryBusinessObject
 298  
          * @param primaryDataObject the top-level BO that is being inquiried or maintained
 299  
          * @param collectionItemBusinessObject an element of a collection under the primaryBusinessObject that we are evaluating view auths for
 300  
          * @param user the logged in user
 301  
          * @param businessObjectAuthorizer
 302  
          * @param inquiryOrMaintenanceDocumentRestrictions
 303  
          * @param propertyPrefix
 304  
          */
 305  
         protected void considerCustomButtonFieldAuthorization(
 306  
                         DataObjectEntry dataObjectEntry,
 307  
                         Object primaryDataObject,
 308  
                         BusinessObject collectionItemBusinessObject,
 309  
                         Person user,
 310  
                         BusinessObjectAuthorizer businessObjectAuthorizer,
 311  
                         MaintenanceDocumentRestrictions maintenanceDocumentRestrictions,
 312  
                         String propertyPrefix) {
 313  0
                 for (String attributeName : dataObjectEntry.getAttributeNames()) {
 314  0
                         AttributeDefinition attributeDefinition = dataObjectEntry
 315  
                                         .getAttributeDefinition(attributeName);
 316  
                         // TODO what is the equivalent of control.isButton in KRAD
 317  0
                         if (attributeDefinition.getControl() != null &&
 318  
                                 attributeDefinition.getControl().isButton()) {
 319  0
                                 AttributeSet collectionItemPermissionDetails = new AttributeSet();
 320  0
                                 AttributeSet collectionItemRoleQualifications = null;
 321  0
                                 if (ObjectUtils.isNotNull(collectionItemBusinessObject)) {
 322  0
                                         collectionItemPermissionDetails.putAll(getButtonFieldPermissionDetails(collectionItemBusinessObject, attributeName));
 323  0
                                         collectionItemPermissionDetails.putAll(businessObjectAuthorizer.
 324  
                                                         getCollectionItemPermissionDetails(collectionItemBusinessObject));
 325  0
                                         collectionItemRoleQualifications = new AttributeSet(businessObjectAuthorizer.
 326  
                                                         getCollectionItemRoleQualifications(collectionItemBusinessObject));
 327  
                                 }
 328  
                                 else {
 329  0
                                         getButtonFieldPermissionDetails(primaryDataObject, attributeName);
 330  
                                 }
 331  
                                 
 332  0
                                 if (!businessObjectAuthorizer
 333  
                                                 .isAuthorizedByTemplate(
 334  
                                                                 primaryDataObject,
 335  
                                                                 KNSConstants.KNS_NAMESPACE,
 336  
                                                                 KimConstants.PermissionTemplateNames.PERFORM_CUSTOM_MAINTENANCE_DOCUMENT_FUNCTION,
 337  
                                                                 user.getPrincipalId(),
 338  
                                                                 collectionItemPermissionDetails,
 339  
                                                                 collectionItemRoleQualifications)) {
 340  0
                                         maintenanceDocumentRestrictions
 341  
                                                         .addHiddenField(propertyPrefix + attributeName);
 342  
                                 }
 343  
                         }
 344  0
                 }
 345  0
         }
 346  
 
 347  
         protected void considerInquiryOrMaintenanceDocumentPresentationController(
 348  
                         InquiryOrMaintenanceDocumentPresentationController businessObjectPresentationController,
 349  
                         BusinessObject businessObject,
 350  
                         InquiryOrMaintenanceDocumentRestrictions inquiryOrMaintenanceDocumentRestrictions) {
 351  0
                 for (String attributeName : businessObjectPresentationController
 352  
                                 .getConditionallyHiddenPropertyNames(businessObject)) {
 353  0
                         inquiryOrMaintenanceDocumentRestrictions
 354  
                                         .addHiddenField(attributeName);
 355  
                 }
 356  0
                 for (String sectionId : businessObjectPresentationController
 357  
                                 .getConditionallyHiddenSectionIds(businessObject)) {
 358  0
                         inquiryOrMaintenanceDocumentRestrictions
 359  
                                         .addHiddenSectionId(sectionId);
 360  
                 }
 361  0
         }
 362  
 
 363  
         protected void considerInquiryOrMaintenanceDocumentAuthorizer(
 364  
                         InquiryOrMaintenanceDocumentAuthorizer authorizer,
 365  
                         BusinessObject businessObject, Person user,
 366  
                         InquiryOrMaintenanceDocumentRestrictions restrictions) {
 367  0
                 for (String sectionId : authorizer
 368  
                                 .getSecurePotentiallyHiddenSectionIds()) {
 369  0
                         Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 370  0
                         additionalPermissionDetails
 371  
                                         .put(KimConstants.AttributeConstants.SECTION_ID, sectionId);
 372  0
                         if (!authorizer.isAuthorizedByTemplate(businessObject,
 373  
                                         KNSConstants.KNS_NAMESPACE,
 374  
                                         KimConstants.PermissionTemplateNames.VIEW_SECTION, user
 375  
                                                         .getPrincipalId(), additionalPermissionDetails,
 376  
                                         null)) {
 377  0
                                 restrictions.addHiddenSectionId(sectionId);
 378  
                         }
 379  0
                 }
 380  0
         }
 381  
 
 382  
         protected void considerMaintenanceDocumentPresentationController(
 383  
                         MaintenanceDocumentPresentationController presentationController,
 384  
                         MaintenanceDocument document,
 385  
                         MaintenanceDocumentRestrictions restrictions) {
 386  0
                 for (String attributeName : presentationController
 387  
                                 .getConditionallyReadOnlyPropertyNames(document)) {
 388  0
                         restrictions.addReadOnlyField(attributeName);
 389  
                 }
 390  0
                 for (String sectionId : presentationController
 391  
                                 .getConditionallyReadOnlySectionIds(document)) {
 392  0
                         restrictions.addReadOnlySectionId(sectionId);
 393  
                 }
 394  0
         }
 395  
 
 396  
         protected void considerMaintenanceDocumentAuthorizer(
 397  
                         MaintenanceDocumentAuthorizer authorizer,
 398  
                         MaintenanceDocument document, Person user,
 399  
                         MaintenanceDocumentRestrictions restrictions) {
 400  0
                 for (String sectionId : authorizer
 401  
                                 .getSecurePotentiallyReadOnlySectionIds()) {
 402  0
                         Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 403  0
                         additionalPermissionDetails
 404  
                                         .put(KimConstants.AttributeConstants.SECTION_ID, sectionId);
 405  0
                         if (!authorizer.isAuthorizedByTemplate(document,
 406  
                                         KNSConstants.KNS_NAMESPACE,
 407  
                                         KimConstants.PermissionTemplateNames.MODIFY_SECTION, user
 408  
                                                         .getPrincipalId(), additionalPermissionDetails,
 409  
                                         null)) {
 410  0
                                 restrictions.addReadOnlySectionId(sectionId);
 411  
                         }
 412  0
                 }
 413  0
         }
 414  
 
 415  
         @SuppressWarnings("unchecked")
 416  
         protected void addInquirableItemRestrictions(Collection sectionDefinitions,
 417  
                         InquiryAuthorizer authorizer, InquiryRestrictions restrictions,
 418  
                         BusinessObject primaryBusinessObject,
 419  
                         BusinessObject businessObject, String propertyPrefix, Person user) {
 420  0
                 for (Object inquirableItemDefinition : sectionDefinitions) {
 421  0
                         if (inquirableItemDefinition instanceof InquiryCollectionDefinition) {
 422  0
                                 InquiryCollectionDefinition inquiryCollectionDefinition = (InquiryCollectionDefinition) inquirableItemDefinition;
 423  0
                                 BusinessObjectEntry collectionBusinessObjectEntry = getDataDictionaryService()
 424  
                                                 .getDataDictionary().getBusinessObjectEntry(
 425  
                                                                 inquiryCollectionDefinition.getBusinessObjectClass().getName());
 426  
 
 427  
                                 try {
 428  0
                                         Collection<BusinessObject> collection = (Collection<BusinessObject>) PropertyUtils
 429  
                                                         .getProperty(businessObject,
 430  
                                                                         inquiryCollectionDefinition.getName());
 431  0
                                         int i = 0;
 432  0
                                         for (Iterator<BusinessObject> iterator = collection.iterator(); iterator
 433  0
                                                         .hasNext();) {
 434  0
                                                 String newPropertyPrefix = propertyPrefix + inquiryCollectionDefinition.getName() + "[" + i + "].";
 435  0
                                                 BusinessObject collectionItemBusinessObject = iterator.next();
 436  0
                                                 considerBusinessObjectFieldUnmaskAuthorization(
 437  
                                                                 collectionItemBusinessObject, user, restrictions,
 438  
                                                                 newPropertyPrefix, null);
 439  0
                                                 considerBusinessObjectFieldViewAuthorization(
 440  
                                                                 collectionBusinessObjectEntry, primaryBusinessObject, collectionItemBusinessObject,
 441  
                                                                 user, authorizer, restrictions, newPropertyPrefix);
 442  0
                                                 addInquirableItemRestrictions(
 443  
                                                                 inquiryCollectionDefinition
 444  
                                                                                 .getInquiryCollections(),
 445  
                                                                 authorizer,
 446  
                                                                 restrictions,
 447  
                                                                 primaryBusinessObject,
 448  
                                                                 collectionItemBusinessObject,
 449  
                                                                 newPropertyPrefix,
 450  
                                                                 user);
 451  0
                                                 i++;
 452  0
                                         }
 453  0
                                 } catch (Exception e) {
 454  0
                                         throw new RuntimeException(
 455  
                                                         "Unable to resolve collection property: "
 456  
                                                                         + businessObject.getClass() + ":"
 457  
                                                                         + inquiryCollectionDefinition.getName(), e);
 458  0
                                 }
 459  0
                         }
 460  
                 }
 461  0
         }
 462  
 
 463  
         @SuppressWarnings("unchecked")
 464  
         protected void addMaintainableItemRestrictions(List<? extends MaintainableItemDefinition> itemDefinitions,
 465  
                         MaintenanceDocumentAuthorizer authorizer,
 466  
                         MaintenanceDocumentRestrictions restrictions,
 467  
                         MaintenanceDocument maintenanceDocument,
 468  
                         BusinessObject businessObject, String propertyPrefix, Person user) {
 469  0
                 for (MaintainableItemDefinition maintainableItemDefinition : itemDefinitions) {
 470  0
                         if (maintainableItemDefinition instanceof MaintainableCollectionDefinition) {
 471  
                                 try {
 472  0
                                         MaintainableCollectionDefinition maintainableCollectionDefinition = (MaintainableCollectionDefinition) maintainableItemDefinition;
 473  
                                         
 474  0
                                         Collection<BusinessObject> collection = (Collection<BusinessObject>) ObjectUtils
 475  
                                                         .getNestedValue(businessObject,
 476  
                                                                         maintainableItemDefinition.getName());
 477  0
                                         BusinessObjectEntry collectionBusinessObjectEntry = getDataDictionaryService()
 478  
                                                         .getDataDictionary().getBusinessObjectEntry(
 479  
                                                                         maintainableCollectionDefinition.getBusinessObjectClass().getName());
 480  0
                                         if (collection != null) {
 481  0
                                             int i = 0;
 482  0
                                              for (Iterator<BusinessObject> iterator = collection.iterator(); iterator
 483  0
                                                         .hasNext();) {
 484  0
                                                      String newPropertyPrefix = propertyPrefix + maintainableItemDefinition.getName() + "[" + i + "].";
 485  0
                                                     BusinessObject collectionBusinessObject = iterator.next();
 486  0
                                                     considerBusinessObjectFieldUnmaskAuthorization(
 487  
                                                                 collectionBusinessObject, user, restrictions,
 488  
                                                                 newPropertyPrefix, maintenanceDocument);
 489  0
                                                     considerBusinessObjectFieldViewAuthorization(
 490  
                                                                 collectionBusinessObjectEntry, maintenanceDocument, collectionBusinessObject, user,
 491  
                                                                 authorizer, restrictions, newPropertyPrefix);
 492  0
                                                     considerBusinessObjectFieldModifyAuthorization(
 493  
                                                                 collectionBusinessObjectEntry, maintenanceDocument, collectionBusinessObject, user,
 494  
                                                                 authorizer, restrictions, newPropertyPrefix);
 495  0
                                                     addMaintainableItemRestrictions(
 496  
                                                                 ((MaintainableCollectionDefinition) maintainableItemDefinition)
 497  
                                                                                 .getMaintainableCollections(),
 498  
                                                                 authorizer, restrictions, maintenanceDocument,
 499  
                                                                 collectionBusinessObject, newPropertyPrefix,
 500  
                                                                 user);
 501  0
                                                      addMaintainableItemRestrictions(
 502  
                                                                 ((MaintainableCollectionDefinition) maintainableItemDefinition)
 503  
                                                                                 .getMaintainableFields(), authorizer,
 504  
                                                                 restrictions, maintenanceDocument,
 505  
                                                                 collectionBusinessObject, newPropertyPrefix,
 506  
                                                                 user);
 507  0
                                                     i++;
 508  0
                                             }
 509  
                                         }
 510  0
                                 } catch (Exception e) {
 511  0
                                         throw new RuntimeException(
 512  
                                                         "Unable to resolve collection property: "
 513  
                                                                         + businessObject.getClass() + ":"
 514  
                                                                         + maintainableItemDefinition.getName(), e);
 515  0
                                 }
 516  
                         }
 517  
                 }
 518  0
         }
 519  
         
 520  
         public boolean canFullyUnmaskField(Person user,
 521  
                         Class<?> dataObjectClass, String fieldName, Document document) {
 522  
                 // KFSMI-5095
 523  0
                 if(isNonProductionEnvAndUnmaskingTurnedOff())
 524  0
                         return false;
 525  
 
 526  0
                 if(user==null || StringUtils.isEmpty(user.getPrincipalId())) 
 527  0
                         return false;
 528  0
                 Boolean result = null;
 529  0
                 if (document != null) { // if a document was passed, evaluate the permission in the context of a document
 530  
                         try { // try/catch and fallthrough is a fix for KULRICE-3365
 531  0
                                 result = getDocumentHelperService().getDocumentAuthorizer( document )
 532  
                                 .isAuthorizedByTemplate( document, 
 533  
                                                 KNSConstants.KNS_NAMESPACE, 
 534  
                                                 KimConstants.PermissionTemplateNames.FULL_UNMASK_FIELD, 
 535  
                                                 user.getPrincipalId(), getFieldPermissionDetails(dataObjectClass, fieldName), null  );
 536  0
                         } catch (IllegalArgumentException e) { 
 537  
                                 // document didn't have needed metadata
 538  
                                 // TODO: this requires intimate knowledge of DocumentHelperServiceImpl 
 539  0
                         } 
 540  
                 }
 541  0
                 if (result == null) { 
 542  0
                         result = getIdentityManagementService().isAuthorizedByTemplateName(
 543  
                                         user.getPrincipalId(),
 544  
                                         KNSConstants.KNS_NAMESPACE,
 545  
                                         KimConstants.PermissionTemplateNames.FULL_UNMASK_FIELD,
 546  
                                         new AttributeSet(getFieldPermissionDetails(dataObjectClass, fieldName)), 
 547  
                                         null);
 548  
                 }
 549  0
                 return result; // should be safe to return Boolean here since the only circumstances that
 550  
                                // will leave it null will result in an exception being thrown above.
 551  
         }
 552  
 
 553  
         public boolean canPartiallyUnmaskField(
 554  
                         Person user, Class<?> dataObjectClass, String fieldName, Document document) {
 555  
                 // KFSMI-5095
 556  0
                 if(isNonProductionEnvAndUnmaskingTurnedOff())
 557  0
                         return false;
 558  
                 
 559  0
                 if(user==null || StringUtils.isEmpty(user.getPrincipalId())) 
 560  0
                         return false;
 561  
 
 562  0
                 if ( document == null ) {
 563  0
                         return getIdentityManagementService().isAuthorizedByTemplateName(
 564  
                                         user.getPrincipalId(),
 565  
                                         KNSConstants.KNS_NAMESPACE,
 566  
                                         KimConstants.PermissionTemplateNames.PARTIAL_UNMASK_FIELD,
 567  
                                         new AttributeSet(getFieldPermissionDetails(dataObjectClass,fieldName)), 
 568  
                                         null);
 569  
                 } else { // if a document was passed, evaluate the permission in the context of a document
 570  0
                         return getDocumentHelperService().getDocumentAuthorizer( document )
 571  
                                         .isAuthorizedByTemplate( document, 
 572  
                                                                                          KNSConstants.KNS_NAMESPACE, 
 573  
                                                                                          KimConstants.PermissionTemplateNames.PARTIAL_UNMASK_FIELD, 
 574  
                                                                                          user.getPrincipalId(), getFieldPermissionDetails(dataObjectClass, fieldName), null  );
 575  
                 }
 576  
         }
 577  
         
 578  
         public boolean canCreate(Class<?> dataObjectClass, Person user, String docTypeName){
 579  0
                 DocumentPresentationController documentPresentationController = getDocumentHelperService().getDocumentPresentationController(docTypeName);
 580  0
             boolean canCreate = ((MaintenanceDocumentPresentationController) documentPresentationController).canCreate(dataObjectClass);
 581  0
             if(canCreate){
 582  0
                         DocumentAuthorizer documentAuthorizer = getDocumentHelperService().getDocumentAuthorizer(docTypeName);
 583  0
                     canCreate = ((MaintenanceDocumentAuthorizer) documentAuthorizer).canCreate(dataObjectClass, user);
 584  
             }
 585  0
                 return canCreate;
 586  
         }
 587  
         
 588  
         public boolean canMaintain(Object dataObject, Person user,
 589  
                         String docTypeName) {
 590  0
                 return ((MaintenanceDocumentAuthorizer) getDocumentHelperService()
 591  
                                 .getDocumentAuthorizer(docTypeName)).canMaintain(
 592  
                                                 dataObject, user);
 593  
         }
 594  
 
 595  
         protected Map<String, String> getFieldPermissionDetails(
 596  
                         Class<?> dataObjectClass, String attributeName) {
 597  
                 try {
 598  0
                         return getFieldPermissionDetails(dataObjectClass.newInstance(),
 599  
                                         attributeName);
 600  0
                 } catch (Exception e) {
 601  0
                         throw new RuntimeException(
 602  
                                         "The getPermissionDetails method of BusinessObjectAuthorizationServiceImpl was unable to instantiate the dataObjectClass"
 603  
                                                         + dataObjectClass, e);
 604  
                 }
 605  
         }
 606  
 
 607  
         protected Map<String, String> getFieldPermissionDetails(
 608  
                         Object dataObject, String attributeName) {
 609  0
                 Map<String, String> permissionDetails = null;
 610  0
                 String namespaceCode = null;
 611  0
                 String componentName = null;
 612  0
                 String propertyName = null;
 613  
                 // JHK: commenting out for KFSMI-2398 - permission checks need to be done at the level specified
 614  
                 // that is, if the parent object specifies the security, that object should be used for the 
 615  
                 // component
 616  
 //                if (attributeName.contains(".")) {
 617  
 //                        try {
 618  
 //                                permissionDetails = KimCommonUtils
 619  
 //                                                .getNamespaceAndComponentSimpleName(PropertyUtils
 620  
 //                                                                .getPropertyType(businessObject, attributeName
 621  
 //                                                                                .substring(0, attributeName
 622  
 //                                                                                                .lastIndexOf("."))));
 623  
 //                        } catch (Exception e) {
 624  
 //                                throw new RuntimeException(
 625  
 //                                                "Unable to discover nested business object class: "
 626  
 //                                                                + businessObject.getClass() + " : "
 627  
 //                                                                + attributeName, e);
 628  
 //                        }
 629  
 //                        permissionDetails.put(KimAttributes.PROPERTY_NAME, attributeName
 630  
 //                                        .substring(attributeName.indexOf(".") + 1));
 631  
 //                } else {
 632  0
                         permissionDetails = KNSUtils
 633  
                                         .getNamespaceAndComponentSimpleName(dataObject
 634  
                                                         .getClass());
 635  0
                         permissionDetails.put(KimConstants.AttributeConstants.PROPERTY_NAME, attributeName);
 636  
 //                }
 637  0
                 return permissionDetails;
 638  
         }
 639  
         
 640  
         protected Map<String, String> getButtonFieldPermissionDetails(
 641  
                         Object businessObject, String attributeName) {
 642  0
                 Map<String, String> permissionDetails = new AttributeSet();
 643  0
                 if (attributeName.contains(".")) {
 644  0
                         permissionDetails.put(KimConstants.AttributeConstants.BUTTON_NAME, attributeName);
 645  
                 } else {
 646  0
                         permissionDetails.put(KimConstants.AttributeConstants.BUTTON_NAME, attributeName);
 647  
                 }
 648  0
                 return permissionDetails;
 649  
         }
 650  
 
 651  
         private DataDictionaryService getDataDictionaryService() {
 652  0
                 if (dataDictionaryService == null) {
 653  0
                         dataDictionaryService = KNSServiceLocatorWeb
 654  
                                         .getDataDictionaryService();
 655  
                 }
 656  0
                 return dataDictionaryService;
 657  
         }
 658  
 
 659  
         private IdentityManagementService getIdentityManagementService() {
 660  0
                 if (identityManagementService == null) {
 661  0
                         identityManagementService = KIMServiceLocator
 662  
                                         .getIdentityManagementService();
 663  
                 }
 664  0
                 return identityManagementService;
 665  
         }
 666  
 
 667  
         private BusinessObjectDictionaryService getBusinessObjectDictionaryService() {
 668  0
                 if (businessObjectDictionaryService == null) {
 669  0
                         businessObjectDictionaryService = KNSServiceLocatorWeb
 670  
                                         .getBusinessObjectDictionaryService();
 671  
                 }
 672  0
                 return businessObjectDictionaryService;
 673  
         }
 674  
 
 675  
         private DocumentHelperService getDocumentHelperService() {
 676  0
                 if (documentHelperService == null) {
 677  0
                         documentHelperService = KNSServiceLocatorWeb.getDocumentHelperService();
 678  
                 }
 679  0
                 return documentHelperService;
 680  
         }
 681  
 
 682  
         private MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() {
 683  0
                 if (maintenanceDocumentDictionaryService == null) {
 684  0
                         maintenanceDocumentDictionaryService = KNSServiceLocatorWeb
 685  
                                         .getMaintenanceDocumentDictionaryService();
 686  
                 }
 687  0
                 return maintenanceDocumentDictionaryService;
 688  
         }
 689  
 
 690  
         private ConfigurationService getKualiConfigurationService() {
 691  0
                 if (kualiConfigurationService == null) {
 692  0
                         kualiConfigurationService = KNSServiceLocator.getKualiConfigurationService();
 693  
                 }
 694  0
                 return kualiConfigurationService;
 695  
         }
 696  
         
 697  
         /**
 698  
          * @see org.kuali.rice.kns.service.BusinessObjectAuthorizationService#attributeValueNeedsToBeEncryptedOnFormsAndLinks(java.lang.Class, java.lang.String)
 699  
          */
 700  
         public boolean attributeValueNeedsToBeEncryptedOnFormsAndLinks(Class<?> dataObjectClass, String attributeName) {
 701  0
                 AttributeSecurity attributeSecurity = getDataDictionaryService().getAttributeSecurity(
 702  
                                 dataObjectClass.getName(), attributeName);
 703  0
                 return attributeSecurity != null && attributeSecurity.hasRestrictionThatRemovesValueFromUI();
 704  
         }
 705  
 
 706  
         private boolean isNonProductionEnvAndUnmaskingTurnedOff(){
 707  0
                 return !getKualiConfigurationService().isProductionEnvironment() && 
 708  
                                 !getKualiConfigurationService().getPropertyAsBoolean(KNSConstants.ENABLE_NONPRODUCTION_UNMASKING);
 709  
         }
 710  
 
 711  
 }