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