Search in sources :

Example 26 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-repository by Alfresco.

the class ResetPasswordServiceImpl method validateIdAndKey.

/**
 * This method ensures that the id refers to an in-progress workflow and that the key matches
 * that stored in the workflow.
 *
 * @throws WebScriptException a 404 if any of the above is not true.
 */
private void validateIdAndKey(String id, String key, String userId) {
    ParameterCheck.mandatory("id", id);
    ParameterCheck.mandatory("key", key);
    ParameterCheck.mandatory("userId", userId);
    WorkflowInstance workflowInstance = null;
    try {
        workflowInstance = workflowService.getWorkflowById(id);
    } catch (WorkflowException ignored) {
    // Intentionally empty.
    }
    if (workflowInstance == null) {
        throw new ResetPasswordWorkflowNotFoundException("The reset password workflow instance with the id [" + id + "] is not found.");
    }
    String recoveredKey;
    String username;
    if (workflowInstance.isActive()) {
        // If the workflow is active we will be able to read the path properties.
        Map<QName, Serializable> pathProps = workflowService.getPathProperties(id);
        username = (String) pathProps.get(WorkflowModelResetPassword.WF_PROP_USERNAME);
        recoveredKey = (String) pathProps.get(WorkflowModelResetPassword.WF_PROP_KEY);
    } else {
        throw new InvalidResetPasswordWorkflowException("The reset password workflow instance with the id [" + id + "] is not active (it might be expired or has already been used).");
    }
    if (username == null || recoveredKey == null || !recoveredKey.equals(key)) {
        String msg;
        if (username == null) {
            msg = "The recovered user name is null for the reset password workflow instance with the id [" + id + "]";
        } else if (recoveredKey == null) {
            msg = "The recovered key is null for the reset password workflow instance with the id [" + id + "]";
        } else {
            msg = "The recovered key [" + recoveredKey + "] does not match the given workflow key [" + key + "] for the reset password workflow instance with the id [" + id + "]";
        }
        throw new InvalidResetPasswordWorkflowException(msg);
    } else if (!username.equals(userId)) {
        throw new InvalidResetPasswordWorkflowException("The given user id [" + userId + "] does not match the person's user id [" + username + "] who requested the password reset.");
    }
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) WorkflowException(org.alfresco.service.cmr.workflow.WorkflowException) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance)

Example 27 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.

the class WorkflowInstanceDelete method canUserEndWorkflow.

/**
 * Determines if the current user can cancel or delete the
 * workflow instance with the given id. Throws a WebscriptException
 * with status-code 404 if workflow-instance to delete wasn't found.
 *
 * @param instanceId The id of the workflow instance to check
 * @return true if the user can end the workflow, false otherwise
 */
private boolean canUserEndWorkflow(String instanceId) {
    boolean canEnd = false;
    // get the initiator
    WorkflowInstance wi = workflowService.getWorkflowById(instanceId);
    if (wi == null) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "The workflow instance to delete/cancel with id " + instanceId + " doesn't exist: ");
    }
    String currentUserName = authenticationService.getCurrentUserName();
    // ALF-17405: Admin can always delete/cancel workflows, regardless of the initiator
    if (authorityService.isAdminAuthority(currentUserName)) {
        canEnd = true;
    } else {
        String ownerName = null;
        // Determine if the current user is the initiator of the workflow.
        // Get the username of the initiator.
        NodeRef initiator = wi.getInitiator();
        if (initiator != null && nodeService.exists(initiator)) {
            ownerName = (String) nodeService.getProperty(initiator, ContentModel.PROP_USERNAME);
        } else {
            /*
                 * Fix for MNT-14411 : Re-created user can't cancel created task.
                 * If owner value can't be found on workflow properties
                 * because initiator nodeRef no longer exists get owner from
                 * initiatorhome nodeRef owner property.
                 */
            WorkflowTask startTask = workflowService.getStartTask(wi.getId());
            Map<QName, Serializable> props = startTask.getProperties();
            ownerName = (String) props.get(ContentModel.PROP_OWNER);
            if (ownerName == null) {
                NodeRef initiatorHomeNodeRef = (NodeRef) props.get(QName.createQName("", WorkflowConstants.PROP_INITIATOR_HOME));
                if (initiatorHomeNodeRef != null) {
                    ownerName = (String) nodeService.getProperty(initiatorHomeNodeRef, ContentModel.PROP_OWNER);
                }
            }
        }
        // if the current user started the workflow allow the cancel action
        if (currentUserName.equals(ownerName)) {
            canEnd = true;
        }
    }
    return canEnd;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) QName(org.alfresco.service.namespace.QName) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance)

Example 28 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.

the class WorkflowInstanceDiagramGet method execute.

@Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    // getting workflow instance id from request parameters
    String workflowInstanceId = params.get("workflow_instance_id");
    WorkflowInstance workflowInstance = workflowService.getWorkflowById(workflowInstanceId);
    // workflow instance was not found -> return 404
    if (workflowInstance == null) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find workflow instance with id: " + workflowInstanceId);
    }
    // check whether there is a diagram available
    if (!workflowService.hasWorkflowImage(workflowInstanceId)) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find diagram for workflow instance with id: " + workflowInstanceId);
    }
    // copy image data into temporary file
    File file = TempFileProvider.createTempFile("workflow-diagram-", ".png");
    InputStream imageData = workflowService.getWorkflowImage(workflowInstanceId);
    OutputStream os = new FileOutputStream(file);
    FileCopyUtils.copy(imageData, os);
    // stream temporary file back to client
    streamContent(req, res, file);
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance) File(java.io.File)

Example 29 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.

the class WorkflowInstancesForNodeGet method buildModel.

@Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    // get nodeRef from request
    NodeRef nodeRef = new NodeRef(params.get(PARAM_STORE_TYPE), params.get(PARAM_STORE_ID), params.get(PARAM_NODE_ID));
    // list all active workflows for nodeRef
    List<WorkflowInstance> workflows = workflowService.getWorkflowsForContent(nodeRef, true);
    List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(workflows.size());
    for (WorkflowInstance workflow : workflows) {
        results.add(modelBuilder.buildSimple(workflow));
    }
    Map<String, Object> model = new HashMap<String, Object>();
    // build the model for ftl
    model.put("workflowInstances", results);
    return model;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance) Map(java.util.Map) HashMap(java.util.HashMap)

Example 30 with WorkflowInstance

use of org.alfresco.service.cmr.workflow.WorkflowInstance in project alfresco-remote-api by Alfresco.

the class AbstractWorkflowRestApiTest method testWorkflowInstanceDeleteAsAdministrator.

public void testWorkflowInstanceDeleteAsAdministrator() throws Exception {
    // Start workflow as USER1
    personManager.setUser(USER1);
    WorkflowDefinition adhocDef = workflowService.getDefinitionByName(getAdhocWorkflowDefinitionName());
    Map<QName, Serializable> params = new HashMap<QName, Serializable>();
    params.put(WorkflowModel.ASSOC_ASSIGNEE, personManager.get(USER2));
    Date dueDate = new Date();
    params.put(WorkflowModel.PROP_DUE_DATE, dueDate);
    params.put(WorkflowModel.PROP_PRIORITY, 1);
    params.put(WorkflowModel.ASSOC_PACKAGE, packageRef);
    params.put(WorkflowModel.PROP_CONTEXT, packageRef);
    WorkflowPath adhocPath = workflowService.startWorkflow(adhocDef.getId(), params);
    WorkflowTask startTask = workflowService.getTasksForWorkflowPath(adhocPath.getId()).get(0);
    startTask = workflowService.endTask(startTask.getId(), null);
    WorkflowInstance adhocInstance = startTask.getPath().getInstance();
    // Run next request as admin
    String admin = authenticationComponent.getDefaultAdministratorUserNames().iterator().next();
    AuthenticationUtil.setFullyAuthenticatedUser(admin);
    sendRequest(new DeleteRequest(URL_WORKFLOW_INSTANCES + "/" + adhocInstance.getId()), Status.STATUS_OK);
    WorkflowInstance instance = workflowService.getWorkflowById(adhocInstance.getId());
    if (instance != null) {
        assertFalse("The deleted workflow is still active!", instance.isActive());
    }
    List<WorkflowInstance> instances = workflowService.getActiveWorkflows(adhocInstance.getDefinition().getId());
    for (WorkflowInstance activeInstance : instances) {
        assertFalse(adhocInstance.getId().equals(activeInstance.getId()));
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) WorkflowDefinition(org.alfresco.service.cmr.workflow.WorkflowDefinition) WorkflowPath(org.alfresco.service.cmr.workflow.WorkflowPath) WorkflowTask(org.alfresco.service.cmr.workflow.WorkflowTask) WorkflowInstance(org.alfresco.service.cmr.workflow.WorkflowInstance) DeleteRequest(org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest) Date(java.util.Date)

Aggregations

WorkflowInstance (org.alfresco.service.cmr.workflow.WorkflowInstance)56 WorkflowDefinition (org.alfresco.service.cmr.workflow.WorkflowDefinition)26 HashMap (java.util.HashMap)22 WorkflowTask (org.alfresco.service.cmr.workflow.WorkflowTask)19 WorkflowPath (org.alfresco.service.cmr.workflow.WorkflowPath)16 QName (org.alfresco.service.namespace.QName)16 Serializable (java.io.Serializable)15 Date (java.util.Date)15 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)15 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)13 Test (org.junit.Test)13 Map (java.util.Map)11 NodeRef (org.alfresco.service.cmr.repository.NodeRef)10 ArrayList (java.util.ArrayList)9 WorkflowException (org.alfresco.service.cmr.workflow.WorkflowException)7 WorkflowNode (org.alfresco.service.cmr.workflow.WorkflowNode)5 Execution (org.activiti.engine.runtime.Execution)4 List (java.util.List)3 ActivitiException (org.activiti.engine.ActivitiException)3 MimetypeMap (org.alfresco.repo.content.MimetypeMap)3