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