Search in sources :

Example 36 with PermissionDeniedException

use of org.jbpm.services.task.exception.PermissionDeniedException in project jbpm by kiegroup.

the class LifeCycleBaseTest method testForwardFromReservedWithIncorrectUser.

@Test
public void testForwardFromReservedWithIncorrectUser() throws Exception {
    // One potential owner, should go straight to state Reserved
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ],businessAdministrators = [ new User('Administrator') ], }),";
    str += "name =  'This is my task name' })";
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    // Claim and Reserved
    taskService.claim(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.Reserved, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    // Check was not delegated
    PermissionDeniedException denied = null;
    try {
        taskService.forward(taskId, "Bobba Fet", "Tony Stark");
    } catch (PermissionDeniedException e) {
        denied = e;
    }
    assertNotNull("Should get permissed denied exception", denied);
    Task task2 = taskService.getTaskById(taskId);
    User user = createUser("Darth Vader");
    assertTrue(task2.getPeopleAssignments().getPotentialOwners().contains(user));
    user = createUser("Tony Stark");
    assertFalse(task2.getPeopleAssignments().getPotentialOwners().contains(user));
    assertEquals("Darth Vader", task2.getTaskData().getActualOwner().getId());
    assertEquals(Status.Reserved, task2.getTaskData().getStatus());
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) User(org.kie.api.task.model.User) StringReader(java.io.StringReader) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) Test(org.junit.Test)

Example 37 with PermissionDeniedException

use of org.jbpm.services.task.exception.PermissionDeniedException in project jbpm by kiegroup.

the class MVELLifeCycleManager method assignOwnerAndStatus.

/**
 * This method will potentially assign the actual owner of this TaskData and set the status
 * of the data.
 * <li>If there is only 1 potential owner, and it is a <code>User</code>, that will become the actual
 * owner of the TaskData and the status will be set to <code>Status.Reserved</code>.</li>
 * <li>f there is only 1 potential owner, and it is a <code>Group</code>,  no owner will be assigned
 * and the status will be set to <code>Status.Ready</code>.</li>
 * <li>If there are more than 1 potential owners, the status will be set to <code>Status.Ready</code>.</li>
 * <li>otherwise, the task data will be unchanged</li>
 *
 * @param taskdata - task data
 * @param potentialOwners - list of potential owners
 * @return current status of task data
 */
public static Status assignOwnerAndStatus(InternalTaskData taskData, List<OrganizationalEntity> potentialOwners) {
    if (taskData.getStatus() != Status.Created) {
        throw new PermissionDeniedException("Can only assign task owner if status is Created!");
    }
    Status assignedStatus = null;
    if (potentialOwners.size() == 1) {
        // if there is a single potential owner, assign and set status to Reserved
        OrganizationalEntity potentialOwner = potentialOwners.get(0);
        // if there is a single potential user owner, assign and set status to Reserved
        if (potentialOwner instanceof User) {
            taskData.setActualOwner((User) potentialOwner);
            assignedStatus = Status.Reserved;
        }
        // If there is a group set as potentialOwners, set the status to Ready ??
        if (potentialOwner instanceof Group) {
            assignedStatus = Status.Ready;
        }
    } else if (potentialOwners.size() > 1) {
        // multiple potential owners, so set to Ready so one can claim.
        assignedStatus = Status.Ready;
    } else {
    // @TODO we have no potential owners
    }
    if (assignedStatus != null) {
        taskData.setStatus(assignedStatus);
    } else {
        // status wasn't assigned, so just return the currrent status
        assignedStatus = taskData.getStatus();
    }
    return assignedStatus;
}
Also used : Status(org.kie.api.task.model.Status) Group(org.kie.api.task.model.Group) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException)

Example 38 with PermissionDeniedException

use of org.jbpm.services.task.exception.PermissionDeniedException in project jbpm by kiegroup.

the class MVELLifeCycleManager method evalCommand.

void evalCommand(final Operation operation, final List<OperationCommand> commands, final Task task, final User user, final OrganizationalEntity targetEntity, List<String> groupIds, OrganizationalEntity... entities) throws PermissionDeniedException {
    boolean statusMatched = false;
    final TaskData taskData = task.getTaskData();
    for (OperationCommand command : commands) {
        // first find out if we have a matching status
        if (command.getStatus() != null) {
            for (Status status : command.getStatus()) {
                if (task.getTaskData().getStatus() == status) {
                    statusMatched = true;
                    // next find out if the user can execute this doOperation
                    if (!isAllowed(command, task, user, groupIds)) {
                        String errorMessage = "User '" + user + "' does not have permissions to execute operation '" + operation + "' on task id " + task.getId();
                        throw new PermissionDeniedException(errorMessage);
                    }
                    commands(command, task, user, targetEntity, entities);
                } else {
                    logger.debug("No match on status for task {} :status {}  != {}", task.getId(), task.getTaskData().getStatus(), status);
                }
            }
        }
        if (command.getPreviousStatus() != null) {
            for (Status status : command.getPreviousStatus()) {
                if (taskData.getPreviousStatus() == status) {
                    statusMatched = true;
                    // next find out if the user can execute this doOperation
                    if (!isAllowed(command, task, user, groupIds)) {
                        String errorMessage = "User '" + user + "' does not have permissions to execute operation '" + operation + "' on task id " + task.getId();
                        throw new PermissionDeniedException(errorMessage);
                    }
                    commands(command, task, user, targetEntity, entities);
                } else {
                    logger.debug("No match on previous status for task {} :status {}  != {}", task.getId(), task.getTaskData().getStatus(), status);
                }
            }
        }
        if (!command.isGroupTargetEntityAllowed() && targetEntity instanceof Group) {
            String errorMessage = "User '" + user + "' was unable to execute operation '" + operation + "' on task id " + task.getId() + " due to 'target entity cannot be group'";
            throw new PermissionDeniedException(errorMessage);
        }
    }
    if (!statusMatched) {
        String errorMessage = "User '" + user + "' was unable to execute operation '" + operation + "' on task id " + task.getId() + " due to a no 'current status' match";
        throw new PermissionDeniedException(errorMessage);
    }
}
Also used : Status(org.kie.api.task.model.Status) Group(org.kie.api.task.model.Group) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) TaskData(org.kie.api.task.model.TaskData)

Example 39 with PermissionDeniedException

use of org.jbpm.services.task.exception.PermissionDeniedException in project jbpm by kiegroup.

the class UserTaskServiceImplTest method testUpdateTaskPermissionDenied.

@Test
public void testUpdateTaskPermissionDenied() {
    processInstanceId = processService.startProcess(deploymentUnit.getIdentifier(), "org.jbpm.writedocument");
    assertNotNull(processInstanceId);
    List<Long> taskIds = runtimeDataService.getTasksByProcessInstanceId(processInstanceId);
    assertNotNull(taskIds);
    assertEquals(1, taskIds.size());
    Long taskId = taskIds.get(0);
    UserTaskInstanceDesc task = runtimeDataService.getTaskById(taskId);
    assertNotNull(task);
    assertEquals("Write a Document", task.getName());
    try {
        ((org.jbpm.kie.services.impl.model.UserTaskInstanceDesc) task).setName("updated");
        userTaskService.updateTask(taskId, "john", task, null, null);
        fail("John is not admin nor potential owner");
    } catch (PermissionDeniedException e) {
    // expected
    }
    task = runtimeDataService.getTaskById(taskId);
    assertNotNull(task);
    assertEquals(Status.Reserved.toString(), task.getStatus());
    assertEquals("Write a Document", task.getName());
    assertEquals(9, task.getPriority().intValue());
}
Also used : PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) UserTaskInstanceDesc(org.jbpm.services.api.model.UserTaskInstanceDesc) AbstractKieServicesBaseTest(org.jbpm.kie.test.util.AbstractKieServicesBaseTest) Test(org.junit.Test)

Example 40 with PermissionDeniedException

use of org.jbpm.services.task.exception.PermissionDeniedException in project jbpm by kiegroup.

the class UserTaskServiceImpl method release.

@Override
public void release(String deploymentId, Long taskId, String userId) {
    UserTaskInstanceDesc task = dataService.getTaskById(taskId);
    validateTask(deploymentId, taskId, task);
    RuntimeManager manager = getRuntimeManager(task);
    if (manager == null) {
        logger.warn("Cannot find runtime manager for task {}", taskId);
        return;
    }
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(task.getProcessInstanceId()));
    try {
        TaskService taskService = engine.getTaskService();
        // perform actual operation
        taskService.release(taskId, userId);
    } catch (PermissionDeniedException e) {
        throw new TaskNotFoundException(e.getMessage());
    } finally {
        disposeRuntimeEngine(manager, engine);
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) InternalTaskService(org.kie.internal.task.api.InternalTaskService) UserTaskService(org.jbpm.services.api.UserTaskService) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) UserTaskInstanceDesc(org.jbpm.services.api.model.UserTaskInstanceDesc)

Aggregations

PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)48 Task (org.kie.api.task.model.Task)30 InternalTask (org.kie.internal.task.api.model.InternalTask)18 Test (org.junit.Test)17 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)17 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)16 InternalTaskService (org.kie.internal.task.api.InternalTaskService)16 TaskNotFoundException (org.jbpm.services.api.TaskNotFoundException)15 UserTaskService (org.jbpm.services.api.UserTaskService)15 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)15 TaskService (org.kie.api.task.TaskService)15 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)15 StringReader (java.io.StringReader)14 TaskContext (org.jbpm.services.task.commands.TaskContext)9 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)6 TaskEventSupport (org.jbpm.services.task.events.TaskEventSupport)5 TaskSummary (org.kie.api.task.model.TaskSummary)5 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)5 InternalTaskData (org.kie.internal.task.api.model.InternalTaskData)5 Content (org.kie.api.task.model.Content)4