Search in sources :

Example 6 with IdmDelegationDto

use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto in project CzechIdMng by bcvsolutions.

the class DelegationCreateNotificationProcessor method process.

@Override
public EventResult<IdmDelegationDto> process(EntityEvent<IdmDelegationDto> event) {
    IdmDelegationDto dto = event.getContent();
    IdmDelegationDefinitionDto definition = lookupService.lookupEmbeddedDto(dto, IdmDelegation_.definition);
    Assert.notNull(definition, "Delegate definition cannot be null!");
    Assert.notNull(definition.getType(), "Delegate type cannot be null!");
    Assert.notNull(definition.getDelegator(), "Delegator cannot be null!");
    Assert.notNull(definition.getDelegate(), "Delegate cannot be null!");
    IdmIdentityDto delegator = identityService.get(definition.getDelegator());
    IdmIdentityDto delegate = identityService.get(definition.getDelegate());
    // UUID currentUserId = securityService.getCurrentId();
    // Send notification to the delegate.
    sendNotification(CoreModuleDescriptor.TOPIC_DELEGATION_INSTANCE_CREATED_TO_DELEGATE, dto, definition, delegator, delegate, delegate);
    return new DefaultEventResult<>(event, this);
}
Also used : IdmDelegationDefinitionDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) IdmDelegationDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)

Example 7 with IdmDelegationDto

use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto in project CzechIdMng by bcvsolutions.

the class DelegationSaveProcessor method process.

@Override
public EventResult<IdmDelegationDto> process(EntityEvent<IdmDelegationDto> event) {
    IdmDelegationDto dto = event.getContent();
    dto = service.saveInternal(dto);
    event.setContent(dto);
    return new DefaultEventResult<>(event, this);
}
Also used : DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) IdmDelegationDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto)

Example 8 with IdmDelegationDto

use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto in project CzechIdMng by bcvsolutions.

the class WorkflowTaskInstanceController method addDelegationToTask.

/**
 * Find and add definition of the delegation connected with this task.
 *
 * @param dto
 */
private void addDelegationToTask(WorkflowTaskInstanceDto dto, BasePermission... permission) {
    if (dto != null && dto.getId() != null) {
        // We need to create mock task, because DTO can be instance of historic task here.
        WorkflowTaskInstanceDto mockTask = new WorkflowTaskInstanceDto();
        mockTask.setId(dto.getId());
        UUID currentUserId = securityService.getCurrentId();
        boolean currentUserIsCandidate = dto.getIdentityLinks().stream().filter(identityLink -> IdentityLinkType.CANDIDATE.equals(identityLink.getType()) || IdentityLinkType.ASSIGNEE.equals(identityLink.getType())).anyMatch(identityLink -> currentUserId != null && UUID.fromString(identityLink.getUserId()).equals(currentUserId));
        boolean filterOnlyForCurrentUser = currentUserIsCandidate && !workflowTaskInstanceService.canReadAllTask(permission);
        List<IdmDelegationDto> delegations = delegationManager.findDelegationForOwner(mockTask, permission).stream().filter(delegation -> {
            // Filter only delegation where delegator or delegate is logged user (and user is not admin).
            if (!filterOnlyForCurrentUser) {
                return true;
            }
            IdmDelegationDefinitionDto definition = DtoUtils.getEmbedded(delegation, IdmDelegation_.definition.getName(), IdmDelegationDefinitionDto.class);
            return definition.getDelegate().equals(currentUserId) || definition.getDelegator().equals(currentUserId);
        }).sorted(Comparator.comparing(IdmDelegationDto::getCreated)).collect(Collectors.toList());
        // TODO: ONLY first delegation definition is sets to the task!
        if (!CollectionUtils.isEmpty(delegations)) {
            Collections.reverse(delegations);
            IdmDelegationDto delegation = delegations.get(0);
            IdmDelegationDefinitionDto definition = DtoUtils.getEmbedded(delegation, IdmDelegation_.definition.getName(), IdmDelegationDefinitionDto.class);
            dto.setDelegationDefinition(definition);
        }
    }
}
Also used : IdmDelegation_(eu.bcvsolutions.idm.core.model.entity.IdmDelegation_) FormDataWrapperDto(eu.bcvsolutions.idm.core.workflow.model.dto.FormDataWrapperDto) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) DtoUtils(eu.bcvsolutions.idm.core.api.utils.DtoUtils) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiParam(io.swagger.annotations.ApiParam) WorkflowTaskInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto) SecurityService(eu.bcvsolutions.idm.core.security.api.service.SecurityService) Valid(javax.validation.Valid) CoreGroupPermission(eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission) ApiOperation(io.swagger.annotations.ApiOperation) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission) WorkflowTaskInstanceService(eu.bcvsolutions.idm.core.workflow.service.WorkflowTaskInstanceService) Pageable(org.springframework.data.domain.Pageable) AuthorizationScope(io.swagger.annotations.AuthorizationScope) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IdmDelegationDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto) WorkflowTaskInstanceAbstractDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceAbstractDto) MediaType(org.springframework.http.MediaType) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) List(java.util.List) AbstractReadDtoController(eu.bcvsolutions.idm.core.api.rest.AbstractReadDtoController) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) DelegationManager(eu.bcvsolutions.idm.core.api.service.DelegationManager) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) BulkActionManager(eu.bcvsolutions.idm.core.api.bulk.action.BulkActionManager) IdentityLinkType(org.activiti.engine.task.IdentityLinkType) RequestBody(org.springframework.web.bind.annotation.RequestBody) IdmBasePermission(eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission) SwaggerConfig(eu.bcvsolutions.idm.core.api.config.swagger.SwaggerConfig) CollectionUtils(org.apache.commons.collections.CollectionUtils) WorkflowProcessInstanceService(eu.bcvsolutions.idm.core.workflow.service.WorkflowProcessInstanceService) WorkflowFilterDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowFilterDto) Api(io.swagger.annotations.Api) MultiValueMap(org.springframework.util.MultiValueMap) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) IdmBulkActionDto(eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto) WorkflowHistoricTaskInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowHistoricTaskInstanceDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) BaseController(eu.bcvsolutions.idm.core.api.rest.BaseController) BaseDtoController(eu.bcvsolutions.idm.core.api.rest.BaseDtoController) PageableDefault(org.springframework.data.web.PageableDefault) Resources(org.springframework.hateoas.Resources) ResponseEntity(org.springframework.http.ResponseEntity) IdmDelegationDefinitionDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto) Comparator(java.util.Comparator) Authorization(io.swagger.annotations.Authorization) Collections(java.util.Collections) IdmDelegationDefinitionDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto) WorkflowTaskInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto) IdmDelegationDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto) UUID(java.util.UUID)

Example 9 with IdmDelegationDto

use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto in project CzechIdMng by bcvsolutions.

the class DelegationEventListener method onEvent.

@Override
public void onEvent(ActivitiEvent event) {
    LOG.debug("DelegationEventListener - recieve event [{}]", event.getType());
    switch(event.getType()) {
        case TASK_CREATED:
            if (event instanceof ActivitiEntityEventImpl && ((ActivitiEntityEventImpl) event).getEntity() instanceof TaskEntity) {
                TaskEntity taskEntity = (TaskEntity) ((ActivitiEntityEventImpl) event).getEntity();
                if (taskEntity != null && taskEntity.getCandidates() != null) {
                    String processDefinitionId = taskEntity.getProcessDefinitionId();
                    String processDelegationType = delegationManager.getProcessDelegationType(processDefinitionId);
                    if (processDelegationType == null) {
                        // If process doesn't have a delegation type defined, then will be used default.
                        processDelegationType = DefaultDelegationType.NAME;
                    }
                    String delegationType = processDelegationType;
                    taskEntity.getCandidates().forEach(identityLink -> {
                        String user = identityLink.getUserId();
                        Assert.notNull(user, "User id cannot be null!");
                        Assert.isTrue(EntityUtils.isUuid(user), "User id must be UUID in this phase!");
                        UUID userId = UUID.fromString(user);
                        WorkflowTaskInstanceDto mockTask = new WorkflowTaskInstanceDto();
                        mockTask.setId(taskEntity.getId());
                        List<IdmDelegationDefinitionDto> delegationDefinitions = delegationManager.findDelegation(delegationType, userId, null, mockTask);
                        if (CollectionUtils.isEmpty(delegationDefinitions)) {
                            // No delegation found.
                            return;
                        }
                        // Create delegation.
                        delegationDefinitions.forEach(delegationDefinition -> {
                            delegationManager.delegate(mockTask, delegationDefinition);
                            UUID delegate = delegationDefinition.getDelegate();
                            Assert.notNull(delegate, "Delegate cannot be null!");
                            taskEntity.addCandidateUser(delegate.toString());
                        });
                        // Remove delegator form task and add delegate as candidate.
                        taskEntity.deleteUserIdentityLink(user, identityLink.getType());
                        // Add delegator to the task as participant (delegator must have permission for read the task)
                        taskEntity.addUserIdentityLink(user, IdentityLinkType.PARTICIPANT);
                    });
                }
            }
            break;
        case TASK_COMPLETED:
            // Check if exists task delegation for this task and set state on executed.
            if (event instanceof ActivitiEntityEventImpl && ((ActivitiEntityEventImpl) event).getEntity() instanceof TaskEntity) {
                TaskEntity taskEntity = (TaskEntity) ((ActivitiEntityEventImpl) event).getEntity();
                if (taskEntity.getId() != null) {
                    IdmDelegationFilter delegationFilter = new IdmDelegationFilter();
                    delegationFilter.setOwnerId(DtoUtils.toUuid(taskEntity.getId()));
                    delegationFilter.setOwnerType(WorkflowTaskInstanceDto.class.getCanonicalName());
                    IdmDelegationDto delegation = delegationService.find(delegationFilter, null).getContent().stream().findFirst().orElse(null);
                    if (delegation != null) {
                        delegation.setOwnerState(new OperationResultDto(OperationState.EXECUTED));
                        delegationService.save(delegation);
                    }
                }
            }
            break;
        case ACTIVITY_CANCELLED:
            // Check if exists task delegation for this cancelled activity and set state to the cancelled.
            if (event instanceof ActivitiActivityCancelledEvent) {
                // Find activiti task by name (event doesn't contains ID of user task)
                ActivitiActivityCancelledEvent cancelledEvent = (ActivitiActivityCancelledEvent) event;
                TaskQuery taskQuery = taskService.createTaskQuery();
                taskQuery.taskDefinitionKey(cancelledEvent.getActivityId());
                taskQuery.processInstanceId(cancelledEvent.getProcessInstanceId());
                taskQuery.processDefinitionId(cancelledEvent.getProcessDefinitionId());
                // Search a task.
                List<Task> tasks = taskQuery.list();
                if (tasks != null && tasks.size() == 1) {
                    Task task = tasks.get(0);
                    if (task.getId() != null) {
                        WorkflowTaskInstanceDto mockTask = new WorkflowTaskInstanceDto();
                        mockTask.setId(task.getId());
                        List<IdmDelegationDto> delegations = delegationManager.findDelegationForOwner(mockTask);
                        if (!CollectionUtils.isEmpty(delegations)) {
                            delegations.forEach(delegation -> {
                                delegation.setOwnerState(new OperationResultDto(OperationState.CANCELED));
                                delegationService.save(delegation);
                            });
                        }
                    }
                }
            }
            break;
        // Delete IdmDelegations using this workflow task - ensures integrity.
        case ENTITY_DELETED:
            if (event instanceof ActivitiEntityEventImpl && ((ActivitiEntityEventImpl) event).getEntity() instanceof HistoricTaskInstanceEntity) {
                HistoricTaskInstanceEntity taskEntity = (HistoricTaskInstanceEntity) ((ActivitiEntityEventImpl) event).getEntity();
                String taskId = taskEntity.getId();
                Assert.notNull(taskId, "Task ID cannot be null here!");
                IdmDelegationFilter delegationFilter = new IdmDelegationFilter();
                delegationFilter.setOwnerId(DtoUtils.toUuid(taskId));
                delegationFilter.setOwnerType(WorkflowTaskInstanceDto.class.getCanonicalName());
                // Delete delegation connected to this task.
                delegationService.find(delegationFilter, null).getContent().forEach(delegation -> {
                    delegationService.delete(delegation);
                });
            }
            break;
        default:
            LOG.debug("DelegationEventListener - receive not required event [{}]", event.getType());
    }
}
Also used : ActivitiEntityEventImpl(org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl) Task(org.activiti.engine.task.Task) TaskEntity(org.activiti.engine.impl.persistence.entity.TaskEntity) IdmDelegationDefinitionDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto) ActivitiActivityCancelledEvent(org.activiti.engine.delegate.event.ActivitiActivityCancelledEvent) WorkflowTaskInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) HistoricTaskInstanceEntity(org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntity) TaskQuery(org.activiti.engine.task.TaskQuery) IdmDelegationDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto) UUID(java.util.UUID) IdmDelegationFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmDelegationFilter)

Example 10 with IdmDelegationDto

use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto in project CzechIdMng by bcvsolutions.

the class DelegationIntegrationTest method testDelegationDefinitionIntegrity.

@Test
public void testDelegationDefinitionIntegrity() {
    IdmIdentityDto delegator = getHelper().createIdentity();
    IdmIdentityDto delegateOne = getHelper().createIdentity();
    // Create default delegations and delegations.
    IdmDelegationDefinitionDto definitionOne = new IdmDelegationDefinitionDto();
    definitionOne.setType(ApproveRoleByManagerDelegationType.NAME);
    definitionOne.setDelegator(delegator.getId());
    definitionOne.setDelegate(delegateOne.getId());
    definitionOne = delegationDefinitionService.save(definitionOne);
    IdmDelegationDto delegationOne = new IdmDelegationDto();
    delegationOne.setDefinition(definitionOne.getId());
    delegationOne.setOwnerId(UUID.randomUUID());
    delegationOne.setOwnerType(WorkflowTaskInstanceDto.class.getCanonicalName());
    delegationOne = delegationService.save(delegationOne);
    IdmDelegationDto delegationTwo = new IdmDelegationDto();
    delegationTwo.setDefinition(definitionOne.getId());
    delegationTwo.setOwnerId(UUID.randomUUID());
    delegationTwo.setOwnerType(WorkflowTaskInstanceDto.class.getCanonicalName());
    delegationTwo = delegationService.save(delegationTwo);
    // Delegations shuld be deleted on delete of the definition.
    delegationDefinitionService.delete(definitionOne);
    IdmDelegationDto delegation = delegationService.get(delegationOne.getId());
    Assert.assertNull(delegation);
    delegation = delegationService.get(delegationTwo.getId());
    Assert.assertNull(delegation);
}
Also used : IdmDelegationDefinitionDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto) WorkflowTaskInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto) IdmDelegationDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test) ChangeIdentityPermissionTest(eu.bcvsolutions.idm.core.workflow.permissions.ChangeIdentityPermissionTest)

Aggregations

IdmDelegationDto (eu.bcvsolutions.idm.core.api.dto.IdmDelegationDto)10 IdmDelegationDefinitionDto (eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto)6 WorkflowTaskInstanceDto (eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto)5 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)3 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)3 IdmDelegationFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmDelegationFilter)3 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)3 UUID (java.util.UUID)3 CoreGroupPermission (eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission)2 IdmBasePermission (eu.bcvsolutions.idm.core.security.api.domain.IdmBasePermission)2 BulkActionManager (eu.bcvsolutions.idm.core.api.bulk.action.BulkActionManager)1 IdmBulkActionDto (eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto)1 SwaggerConfig (eu.bcvsolutions.idm.core.api.config.swagger.SwaggerConfig)1 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)1 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)1 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)1 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)1 EntityNotFoundException (eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 AbstractReadDtoController (eu.bcvsolutions.idm.core.api.rest.AbstractReadDtoController)1