Coverage Report - org.kuali.rice.krad.uif.authorization.DocumentAuthorizerBase
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentAuthorizerBase
0%
0/94
0%
0/82
3.933
 
 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.krad.uif.authorization;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 21  
 import org.kuali.rice.kew.api.WorkflowDocument;
 22  
 import org.kuali.rice.kew.api.doctype.Process;
 23  
 import org.kuali.rice.kew.api.doctype.RoutePath;
 24  
 import org.kuali.rice.kew.util.KEWConstants;
 25  
 import org.kuali.rice.kim.bo.Person;
 26  
 import org.kuali.rice.kim.util.KimConstants;
 27  
 import org.kuali.rice.krad.document.Document;
 28  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 29  
 import org.kuali.rice.krad.util.KRADConstants;
 30  
 import org.kuali.rice.krad.web.form.DocumentFormBase;
 31  
 import org.kuali.rice.krad.web.form.UifFormBase;
 32  
 
 33  
 import java.util.HashMap;
 34  
 import java.util.Map;
 35  
 import java.util.Set;
 36  
 
 37  
 /**
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  0
 public class DocumentAuthorizerBase extends AuthorizerBase {
 41  0
     protected static Log LOG = LogFactory.getLog(DocumentAuthorizerBase.class);
 42  
 
 43  
     public static final String PRE_ROUTING_ROUTE_NAME = "PreRoute";
 44  
     public static final String EDIT_MODE_DEFAULT_TRUE_VALUE = "TRUE";
 45  
     public static final String USER_SESSION_METHOD_TO_CALL_OBJECT_KEY = "METHOD_TO_CALL_KEYS_METHOD_OBJECT_KEY";
 46  
     public static final String USER_SESSION_METHOD_TO_CALL_COMPLETE_OBJECT_KEY = "METHOD_TO_CALL_KEYS_COMPLETE_OBJECT_KEY";
 47  
 
 48  
     @Override
 49  
     public Set<String> getActionFlags(UifFormBase model, Person user, Set<String> documentActions) {
 50  0
         Document document = ((DocumentFormBase) model).getDocument();
 51  
 
 52  0
         if (LOG.isDebugEnabled()) {
 53  0
             LOG.debug("calling DocumentAuthorizerBase.getDocumentActionFlags for document '"
 54  
                     + document.getDocumentNumber() + "'. user '" + user.getPrincipalName() + "'");
 55  
         }
 56  
 
 57  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_EDIT)
 58  
                 && !isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 59  
                         KimConstants.PermissionTemplateNames.EDIT_DOCUMENT, user.getPrincipalId())) {
 60  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_EDIT);
 61  
         }
 62  
 
 63  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_COPY)
 64  
                 && !isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 65  
                         KimConstants.PermissionTemplateNames.COPY_DOCUMENT, user.getPrincipalId())) {
 66  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_COPY);
 67  
         }
 68  
 
 69  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE)
 70  
                 && !isAuthorizedByTemplate(document, KRADConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 71  
                         KimConstants.PermissionTemplateNames.BLANKET_APPROVE_DOCUMENT, user.getPrincipalId())) {
 72  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_BLANKET_APPROVE);
 73  
         }
 74  
 
 75  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_CANCEL)
 76  
                 && !isAuthorizedByTemplate(document, KRADConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 77  
                         KimConstants.PermissionTemplateNames.CANCEL_DOCUMENT, user.getPrincipalId())) {
 78  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_CANCEL);
 79  
         }
 80  
 
 81  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_SAVE)
 82  
                 && !isAuthorizedByTemplate(document, KRADConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 83  
                         KimConstants.PermissionTemplateNames.SAVE_DOCUMENT, user.getPrincipalId())) {
 84  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_SAVE);
 85  
         }
 86  
 
 87  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_ROUTE)
 88  
                 && !isAuthorizedByTemplate(document, KRADConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 89  
                         KimConstants.PermissionTemplateNames.ROUTE_DOCUMENT, user.getPrincipalId())) {
 90  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_ROUTE);
 91  
         }
 92  
 
 93  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_ACKNOWLEDGE)
 94  
                 && !canTakeRequestedAction(document, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, user)) {
 95  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_ACKNOWLEDGE);
 96  
         }
 97  
 
 98  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_FYI)
 99  
                 && !canTakeRequestedAction(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 100  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_FYI);
 101  
         }
 102  
 
 103  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_APPROVE)
 104  
                 || documentActions.contains(KRADConstants.KUALI_ACTION_CAN_DISAPPROVE)) {
 105  0
             if (!canTakeRequestedAction(document, KEWConstants.ACTION_REQUEST_APPROVE_REQ, user)) {
 106  0
                 documentActions.remove(KRADConstants.KUALI_ACTION_CAN_APPROVE);
 107  0
                 documentActions.remove(KRADConstants.KUALI_ACTION_CAN_DISAPPROVE);
 108  
             }
 109  
         }
 110  
 
 111  0
         if (!canSendAnyTypeAdHocRequests(document, user)) {
 112  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_ADD_ADHOC_REQUESTS);
 113  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_SEND_ADHOC_REQUESTS);
 114  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI);
 115  
         }
 116  
 
 117  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI)
 118  
                 && !canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 119  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_SEND_NOTE_FYI);
 120  
         }
 121  
 
 122  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_ANNOTATE)
 123  
                 && !documentActions.contains(KRADConstants.KUALI_ACTION_CAN_EDIT)) {
 124  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_ANNOTATE);
 125  
         }
 126  
 
 127  0
         if (documentActions.contains(KRADConstants.KUALI_ACTION_CAN_EDIT__DOCUMENT_OVERVIEW)
 128  
                 && !canEditDocumentOverview(document, user)) {
 129  0
             documentActions.remove(KRADConstants.KUALI_ACTION_CAN_EDIT__DOCUMENT_OVERVIEW);
 130  
         }
 131  
 
 132  0
         return documentActions;
 133  
     }
 134  
 
 135  
     public final boolean canInitiate(String documentTypeName, Person user) {
 136  0
                 String nameSpaceCode = KRADConstants.KUALI_RICE_SYSTEM_NAMESPACE;
 137  0
                 Map<String, String> permissionDetails = new HashMap<String, String>();
 138  0
                 permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME,
 139  
                                 documentTypeName);
 140  0
                 return getPermissionService().isAuthorizedByTemplateName(
 141  
                                 user.getPrincipalId(), nameSpaceCode,
 142  
                                 KimConstants.PermissionTemplateNames.INITIATE_DOCUMENT,
 143  
                                 permissionDetails, null);
 144  
         }
 145  
 
 146  
         public final boolean canReceiveAdHoc(Document document, Person user,
 147  
                         String actionRequestCode) {
 148  0
                 Map<String,String> additionalPermissionDetails = new HashMap<String, String>();
 149  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD, actionRequestCode);
 150  0
                 return isAuthorizedByTemplate(document,
 151  
                                 KRADConstants.KUALI_RICE_WORKFLOW_NAMESPACE,
 152  
                                 KimConstants.PermissionTemplateNames.AD_HOC_REVIEW_DOCUMENT,
 153  
                                 user.getPrincipalId(), additionalPermissionDetails, null );
 154  
         }
 155  
 
 156  
         public final boolean canOpen(Document document, Person user) {
 157  0
                 return isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 158  
                                 KimConstants.PermissionTemplateNames.OPEN_DOCUMENT, user
 159  
                                                 .getPrincipalId());
 160  
         }
 161  
 
 162  
         public final boolean canAddNoteAttachment(Document document,
 163  
                         String attachmentTypeCode, Person user) {
 164  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 165  0
                 if (attachmentTypeCode != null) {
 166  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 167  
                                         attachmentTypeCode);
 168  
                 }
 169  0
                 return isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 170  
                                 KimConstants.PermissionTemplateNames.ADD_NOTE_ATTACHMENT, user
 171  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 172  
         }
 173  
 
 174  
         public final boolean canDeleteNoteAttachment(Document document,
 175  
                         String attachmentTypeCode, String createdBySelfOnly, Person user) {
 176  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 177  0
                 if (attachmentTypeCode != null) {
 178  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 179  
                                         attachmentTypeCode);
 180  
                 }
 181  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.CREATED_BY_SELF,
 182  
                                 createdBySelfOnly);
 183  0
                 return isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 184  
                                 KimConstants.PermissionTemplateNames.DELETE_NOTE_ATTACHMENT,
 185  
                                 user.getPrincipalId(), additionalPermissionDetails, null);
 186  
         }
 187  
 
 188  
         public final boolean canViewNoteAttachment(Document document,
 189  
                         String attachmentTypeCode, Person user) {
 190  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 191  0
                 if (attachmentTypeCode != null) {
 192  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ATTACHMENT_TYPE_CODE,
 193  
                                         attachmentTypeCode);
 194  
                 }
 195  0
                 return isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 196  
                                 KimConstants.PermissionTemplateNames.VIEW_NOTE_ATTACHMENT, user
 197  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 198  
         }
 199  
         
 200  
         public final boolean canSendAdHocRequests(Document document,
 201  
                         String actionRequestCd, Person user) {
 202  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 203  0
                 if (actionRequestCd != null) {
 204  0
                         additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD,
 205  
                                         actionRequestCd);
 206  
                 }
 207  0
                 return isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 208  
                                 KimConstants.PermissionTemplateNames.SEND_AD_HOC_REQUEST, user
 209  
                                                 .getPrincipalId(), additionalPermissionDetails, null);
 210  
         }
 211  
         
 212  
         public boolean canEditDocumentOverview(Document document, Person user){
 213  0
                 return isAuthorizedByTemplate(document,
 214  
                                 KRADConstants.KRAD_NAMESPACE,
 215  
                                 KimConstants.PermissionTemplateNames.EDIT_DOCUMENT,
 216  
                                 user.getPrincipalId()) && this.isDocumentInitiator(document, user);
 217  
         }
 218  
         
 219  
         protected final boolean canSendAnyTypeAdHocRequests(Document document, Person user) {
 220  0
                 if (canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_FYI_REQ, user)) {
 221  0
                     RoutePath routePath = KewApiServiceLocator.getDocumentTypeService().getRoutePathForDocumentTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
 222  0
                     Process process = routePath.getPrimaryProcess();
 223  0
                     if (process != null) {
 224  0
                         if (process.getInitialRouteNode() == null) {
 225  0
                             return false;
 226  
                         }
 227  
                     } else {
 228  0
                         return false;
 229  
                     }
 230  0
                     return true;
 231  0
                 } else if(canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, user)){
 232  0
                         return true;
 233  
                 }
 234  0
                 return canSendAdHocRequests(document, KEWConstants.ACTION_REQUEST_APPROVE_REQ, user);
 235  
         }
 236  
 
 237  
         protected boolean canTakeRequestedAction(Document document,
 238  
                         String actionRequestCode, Person user) {
 239  0
                 Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
 240  0
                 additionalPermissionDetails.put(KimConstants.AttributeConstants.ACTION_REQUEST_CD,
 241  
                                 actionRequestCode);
 242  0
                 return isAuthorizedByTemplate(document, KRADConstants.KRAD_NAMESPACE,
 243  
                                 KimConstants.PermissionTemplateNames.TAKE_REQUESTED_ACTION,
 244  
                                 user.getPrincipalId(), additionalPermissionDetails, null);
 245  
         }
 246  
 
 247  
         @Override
 248  
         protected void addPermissionDetails(Object dataObject,
 249  
                         Map<String, String> attributes) {
 250  0
                 super.addPermissionDetails(dataObject, attributes);
 251  0
                 if (dataObject instanceof Document) {
 252  0
                         addStandardAttributes((Document) dataObject, attributes);
 253  
                 }
 254  0
         }
 255  
 
 256  
         @Override
 257  
         protected void addRoleQualification(Object dataObject,
 258  
                         Map<String, String> attributes) {
 259  0
                 super.addRoleQualification(dataObject, attributes);
 260  0
                 if (dataObject instanceof Document) {
 261  0
                         addStandardAttributes((Document) dataObject, attributes);
 262  
                 }
 263  0
         }
 264  
 
 265  
         protected void addStandardAttributes(Document document,
 266  
                         Map<String, String> attributes) {
 267  0
                 WorkflowDocument wd = document.getDocumentHeader()
 268  
                                 .getWorkflowDocument();
 269  0
                 attributes.put(KimConstants.AttributeConstants.DOCUMENT_NUMBER, document
 270  
                                 .getDocumentNumber());
 271  0
                 attributes.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, wd.getDocumentTypeName());
 272  0
                 if (wd.isInitiated() || wd.isSaved()) {
 273  0
                         attributes.put(KimConstants.AttributeConstants.ROUTE_NODE_NAME,
 274  
                                         PRE_ROUTING_ROUTE_NAME);
 275  
                 } else {
 276  0
                         attributes.put(KimConstants.AttributeConstants.ROUTE_NODE_NAME, KRADServiceLocatorWeb.getWorkflowDocumentService().getCurrentRouteNodeNames(wd));
 277  
                 }
 278  0
                 attributes.put(KimConstants.AttributeConstants.ROUTE_STATUS_CODE, wd.getStatus().getCode());
 279  0
         }
 280  
         
 281  
         protected boolean isDocumentInitiator(Document document, Person user) {
 282  0
         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
 283  0
         return workflowDocument.getInitiatorPrincipalId().equalsIgnoreCase(user.getPrincipalId());
 284  
     }
 285  
 
 286  
 }