Coverage Report - org.kuali.rice.kns.uif.authorization.DocumentAuthorizerBase
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentAuthorizerBase
0%
0/97
0%
0/82
4.067
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 1.0 (the
 5  
  * "License"); you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12  
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13  
  * License for the specific language governing permissions and limitations under
 14  
  * the License.
 15  
  */
 16  
 package org.kuali.rice.kns.uif.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.document.Document;
 32  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 33  
 import org.kuali.rice.kns.util.KNSConstants;
 34  
 import org.kuali.rice.kns.web.spring.form.DocumentFormBase;
 35  
 import org.kuali.rice.kns.web.spring.form.UifFormBase;
 36  
 import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
 37  
 
 38  
 /**
 39  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 40  
  */
 41  0
 public class DocumentAuthorizerBase extends AuthorizerBase {
 42  0
     protected static Log LOG = LogFactory.getLog(DocumentAuthorizerBase.class);
 43  
 
 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  
 
 49  
     @Override
 50  
     public Set<String> getActionFlags(UifFormBase model, Person user, Set<String> documentActions) {
 51  0
         Document document = ((DocumentFormBase) model).getDocument();
 52  
 
 53  0
         if (LOG.isDebugEnabled()) {
 54  0
             LOG.debug("calling DocumentAuthorizerBase.getDocumentActionFlags for document '"
 55  
                     + document.getDocumentNumber() + "'. user '" + user.getPrincipalName() + "'");
 56  
         }
 57  
 
 58  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_EDIT)
 59  
                 && !isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 60  
                         KimConstants.PermissionTemplateNames.EDIT_DOCUMENT, user.getPrincipalId())) {
 61  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_EDIT);
 62  
         }
 63  
 
 64  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_COPY)
 65  
                 && !isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 66  
                         KimConstants.PermissionTemplateNames.COPY_DOCUMENT, user.getPrincipalId())) {
 67  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_COPY);
 68  
         }
 69  
 
 70  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_BLANKET_APPROVE)
 71  
                 && !isAuthorizedByTemplate(document, KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 72  
                         KimConstants.PermissionTemplateNames.BLANKET_APPROVE_DOCUMENT, user.getPrincipalId())) {
 73  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_BLANKET_APPROVE);
 74  
         }
 75  
 
 76  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_CANCEL)
 77  
                 && !isAuthorizedByTemplate(document, KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 78  
                         KimConstants.PermissionTemplateNames.CANCEL_DOCUMENT, user.getPrincipalId())) {
 79  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_CANCEL);
 80  
         }
 81  
 
 82  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_SAVE)
 83  
                 && !isAuthorizedByTemplate(document, KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 84  
                         KimConstants.PermissionTemplateNames.SAVE_DOCUMENT, user.getPrincipalId())) {
 85  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SAVE);
 86  
         }
 87  
 
 88  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_ROUTE)
 89  
                 && !isAuthorizedByTemplate(document, KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 90  
                         KimConstants.PermissionTemplateNames.ROUTE_DOCUMENT, user.getPrincipalId())) {
 91  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ROUTE);
 92  
         }
 93  
 
 94  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_ACKNOWLEDGE)
 95  
                 && !canTakeRequestedAction(document, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, user)) {
 96  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ACKNOWLEDGE);
 97  
         }
 98  
 
 99  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_FYI)
 100  
                 && !canTakeRequestedAction(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 101  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_FYI);
 102  
         }
 103  
 
 104  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_APPROVE)
 105  
                 || documentActions.contains(KNSConstants.KUALI_ACTION_CAN_DISAPPROVE)) {
 106  0
             if (!canTakeRequestedAction(document, KEWConstants.ACTION_REQUEST_APPROVE_REQ, user)) {
 107  0
                 documentActions.remove(KNSConstants.KUALI_ACTION_CAN_APPROVE);
 108  0
                 documentActions.remove(KNSConstants.KUALI_ACTION_CAN_DISAPPROVE);
 109  
             }
 110  
         }
 111  
 
 112  0
         if (!canSendAnyTypeAdHocRequests(document, user)) {
 113  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ADD_ADHOC_REQUESTS);
 114  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SEND_ADHOC_REQUESTS);
 115  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI);
 116  
         }
 117  
 
 118  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI)
 119  
                 && !canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 120  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI);
 121  
         }
 122  
 
 123  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_ANNOTATE)
 124  
                 && !documentActions.contains(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
 125  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_ANNOTATE);
 126  
         }
 127  
 
 128  0
         if (documentActions.contains(KNSConstants.KUALI_ACTION_CAN_EDIT__DOCUMENT_OVERVIEW)
 129  
                 && !canEditDocumentOverview(document, user)) {
 130  0
             documentActions.remove(KNSConstants.KUALI_ACTION_CAN_EDIT__DOCUMENT_OVERVIEW);
 131  
         }
 132  
 
 133  0
         return documentActions;
 134  
     }
 135  
 
 136  
     public final boolean canInitiate(String documentTypeName, Person user) {
 137  0
                 String nameSpaceCode = KNSConstants.KUALI_RICE_SYSTEM_NAMESPACE;
 138  0
                 AttributeSet permissionDetails = new AttributeSet();
 139  0
                 permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME,
 140  
                                 documentTypeName);
 141  0
                 return getIdentityManagementService().isAuthorizedByTemplateName(
 142  
                                 user.getPrincipalId(), nameSpaceCode,
 143  
                                 KimConstants.PermissionTemplateNames.INITIATE_DOCUMENT,
 144  
                                 permissionDetails, null);
 145  
         }
 146  
 
 147  
         public final boolean canReceiveAdHoc(Document document, Person user,
 148  
                         String actionRequestCode) {
 149  0
                 Map<String,String> additionalPermissionDetails = new HashMap<String, String>();
 150  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD, actionRequestCode);
 151  0
                 return isAuthorizedByTemplate(document,
 152  
                                 KNSConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 153  
                                 KimConstants.PermissionTemplateNames.AD_HOC_REVIEW_DOCUMENT,
 154  
                                 user.getPrincipalId(), additionalPermissionDetails, null );
 155  
         }
 156  
 
 157  
         public final boolean canOpen(Document document, Person user) {
 158  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 159  
                                 KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, user
 160  
                                                 .getPrincipalId());
 161  
         }
 162  
 
 163  
         public final boolean canAddNoteAttachment(Document document,
 164  
                         String attachmentTypeCode, Person user) {
 165  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 166  0
                 if (attachmentTypeCode != null) {
 167  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 168  
                                         attachmentTypeCode);
 169  
                 }
 170  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 171  
                                 KimConstants.PermissionTemplateNames.ADD_NOTE_ATTACHMENT, user
 172  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 173  
         }
 174  
 
 175  
         public final boolean canDeleteNoteAttachment(Document document,
 176  
                         String attachmentTypeCode, String createdBySelfOnly, Person user) {
 177  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 178  0
                 if (attachmentTypeCode != null) {
 179  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 180  
                                         attachmentTypeCode);
 181  
                 }
 182  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.CREATED_BY_SELF,
 183  
                                 createdBySelfOnly);
 184  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 185  
                                 KimConstants.PermissionTemplateNames.DELETE_NOTE_ATTACHMENT,
 186  
                                 user.getPrincipalId(), additionalPermissionDetails, null);
 187  
         }
 188  
 
 189  
         public final boolean canViewNoteAttachment(Document document,
 190  
                         String attachmentTypeCode, Person user) {
 191  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 192  0
                 if (attachmentTypeCode != null) {
 193  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 194  
                                         attachmentTypeCode);
 195  
                 }
 196  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 197  
                                 KimConstants.PermissionTemplateNames.VIEW_NOTE_ATTACHMENT, user
 198  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 199  
         }
 200  
         
 201  
         public final boolean canSendAdHocRequests(Document document,
 202  
                         String actionRequestCd, Person user) {
 203  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 204  0
                 if (actionRequestCd != null) {
 205  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD,
 206  
                                         actionRequestCd);
 207  
                 }
 208  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 209  
                                 KimConstants.PermissionTemplateNames.SEND_AD_HOC_REQUEST, user
 210  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 211  
         }
 212  
         
 213  
         public boolean canEditDocumentOverview(Document document, Person user){
 214  0
                 return isAuthorizedByTemplate(document,
 215  
                                 KNSConstants.KNS_NAMESPACE,
 216  
                                 KimConstants.PermissionTemplateNames.EDIT_DOCUMENT,
 217  
                                 user.getPrincipalId()) && this.isDocumentInitiator(document, user);
 218  
         }
 219  
         
 220  
         protected final boolean canSendAnyTypeAdHocRequests(Document document, Person user) {
 221  0
                 if(canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 222  
                     try {
 223  0
                 DocumentTypeDTO docType = KNSServiceLocatorWeb.getWorkflowInfoService().getDocType(document.getDocumentHeader().getWorkflowDocument().getDocumentType());
 224  0
                 ProcessDTO process = docType.getRoutePath().getPrimaryProcess();
 225  0
                 if (process != null) {
 226  0
                     if (process.getInitialRouteNode() == null) {
 227  0
                         return false;
 228  
                     }
 229  
                 } else {
 230  0
                     return false;
 231  
                 }
 232  0
                     } catch (WorkflowException e) {
 233  0
                 return false;
 234  0
             }
 235  0
                         return true;
 236  0
                 }else if(canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, user)){
 237  0
                         return true;
 238  
                 }
 239  0
                 return canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_APPROVE_REQ, user);
 240  
         }
 241  
 
 242  
         protected boolean canTakeRequestedAction(Document document,
 243  
                         String actionRequestCode, Person user) {
 244  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 245  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD,
 246  
                                 actionRequestCode);
 247  0
                 return isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE,
 248  
                                 KimConstants.PermissionTemplateNames.TAKE_REQUESTED_ACTION,
 249  
                                 user.getPrincipalId(), additionalPermissionDetails, null);
 250  
         }
 251  
 
 252  
         @Override
 253  
         protected void addPermissionDetails(Object dataObject,
 254  
                         Map<String, String> attributes) {
 255  0
                 super.addPermissionDetails(dataObject, attributes);
 256  0
                 if (dataObject instanceof Document) {
 257  0
                         addStandardAttributes((Document) dataObject, attributes);
 258  
                 }
 259  0
         }
 260  
 
 261  
         @Override
 262  
         protected void addRoleQualification(Object dataObject,
 263  
                         Map<String, String> attributes) {
 264  0
                 super.addRoleQualification(dataObject, attributes);
 265  0
                 if (dataObject instanceof Document) {
 266  0
                         addStandardAttributes((Document) dataObject, attributes);
 267  
                 }
 268  0
         }
 269  
 
 270  
         protected void addStandardAttributes(Document document,
 271  
                         Map<String, String> attributes) {
 272  0
                 KualiWorkflowDocument wd = document.getDocumentHeader()
 273  
                                 .getWorkflowDocument();
 274  0
                 attributes.put(KimConstants.AttributeConstants.DOCUMENT_NUMBER, document
 275  
                                 .getDocumentNumber());
 276  0
                 attributes.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, wd.getDocumentType());
 277  0
                 if (wd.stateIsInitiated() || wd.stateIsSaved()) {
 278  0
                         attributes.put(KimConstants.AttributeConstants.ROUTE_NODE_NAME,
 279  
                                         PRE_ROUTING_ROUTE_NAME);
 280  
                 } else {
 281  0
                         attributes.put(KimConstants.AttributeConstants.ROUTE_NODE_NAME, wd
 282  
                                         .getCurrentRouteNodeNames());
 283  
                 }
 284  0
                 attributes.put(KimConstants.AttributeConstants.ROUTE_STATUS_CODE, wd.getRouteHeader()
 285  
                                 .getDocRouteStatus());
 286  0
         }
 287  
         
 288  
         protected boolean isDocumentInitiator(Document document, Person user) {
 289  0
         KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
 290  0
         return workflowDocument.getInitiatorPrincipalId().equalsIgnoreCase(user.getPrincipalId());
 291  
     }
 292  
 
 293  
 }