Search in sources :

Example 21 with PermissionDeniedException

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

the class LifeCycleBaseTest method testFailWithIncorrectUser.

@Test
public void testFailWithIncorrectUser() {
    // 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();
    // Go straight from Ready to Inprogress
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    // Should not fail as wrong user
    PermissionDeniedException denied = null;
    try {
        taskService.fail(taskId, "Bobba Fet", null);
    } catch (PermissionDeniedException e) {
        denied = e;
    }
    assertNotNull("Should get permissed denied exception", denied);
    Task task2 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task2.getTaskData().getStatus());
    assertEquals("Darth Vader", task2.getTaskData().getActualOwner().getId());
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) StringReader(java.io.StringReader) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) Test(org.junit.Test)

Example 22 with PermissionDeniedException

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

the class LifeCycleLocalWithRuleServiceTest method testCompleteTaskWithCheckByRule.

@Test
public void testCompleteTaskWithCheckByRule() {
    // One potential owner, should go straight to state Reserved
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { workItemId = 1 } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('mary')],businessAdministrators = [ new User('Administrator') ], }),";
    str += "description =  'This is my description', ";
    str += "subject = 'This is my subject', ";
    str += "name = 'This is my task name' })";
    Task task = (Task) TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    List<TaskSummary> tasks = taskService.getTasksOwned("mary", "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    long taskId = tasks.get(0).getId();
    taskService.start(taskId, "mary");
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("approved", "false");
    try {
        taskService.complete(taskId, "mary", data);
        fail("Task should not be created due to rule violation");
    } catch (PermissionDeniedException e) {
        assertTrue(e.getMessage().indexOf("Mary is not allowed to complete task with approved false") != -1);
    }
}
Also used : Task(org.kie.api.task.model.Task) HashMap(java.util.HashMap) StringReader(java.io.StringReader) TaskSummary(org.kie.api.task.model.TaskSummary) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) Test(org.junit.Test)

Example 23 with PermissionDeniedException

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

the class MVELLifeCycleManager method taskOperation.

public void taskOperation(final Operation operation, final long taskId, final String userId, final String targetEntityId, final Map<String, Object> data, List<String> groupIds, OrganizationalEntity... entities) throws TaskException {
    try {
        final List<OperationCommand> commands = operations.get(operation);
        Task task = persistenceContext.findTask(taskId);
        if (task == null) {
            String errorMessage = "Task '" + taskId + "' not found";
            throw new PermissionDeniedException(errorMessage);
        }
        String deploymentId = (String) context.get(EnvironmentName.DEPLOYMENT_ID);
        if (deploymentId != null && !deploymentId.equals(task.getTaskData().getDeploymentId())) {
            throw new IllegalStateException("Task instance " + task.getId() + " is owned by another deployment expected " + task.getTaskData().getDeploymentId() + " found " + deploymentId);
        }
        // automatically load task variables on each operation if the event manager is activated
        if (EventManagerProvider.getInstance().isActive()) {
            taskContentService.loadTaskVariables(task);
        }
        User user = persistenceContext.findUser(userId);
        OrganizationalEntity targetEntity = null;
        if (targetEntityId != null && !targetEntityId.equals("")) {
            targetEntity = persistenceContext.findOrgEntity(targetEntityId);
        }
        getExecutionErrorHandler().processing(task);
        switch(operation) {
            case Activate:
                {
                    taskEventSupport.fireBeforeTaskActivated(task, context);
                    break;
                }
            case Claim:
                {
                    taskEventSupport.fireBeforeTaskClaimed(task, context);
                    break;
                }
            case Complete:
                {
                    taskEventSupport.fireBeforeTaskCompleted(task, context);
                    break;
                }
            case Delegate:
                {
                    taskEventSupport.fireBeforeTaskDelegated(task, context);
                    break;
                }
            case Exit:
                {
                    taskEventSupport.fireBeforeTaskExited(task, context);
                    break;
                }
            case Fail:
                {
                    if (data != null) {
                        FaultData faultData = ContentMarshallerHelper.marshalFault(task, data, null);
                        Content content = TaskModelProvider.getFactory().newContent();
                        ((InternalContent) content).setContent(faultData.getContent());
                        persistenceContext.persistContent(content);
                        persistenceContext.setFaultToTask(content, faultData, task);
                    }
                    taskEventSupport.fireBeforeTaskFailed(task, context);
                    break;
                }
            case Forward:
                {
                    taskEventSupport.fireBeforeTaskForwarded(task, context);
                    break;
                }
            case Nominate:
                {
                    taskEventSupport.fireBeforeTaskNominated(task, context);
                    break;
                }
            case Release:
                {
                    taskEventSupport.fireBeforeTaskReleased(task, context);
                    break;
                }
            case Resume:
                {
                    taskEventSupport.fireBeforeTaskResumed(task, context);
                    break;
                }
            case Skip:
                {
                    taskEventSupport.fireBeforeTaskSkipped(task, context);
                    break;
                }
            case Start:
                {
                    taskEventSupport.fireBeforeTaskStarted(task, context);
                    break;
                }
            case Stop:
                {
                    taskEventSupport.fireBeforeTaskStopped(task, context);
                    break;
                }
            case Suspend:
                {
                    taskEventSupport.fireBeforeTaskSuspended(task, context);
                    break;
                }
        }
        evalCommand(operation, commands, task, user, targetEntity, groupIds, entities);
        persistenceContext.updateTask(task);
        switch(operation) {
            case Activate:
                {
                    taskEventSupport.fireAfterTaskActivated(task, context);
                    break;
                }
            case Claim:
                {
                    taskEventSupport.fireAfterTaskClaimed(task, context);
                    break;
                }
            case Complete:
                {
                    if (data != null) {
                        taskContentService.addOutputContent(taskId, data);
                    }
                    taskEventSupport.fireAfterTaskCompleted(task, context);
                    break;
                }
            case Delegate:
                {
                    // This is a really bad hack to execut the correct behavior
                    ((InternalTaskData) task.getTaskData()).setStatus(Status.Reserved);
                    taskEventSupport.fireAfterTaskDelegated(task, context);
                    break;
                }
            case Exit:
                {
                    taskEventSupport.fireAfterTaskExited(task, context);
                    break;
                }
            case Fail:
                {
                    taskEventSupport.fireAfterTaskFailed(task, context);
                    break;
                }
            case Forward:
                {
                    invokeAssignmentService(task, context, userId);
                    taskEventSupport.fireAfterTaskForwarded(task, context);
                    break;
                }
            case Nominate:
                {
                    invokeAssignmentService(task, context, userId);
                    taskEventSupport.fireAfterTaskNominated(task, context);
                    break;
                }
            case Release:
                {
                    invokeAssignmentService(task, context, userId);
                    taskEventSupport.fireAfterTaskReleased(task, context);
                    break;
                }
            case Resume:
                {
                    taskEventSupport.fireAfterTaskResumed(task, context);
                    break;
                }
            case Start:
                {
                    taskEventSupport.fireAfterTaskStarted(task, context);
                    break;
                }
            case Skip:
                {
                    taskEventSupport.fireAfterTaskSkipped(task, context);
                    break;
                }
            case Stop:
                {
                    taskEventSupport.fireAfterTaskStopped(task, context);
                    break;
                }
            case Suspend:
                {
                    taskEventSupport.fireAfterTaskSuspended(task, context);
                    break;
                }
        }
        getExecutionErrorHandler().processed(task);
    } catch (RuntimeException re) {
        throw re;
    }
}
Also used : Task(org.kie.api.task.model.Task) User(org.kie.api.task.model.User) FaultData(org.kie.internal.task.api.model.FaultData) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) Content(org.kie.api.task.model.Content) InternalContent(org.kie.internal.task.api.model.InternalContent) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException)

Example 24 with PermissionDeniedException

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

the class CompleteTaskCommand method execute.

public Void execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    doCallbackUserOperation(userId, context, true);
    groupIds = doUserGroupCallbackOperation(userId, null, context);
    context.set("local:groups", groupIds);
    Task task = context.getTaskQueryService().getTaskInstanceById(taskId);
    if (task == null) {
        throw new PermissionDeniedException("Task '" + taskId + "' not found");
    }
    context.loadTaskVariables(task);
    Map<String, Object> outputdata = task.getTaskData().getTaskOutputVariables();
    if (outputdata != null) {
        // if there are data given with completion, merged them into existing outputs
        if (data != null) {
            outputdata.putAll(data);
        }
        // since output data was non null make it the actual data
        data = outputdata;
    }
    context.getTaskRuleService().executeRules(task, userId, data, TaskRuleService.COMPLETE_TASK_SCOPE);
    ((InternalTaskData) task.getTaskData()).setTaskOutputVariables(data);
    TaskInstanceService instanceService = context.getTaskInstanceService();
    instanceService.complete(taskId, userId, data);
    return null;
}
Also used : Task(org.kie.api.task.model.Task) TaskInstanceService(org.kie.internal.task.api.TaskInstanceService) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException)

Example 25 with PermissionDeniedException

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

the class ListTaskNotificationsCommand method execute.

@Override
public List<TaskNotification> execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    Language lang = factory.newLanguage();
    lang.setMapkey("en-UK");
    Task task = context.getTaskQueryService().getTaskInstanceById(taskId);
    if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context)) {
        throw new PermissionDeniedException("User " + userId + " is not business admin of task " + taskId);
    }
    Deadlines deadlines = ((InternalTask) task).getDeadlines();
    List<TaskNotification> notificationsNotStarted = deadlines.getStartDeadlines().stream().filter(d -> !d.getEscalations().isEmpty() && !d.getEscalations().get(0).getNotifications().isEmpty()).map(d -> {
        Notification n = d.getEscalations().get(0).getNotifications().get(0);
        EmailNotificationHeader email = ((EmailNotification) n).getEmailHeaders().get(lang);
        return new TaskNotificationImpl(d.getId(), get(n.getNames()), email.getSubject(), email.getBody(), d.getDate(), n.getRecipients(), n.getBusinessAdministrators(), !d.isEscalated());
    }).collect(Collectors.toList());
    List<TaskNotification> notificationsNotCompleted = deadlines.getEndDeadlines().stream().filter(d -> !d.getEscalations().isEmpty() && !d.getEscalations().get(0).getNotifications().isEmpty()).map(d -> {
        Notification n = d.getEscalations().get(0).getNotifications().get(0);
        EmailNotificationHeader email = ((EmailNotification) n).getEmailHeaders().get(lang);
        return new TaskNotificationImpl(d.getId(), get(n.getNames()), email.getSubject(), email.getBody(), d.getDate(), n.getRecipients(), n.getBusinessAdministrators(), !d.isEscalated());
    }).collect(Collectors.toList());
    List<TaskNotification> result = new ArrayList<>();
    result.addAll(notificationsNotStarted);
    result.addAll(notificationsNotCompleted);
    if (activeOnly) {
        logger.debug("Removing already completed deadlines from the result");
        result = result.stream().filter(t -> t.isActive()).collect(Collectors.toList());
    }
    return result;
}
Also used : Logger(org.slf4j.Logger) Task(org.kie.api.task.model.Task) EmailNotificationHeader(org.kie.internal.task.api.model.EmailNotificationHeader) LoggerFactory(org.slf4j.LoggerFactory) I18NText(org.kie.api.task.model.I18NText) TaskModelProvider(org.kie.internal.task.api.TaskModelProvider) TaskNotification(org.jbpm.services.api.admin.TaskNotification) EmailNotification(org.kie.internal.task.api.model.EmailNotification) Collectors(java.util.stream.Collectors) Language(org.kie.internal.task.api.model.Language) ArrayList(java.util.ArrayList) TaskNotificationImpl(org.jbpm.kie.services.impl.admin.TaskNotificationImpl) UserGroupCallbackTaskCommand(org.jbpm.services.task.commands.UserGroupCallbackTaskCommand) List(java.util.List) Context(org.kie.api.runtime.Context) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskModelFactory(org.kie.internal.task.api.TaskModelFactory) InternalTask(org.kie.internal.task.api.model.InternalTask) TaskContext(org.jbpm.services.task.commands.TaskContext) Deadlines(org.kie.internal.task.api.model.Deadlines) Notification(org.kie.internal.task.api.model.Notification) Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) TaskContext(org.jbpm.services.task.commands.TaskContext) EmailNotificationHeader(org.kie.internal.task.api.model.EmailNotificationHeader) InternalTask(org.kie.internal.task.api.model.InternalTask) ArrayList(java.util.ArrayList) EmailNotification(org.kie.internal.task.api.model.EmailNotification) TaskNotification(org.jbpm.services.api.admin.TaskNotification) EmailNotification(org.kie.internal.task.api.model.EmailNotification) Notification(org.kie.internal.task.api.model.Notification) TaskNotificationImpl(org.jbpm.kie.services.impl.admin.TaskNotificationImpl) Deadlines(org.kie.internal.task.api.model.Deadlines) Language(org.kie.internal.task.api.model.Language) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskNotification(org.jbpm.services.api.admin.TaskNotification)

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