Coverage Report - org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentAuthorizerBase
0%
0/96
0%
0/82
4.067
 
 1  
 /*
 2  
  * Copyright 2007 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.document.authorization;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 import java.util.Set;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.kuali.rice.core.util.AttributeSet;
 25  
 import org.kuali.rice.kew.dto.DocumentTypeDTO;
 26  
 import org.kuali.rice.kew.dto.ProcessDTO;
 27  
 import org.kuali.rice.kew.exception.WorkflowException;
 28  
 import org.kuali.rice.kew.util.KEWConstants;
 29  
 import org.kuali.rice.kim.bo.Person;
 30  
 import org.kuali.rice.kim.util.KimConstants;
 31  
 import org.kuali.rice.kns.authorization.BusinessObjectAuthorizerBase;
 32  
 import org.kuali.rice.kns.document.Document;
 33  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 34  
 import org.kuali.rice.kns.util.KNSConstants;
 35  
 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
 36  
 
 37  
 /**
 38  
  * DocumentAuthorizer containing common, reusable document-level authorization
 39  
  * code.
 40  
  */
 41  0
 public class DocumentAuthorizerBase extends BusinessObjectAuthorizerBase
 42  
                 implements DocumentAuthorizer {
 43  0
         protected static Log LOG = LogFactory.getLog(DocumentAuthorizerBase.class);
 44  
         public static final String PRE_ROUTING_ROUTE_NAME = "PreRoute";
 45  
         public static final String EDIT_MODE_DEFAULT_TRUE_VALUE = "TRUE";
 46  
         public static final String USER_SESSION_METHOD_TO_CALL_OBJECT_KEY = "METHOD_TO_CALL_KEYS_METHOD_OBJECT_KEY";
 47  
         public static final String USER_SESSION_METHOD_TO_CALL_COMPLETE_OBJECT_KEY = "METHOD_TO_CALL_KEYS_COMPLETE_OBJECT_KEY";
 48  
     public static final String USER_SESSION_METHOD_TO_CALL_COMPLETE_MARKER = "_EXITING";
 49  
 
 50  
         /**
 51  
          * Individual document families will need to reimplement this according to
 52  
          * their own needs; this version should be good enough to be usable during
 53  
          * initial development.
 54  
          */
 55  
         public Set<String> getDocumentActions(Document document, Person user,
 56  
                         Set<String> documentActions) {
 57  0
                 if (LOG.isDebugEnabled()) {
 58  0
                         LOG
 59  
                                         .debug("calling DocumentAuthorizerBase.getDocumentActionFlags for document '"
 60  
                                                         + document.getDocumentNumber()
 61  
                                                         + "'. user '"
 62  
                                                         + user.getPrincipalName() + "'");
 63  
                 }
 64  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_EDIT)
 65  
                                 && !isAuthorizedByTemplate(document,
 66  
                                                 KNSConstants.KNS_NAMESPACE,
 67  
                                                 KimConstants.PermissionTemplateNames.EDIT_DOCUMENT,
 68  
                                                 user.getPrincipalId())) {
 69  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_EDIT);
 70  
                 }
 71  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_COPY)
 72  
                                 && !isAuthorizedByTemplate(document,
 73  
                                                 KNSConstants.KNS_NAMESPACE,
 74  
                                                 KimConstants.PermissionTemplateNames.COPY_DOCUMENT,
 75  
                                                 user.getPrincipalId())) {
 76  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_COPY);
 77  
                 }
 78  0
                 if (documentActions
 79  
                                 .contains(KNSConstants.KUALI_ACTION_CAN_BLANKET_APPROVE)
 80  
                                 && !isAuthorizedByTemplate(
 81  
                                                 document,
 82  
                                                 KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 83  
                                                 KimConstants.PermissionTemplateNames.BLANKET_APPROVE_DOCUMENT,
 84  
                                                 user.getPrincipalId())) {
 85  0
                         documentActions
 86  
                                         .remove(KNSConstants.KUALI_ACTION_CAN_BLANKET_APPROVE);
 87  
                 }
 88  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_CANCEL)
 89  
                                 && !isAuthorizedByTemplate(document,
 90  
                                                 KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 91  
                                                 KimConstants.PermissionTemplateNames.CANCEL_DOCUMENT,
 92  
                                                 user.getPrincipalId())) {
 93  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_CANCEL);
 94  
                 }
 95  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_SAVE)
 96  
                                 && !isAuthorizedByTemplate(document,
 97  
                                                 KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 98  
                                                 KimConstants.PermissionTemplateNames.SAVE_DOCUMENT,
 99  
                                                 user.getPrincipalId())) {
 100  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SAVE);
 101  
                 }
 102  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_ROUTE)
 103  
                                 && !isAuthorizedByTemplate(document,
 104  
                                                 KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 105  
                                                 KimConstants.PermissionTemplateNames.ROUTE_DOCUMENT,
 106  
                                                 user.getPrincipalId())) {
 107  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ROUTE);
 108  
                 }
 109  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_ACKNOWLEDGE) 
 110  
                                 && !canTakeRequestedAction(document,
 111  
                                 KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, user)) {
 112  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ACKNOWLEDGE);
 113  
                 }
 114  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_FYI) &&
 115  
                                 !canTakeRequestedAction(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 116  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_FYI);
 117  
                 }
 118  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_APPROVE)
 119  
                                 || documentActions
 120  
                                                 .contains(KNSConstants.KUALI_ACTION_CAN_DISAPPROVE)) {
 121  0
                         if (!canTakeRequestedAction(document,
 122  
                                         KEWConstants.ACTION_REQUEST_APPROVE_REQ, user)) {
 123  0
                                 documentActions.remove(KNSConstants.KUALI_ACTION_CAN_APPROVE);
 124  0
                                 documentActions
 125  
                                                 .remove(KNSConstants.KUALI_ACTION_CAN_DISAPPROVE);
 126  
                         }
 127  
                 }
 128  
                 
 129  0
                 if ( !canSendAnyTypeAdHocRequests(document, user) ) {
 130  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ADD_ADHOC_REQUESTS);
 131  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SEND_ADHOC_REQUESTS);
 132  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI);
 133  
                 }
 134  
                 
 135  0
                 if(documentActions
 136  
                                 .contains(KNSConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI)
 137  
                                 && !canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)){
 138  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI);
 139  
                 }
 140  
                                 
 141  0
                 if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_ANNOTATE)
 142  
                                 && !documentActions
 143  
                                                 .contains(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
 144  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ANNOTATE);
 145  
                 }
 146  0
                 if(documentActions.contains(KNSConstants.KUALI_ACTION_CAN_EDIT__DOCUMENT_OVERVIEW) 
 147  
                                 &&!canEditDocumentOverview(document, user)){
 148  0
                         documentActions.remove(KNSConstants.KUALI_ACTION_CAN_EDIT__DOCUMENT_OVERVIEW);
 149  
                 }
 150  0
                 return documentActions;
 151  
         }
 152  
 
 153  
         public final boolean canInitiate(String documentTypeName, Person user) {
 154  0
                 String nameSpaceCode = KNSConstants.KUALI_RICE_SYSTEM_NAMESPACE;
 155  0
                 AttributeSet permissionDetails = new AttributeSet();
 156  0
                 permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME,
 157  
                                 documentTypeName);
 158  0
                 return getIdentityManagementService().isAuthorizedByTemplateName(
 159  
                                 user.getPrincipalId(), nameSpaceCode,
 160  
                                 KimConstants.PermissionTemplateNames.INITIATE_DOCUMENT,
 161  
                                 permissionDetails, null);
 162  
         }
 163  
 
 164  
         public final boolean canReceiveAdHoc(Document document, Person user,
 165  
                         String actionRequestCode) {
 166  0
                 Map<String,String> additionalPermissionDetails = new HashMap<String, String>();
 167  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD, actionRequestCode);
 168  0
                 return isAuthorizedByTemplate(document,
 169  
                                 KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 170  
                                 KimConstants.PermissionTemplateNames.AD_HOC_REVIEW_DOCUMENT,
 171  
                                 user.getPrincipalId(), additionalPermissionDetails, null );
 172  
         }
 173  
 
 174  
         public final boolean canOpen(Document document, Person user) {
 175  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 176  
                                 KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, user
 177  
                                                 .getPrincipalId());
 178  
         }
 179  
 
 180  
         public final boolean canAddNoteAttachment(Document document,
 181  
                         String attachmentTypeCode, Person user) {
 182  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 183  0
                 if (attachmentTypeCode != null) {
 184  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 185  
                                         attachmentTypeCode);
 186  
                 }
 187  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 188  
                                 KimConstants.PermissionTemplateNames.ADD_NOTE_ATTACHMENT, user
 189  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 190  
         }
 191  
 
 192  
         public final boolean canDeleteNoteAttachment(Document document,
 193  
                         String attachmentTypeCode, String createdBySelfOnly, Person user) {
 194  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 195  0
                 if (attachmentTypeCode != null) {
 196  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 197  
                                         attachmentTypeCode);
 198  
                 }
 199  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.CREATED_BY_SELF,
 200  
                                 createdBySelfOnly);
 201  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 202  
                                 KimConstants.PermissionTemplateNames.DELETE_NOTE_ATTACHMENT,
 203  
                                 user.getPrincipalId(), additionalPermissionDetails, null);
 204  
         }
 205  
 
 206  
         public final boolean canViewNoteAttachment(Document document,
 207  
                         String attachmentTypeCode, Person user) {
 208  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 209  0
                 if (attachmentTypeCode != null) {
 210  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 211  
                                         attachmentTypeCode);
 212  
                 }
 213  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 214  
                                 KimConstants.PermissionTemplateNames.VIEW_NOTE_ATTACHMENT, user
 215  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 216  
         }
 217  
         
 218  
         public final boolean canSendAdHocRequests(Document document,
 219  
                         String actionRequestCd, Person user) {
 220  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 221  0
                 if (actionRequestCd != null) {
 222  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD,
 223  
                                         actionRequestCd);
 224  
                 }
 225  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 226  
                                 KimConstants.PermissionTemplateNames.SEND_AD_HOC_REQUEST, user
 227  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 228  
         }
 229  
         
 230  
         public boolean canEditDocumentOverview(Document document, Person user){
 231  0
                 return isAuthorizedByTemplate(document,
 232  
                                 KNSConstants.KNS_NAMESPACE,
 233  
                                 KimConstants.PermissionTemplateNames.EDIT_DOCUMENT,
 234  
                                 user.getPrincipalId()) && this.isDocumentInitiator(document, user);
 235  
         }
 236  
         
 237  
         protected final boolean canSendAnyTypeAdHocRequests(Document document, Person user) {
 238  0
                 if(canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 239  
                     try {
 240  0
                 DocumentTypeDTO docType = KNSServiceLocatorWeb.getWorkflowInfoService().getDocType(document.getDocumentHeader().getWorkflowDocument().getDocumentType());
 241  0
                 ProcessDTO process = docType.getRoutePath().getPrimaryProcess();
 242  0
                 if (process != null) {
 243  0
                     if (process.getInitialRouteNode() == null) {
 244  0
                         return false;
 245  
                     }
 246  
                 } else {
 247  0
                     return false;
 248  
                 }
 249  0
                     } catch (WorkflowException e) {
 250  0
                 return false;
 251  0
             }
 252  0
                         return true;
 253  0
                 }else if(canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, user)){
 254  0
                         return true;
 255  
                 }
 256  0
                 return canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_APPROVE_REQ, user);
 257  
         }
 258  
 
 259  
         protected boolean canTakeRequestedAction(Document document,
 260  
                         String actionRequestCode, Person user) {
 261  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 262  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD,
 263  
                                 actionRequestCode);
 264  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 265  
                                 KimConstants.PermissionTemplateNames.TAKE_REQUESTED_ACTION,
 266  
                                 user.getPrincipalId(), additionalPermissionDetails, null);
 267  
         }
 268  
 
 269  
         @Override
 270  
         protected void addPermissionDetails(Object dataObject,
 271  
                         Map<String, String> attributes) {
 272  0
                 super.addPermissionDetails(dataObject, attributes);
 273  0
                 if (dataObject instanceof Document) {
 274  0
                         addStandardAttributes((Document) dataObject, attributes);
 275  
                 }
 276  0
         }
 277  
 
 278  
         @Override
 279  
         protected void addRoleQualification(Object dataObject,
 280  
                         Map<String, String> attributes) {
 281  0
                 super.addRoleQualification(dataObject, attributes);
 282  0
                 if (dataObject instanceof Document) {
 283  0
                         addStandardAttributes((Document) dataObject, attributes);
 284  
                 }
 285  0
         }
 286  
 
 287  
         protected void addStandardAttributes(Document document,
 288  
                         Map<String, String> attributes) {
 289  0
                 KualiWorkflowDocument wd = document.getDocumentHeader()
 290  
                                 .getWorkflowDocument();
 291  0
                 attributes.put(KimConstants.AttributeConstants.DOCUMENT_NUMBER, document
 292  
                                 .getDocumentNumber());
 293  0
                 attributes.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, wd.getDocumentType());
 294  0
                 if (wd.stateIsInitiated() || wd.stateIsSaved()) {
 295  0
                         attributes.put(KimConstants.AttributeConstants.ROUTE_NODE_NAME,
 296  
                                         PRE_ROUTING_ROUTE_NAME);
 297  
                 } else {
 298  0
                         attributes.put(KimConstants.AttributeConstants.ROUTE_NODE_NAME, wd
 299  
                                         .getCurrentRouteNodeNames());
 300  
                 }
 301  0
                 attributes.put(KimConstants.AttributeConstants.ROUTE_STATUS_CODE, wd.getRouteHeader()
 302  
                                 .getDocRouteStatus());
 303  0
         }
 304  
         
 305  
         protected boolean isDocumentInitiator(Document document, Person user) {
 306  0
         KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
 307  0
         return workflowDocument.getInitiatorPrincipalId().equalsIgnoreCase(user.getPrincipalId());
 308  
     }
 309  
 }