Search in sources :

Example 11 with NotAuthorizedException

use of pro.taskana.exceptions.NotAuthorizedException in project taskana by Taskana.

the class WorkbasketDefinitionController method exportWorkbaskets.

@GetMapping
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<List<WorkbasketDefinition>> exportWorkbaskets(@RequestParam(required = false) String domain) {
    try {
        WorkbasketQuery workbasketQuery = workbasketService.createWorkbasketQuery();
        List<WorkbasketSummary> workbasketSummaryList = domain != null ? workbasketQuery.domainIn(domain).list() : workbasketQuery.list();
        List<WorkbasketDefinition> basketExports = new ArrayList<>();
        for (WorkbasketSummary summary : workbasketSummaryList) {
            Workbasket workbasket = workbasketService.getWorkbasket(summary.getId());
            basketExports.add(workbasketDefinitionMapper.toResource(workbasket));
        }
        return new ResponseEntity<>(basketExports, HttpStatus.OK);
    } catch (WorkbasketNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } catch (NotAuthorizedException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    }
}
Also used : WorkbasketDefinition(pro.taskana.rest.resource.WorkbasketDefinition) ResponseEntity(org.springframework.http.ResponseEntity) WorkbasketQuery(pro.taskana.WorkbasketQuery) ArrayList(java.util.ArrayList) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) Workbasket(pro.taskana.Workbasket) WorkbasketSummary(pro.taskana.WorkbasketSummary) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with NotAuthorizedException

use of pro.taskana.exceptions.NotAuthorizedException in project taskana by Taskana.

the class WorkbasketDefinitionController method importWorkbaskets.

/**
 * This method imports a <b>list of {@link WorkbasketDefinition}</b>. This does not exactly match the REST norm, but
 * we want to have an option to import all settings at once. When a logical equal (key and domain are equal)
 * workbasket already exists an update will be executed. Otherwise a new workbasket will be created.
 *
 * @param definitions the list of workbasket definitions which will be imported to the current system.
 * @return Return answer is determined by the status code: 200 - all good 400 - list state error (referring to non
 * existing id's) 401 - not authorized
 */
@PostMapping(path = "/import")
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<String> importWorkbaskets(@RequestBody List<WorkbasketDefinition> definitions) {
    try {
        // key: logical ID
        // value: system ID (in database)
        Map<String, String> systemIds = workbasketService.createWorkbasketQuery().list().stream().collect(Collectors.toMap(this::logicalId, WorkbasketSummary::getId));
        // key: old system ID
        // value: system ID
        Map<String, String> idConversion = new HashMap<>();
        // STEP 1: update or create workbaskets from the import
        for (WorkbasketDefinition definition : definitions) {
            WorkbasketResource res = definition.workbasketResource;
            Workbasket workbasket;
            String oldId = res.workbasketId;
            if (systemIds.containsKey(logicalId(res))) {
                res.workbasketId = systemIds.get(logicalId(res));
                workbasket = workbasketService.updateWorkbasket(workbasketMapper.toModel(res));
            } else {
                res.workbasketId = null;
                workbasket = workbasketService.createWorkbasket(workbasketMapper.toModel(res));
            }
            res.workbasketId = oldId;
            // simply delete all existing accessItems and create new ones.
            for (WorkbasketAccessItem accessItem : workbasketService.getWorkbasketAccessItems(workbasket.getId())) {
                workbasketService.deleteWorkbasketAccessItem(accessItem.getId());
            }
            for (WorkbasketAccessItemResource authorization : definition.authorizations) {
                workbasketService.createWorkbasketAccessItem(workbasketAccessItemMapper.toModel(authorization));
            }
            idConversion.put(definition.workbasketResource.workbasketId, workbasket.getId());
        }
        // This can not be done in step 1 because the system IDs are only known after step 1
        for (WorkbasketDefinition definition : definitions) {
            List<String> distributionTargets = new ArrayList<>();
            for (String oldId : definition.distributionTargets) {
                if (idConversion.containsKey(oldId)) {
                    distributionTargets.add(idConversion.get(oldId));
                } else {
                    throw new InvalidWorkbasketException(String.format("invalid import state: Workbasket '%s' does not exist in the given import list", oldId));
                }
            }
            workbasketService.setDistributionTargets(// no verification necessary since the workbasket was already imported in step 1.
            idConversion.get(definition.workbasketResource.workbasketId), distributionTargets);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    } catch (WorkbasketNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } catch (InvalidWorkbasketException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } catch (NotAuthorizedException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    } catch (InvalidArgumentException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
    } catch (WorkbasketAlreadyExistException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.CONFLICT);
    } catch (DomainNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
}
Also used : WorkbasketDefinition(pro.taskana.rest.resource.WorkbasketDefinition) WorkbasketAccessItemResource(pro.taskana.rest.resource.WorkbasketAccessItemResource) HashMap(java.util.HashMap) WorkbasketAlreadyExistException(pro.taskana.exceptions.WorkbasketAlreadyExistException) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) ArrayList(java.util.ArrayList) InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) DomainNotFoundException(pro.taskana.exceptions.DomainNotFoundException) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) ResponseEntity(org.springframework.http.ResponseEntity) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) WorkbasketResource(pro.taskana.rest.resource.WorkbasketResource) Workbasket(pro.taskana.Workbasket) PostMapping(org.springframework.web.bind.annotation.PostMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with NotAuthorizedException

use of pro.taskana.exceptions.NotAuthorizedException in project taskana by Taskana.

the class TaskServiceImplIntExplicitTest method shouldNotTransferByFailingSecurity.

@WithAccessId(userName = "User", groupNames = { "businessadmin" })
@Test
public void shouldNotTransferByFailingSecurity() throws WorkbasketNotFoundException, ClassificationNotFoundException, NotAuthorizedException, ClassificationAlreadyExistException, SQLException, TaskNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, InvalidArgumentException, WorkbasketAlreadyExistException, DomainNotFoundException {
    final String user = "User";
    // Set up Security for this Test
    dataSource = TaskanaEngineConfigurationTest.getDataSource();
    taskanaEngineConfiguration = new TaskanaEngineConfiguration(dataSource, false, true);
    taskanaEngine = taskanaEngineConfiguration.buildTaskanaEngine();
    taskanaEngineImpl = (TaskanaEngineImpl) taskanaEngine;
    taskanaEngineImpl.setConnectionManagementMode(ConnectionManagementMode.AUTOCOMMIT);
    taskServiceImpl = (TaskServiceImpl) taskanaEngine.getTaskService();
    classificationService = taskanaEngine.getClassificationService();
    workbasketService = taskanaEngine.getWorkbasketService();
    ClassificationImpl classification = (ClassificationImpl) classificationService.newClassification("KEY", "DOMAIN_A", "TASK");
    classification.setCategory("EXTERNAL");
    classification.setName("Transfert-Task Classification");
    classificationService.createClassification(classification);
    WorkbasketImpl wb = (WorkbasketImpl) workbasketService.newWorkbasket("wbKey1", "DOMAIN_A");
    wb.setName("BASE WB");
    wb.setDescription("Normal base WB");
    wb.setOwner(user);
    wb.setType(WorkbasketType.GROUP);
    wb = (WorkbasketImpl) workbasketService.createWorkbasket(wb);
    createWorkbasketWithSecurity(wb, wb.getOwner(), true, true, true, true);
    WorkbasketImpl wbNoAppend = (WorkbasketImpl) workbasketService.newWorkbasket("keyNoAppend", "DOMAIN_B");
    wbNoAppend.setName("Test-Security-WorkBasket-APPEND");
    wbNoAppend.setDescription("Workbasket without permission APPEND on Task");
    wbNoAppend.setOwner(user);
    wbNoAppend.setType(WorkbasketType.CLEARANCE);
    wbNoAppend = (WorkbasketImpl) workbasketService.createWorkbasket(wbNoAppend);
    createWorkbasketWithSecurity(wbNoAppend, wbNoAppend.getOwner(), true, true, false, true);
    WorkbasketImpl wbNoTransfer = (WorkbasketImpl) workbasketService.newWorkbasket("keyNoTransfer", "DOMAIN_A");
    wbNoTransfer.setName("Test-Security-WorkBasket-TRANSFER");
    wbNoTransfer.setDescription("Workbasket without permission TRANSFER on Task");
    wbNoTransfer.setOwner(user);
    wbNoTransfer.setType(WorkbasketType.GROUP);
    wbNoTransfer = (WorkbasketImpl) workbasketService.createWorkbasket(wbNoTransfer);
    createWorkbasketWithSecurity(wbNoTransfer, wbNoTransfer.getOwner(), true, true, true, false);
    TaskImpl task = (TaskImpl) taskServiceImpl.newTask(wb.getId());
    task.setName("Task Name");
    task.setDescription("Task used for transfer Test");
    task.setOwner(user);
    task.setClassificationKey(classification.getKey());
    task.setPrimaryObjRef(JunitHelper.createDefaultObjRef());
    task = (TaskImpl) taskServiceImpl.createTask(task);
    // Check failing with missing APPEND
    try {
        task = (TaskImpl) taskServiceImpl.transfer(task.getId(), wbNoAppend.getId());
        fail("Transfer Task should be FAILD, because there are no APPEND-Rights on destination WB.");
    } catch (NotAuthorizedException e) {
        if (!e.getMessage().contains("APPEND")) {
            fail("Transfer Task should be FAILD, because there are no APPEND-Rights on destination WB.");
        }
        assertThat(task.isTransferred(), equalTo(false));
        assertThat(task.getWorkbasketKey(), not(equalTo(wbNoAppend.getKey())));
        assertThat(task.getWorkbasketKey(), equalTo(wb.getKey()));
    }
    // Check failing with missing TRANSFER
    task.setId("");
    task.setWorkbasketKey(wbNoTransfer.getKey());
    task.getWorkbasketSummaryImpl().setId(wbNoTransfer.getId());
    task = (TaskImpl) taskServiceImpl.createTask(task);
    try {
        task = (TaskImpl) taskServiceImpl.transfer(task.getId(), wb.getId());
        fail("Transfer Task should be FAILD, because there are no TRANSFER-Rights on current WB.");
    } catch (NotAuthorizedException e) {
        if (!e.getMessage().contains("TRANSFER")) {
            fail("Transfer Task should be FAILD, because there are no APPEND-Rights on current WB.");
        }
        assertThat(task.isTransferred(), equalTo(false));
        assertThat(task.getWorkbasketKey(), not(equalTo(wbNoAppend.getKey())));
    }
}
Also used : TaskanaEngineConfiguration(pro.taskana.configuration.TaskanaEngineConfiguration) TaskImpl(pro.taskana.impl.TaskImpl) WorkbasketImpl(pro.taskana.impl.WorkbasketImpl) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) ClassificationImpl(pro.taskana.impl.ClassificationImpl) TaskanaEngineConfigurationTest(pro.taskana.impl.configuration.TaskanaEngineConfigurationTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Example 14 with NotAuthorizedException

use of pro.taskana.exceptions.NotAuthorizedException in project taskana by Taskana.

the class TaskServiceImplTest method testCreateThrowingAuthorizedOnWorkbasket.

@Test(expected = NotAuthorizedException.class)
public void testCreateThrowingAuthorizedOnWorkbasket() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, TaskAlreadyExistException, TaskNotFoundException, InvalidWorkbasketException, InvalidArgumentException {
    TaskServiceImpl cutSpy = Mockito.spy(cut);
    Classification dummyClassification = createDummyClassification();
    TaskImpl task = createUnitTestTask("", "dummyTask", "1", dummyClassification);
    Workbasket dummyWorkbasket = createWorkbasket("2", "k1");
    task.setWorkbasketSummary(dummyWorkbasket.asSummary());
    doReturn(dummyWorkbasket).when(workbasketServiceMock).getWorkbasket(any());
    doThrow(TaskNotFoundException.class).when(cutSpy).getTask(task.getId());
    doThrow(NotAuthorizedException.class).when(workbasketServiceMock).checkAuthorization(task.getWorkbasketSummary().getId(), WorkbasketPermission.APPEND);
    try {
        cutSpy.createTask(task);
    } catch (NotAuthorizedException e) {
        verify(taskanaEngineMock, times(1)).openConnection();
        verify(workbasketServiceMock, times(1)).getWorkbasket(task.getWorkbasketSummary().getId());
        verify(workbasketServiceMock, times(1)).checkAuthorization(task.getWorkbasketSummary().getId(), WorkbasketPermission.APPEND);
        verify(taskanaEngineMock, times(1)).returnConnection();
        verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, classificationQueryImplMock, classificationServiceImplMock);
        throw e;
    }
}
Also used : Classification(pro.taskana.Classification) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) Workbasket(pro.taskana.Workbasket) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with NotAuthorizedException

use of pro.taskana.exceptions.NotAuthorizedException in project taskana by Taskana.

the class UpdateWorkbasketAuthorizationsAccTest method testInsertAccessItemList.

@WithAccessId(userName = "teamlead_1", groupNames = { "group_1", "businessadmin" })
@Test
public void testInsertAccessItemList() throws InvalidArgumentException, NotAuthorizedException {
    WorkbasketService workbasketService = taskanaEngine.getWorkbasketService();
    final String wbId = "WBI:100000000000000000000000000000000004";
    List<WorkbasketAccessItem> accessItems = workbasketService.getWorkbasketAccessItems(wbId);
    int countBefore = accessItems.size();
    // update some values
    WorkbasketAccessItem item0 = accessItems.get(0);
    item0.setPermAppend(false);
    item0.setPermOpen(false);
    item0.setPermTransfer(false);
    final String updateId0 = item0.getId();
    // insert new entry
    WorkbasketAccessItem newItem = workbasketService.newWorkbasketAccessItem(wbId, CurrentUserContext.getUserid());
    newItem.setPermRead(true);
    newItem.setPermOpen(true);
    newItem.setPermCustom12(true);
    accessItems.add(newItem);
    workbasketService.setWorkbasketAccessItems(wbId, accessItems);
    List<WorkbasketAccessItem> updatedAccessItems = workbasketService.getWorkbasketAccessItems(wbId);
    int countAfter = updatedAccessItems.size();
    assertTrue((countBefore + 1) == countAfter);
    item0 = updatedAccessItems.stream().filter(i -> i.getId().equals(updateId0)).findFirst().get();
    assertFalse(item0.isPermAppend());
    assertFalse(item0.isPermOpen());
    assertFalse(item0.isPermTransfer());
    assertFalse(item0.isPermAppend());
    assertFalse(item0.isPermTransfer());
}
Also used : NotAuthorizedToQueryWorkbasketException(pro.taskana.exceptions.NotAuthorizedToQueryWorkbasketException) WithAccessId(pro.taskana.security.WithAccessId) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) RunWith(org.junit.runner.RunWith) WorkbasketService(pro.taskana.WorkbasketService) CurrentUserContext(pro.taskana.security.CurrentUserContext) Assert.assertThat(org.junit.Assert.assertThat) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) SQLException(java.sql.SQLException) JAASRunner(pro.taskana.security.JAASRunner) Task(pro.taskana.Task) Assert.fail(org.junit.Assert.fail) KeyDomain(pro.taskana.KeyDomain) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) IsNot.not(org.hamcrest.core.IsNot.not) TaskAlreadyExistException(pro.taskana.exceptions.TaskAlreadyExistException) TaskSummary(pro.taskana.TaskSummary) ClassificationNotFoundException(pro.taskana.exceptions.ClassificationNotFoundException) AbstractAccTest(acceptance.AbstractAccTest) Assert.assertTrue(org.junit.Assert.assertTrue) InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) Test(org.junit.Test) WorkbasketAccessItemImpl(pro.taskana.impl.WorkbasketAccessItemImpl) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) TaskService(pro.taskana.TaskService) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) Assert(org.junit.Assert) WorkbasketService(pro.taskana.WorkbasketService) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) AbstractAccTest(acceptance.AbstractAccTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Aggregations

NotAuthorizedException (pro.taskana.exceptions.NotAuthorizedException)27 Test (org.junit.Test)15 InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)12 ArrayList (java.util.ArrayList)11 AbstractAccTest (acceptance.AbstractAccTest)10 Workbasket (pro.taskana.Workbasket)9 WithAccessId (pro.taskana.security.WithAccessId)9 List (java.util.List)8 WorkbasketService (pro.taskana.WorkbasketService)8 WorkbasketNotFoundException (pro.taskana.exceptions.WorkbasketNotFoundException)8 TaskSummary (pro.taskana.TaskSummary)7 WorkbasketSummary (pro.taskana.WorkbasketSummary)7 ClassificationNotFoundException (pro.taskana.exceptions.ClassificationNotFoundException)7 InvalidWorkbasketException (pro.taskana.exceptions.InvalidWorkbasketException)7 SQLException (java.sql.SQLException)6 Collectors (java.util.stream.Collectors)5 ClassificationSummary (pro.taskana.ClassificationSummary)5 TaskService (pro.taskana.TaskService)5 TaskNotFoundException (pro.taskana.exceptions.TaskNotFoundException)5 Classification (pro.taskana.Classification)4