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.");
}
}
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;
}
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);
}
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;
}
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()));
}
}
Aggregations