use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto 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.IdmDelegationDefinitionDto in project CzechIdMng by bcvsolutions.
the class ApproveRoleByManagerDelegationType method findDelegation.
@Override
public List<IdmDelegationDefinitionDto> findDelegation(UUID delegatorId, UUID contractId, BaseDto owner) {
Assert.isTrue(owner instanceof WorkflowTaskInstanceDto, "Owner must be workflow task for this delegation type!");
WorkflowTaskInstanceDto task = (WorkflowTaskInstanceDto) owner;
Object conceptRoleObj = taskInstanceService.getProcessVariable(task.getId(), ROLE_CONCEPT_PROPERTY);
if (conceptRoleObj instanceof VariableInstance) {
VariableInstance variableInstance = (VariableInstance) conceptRoleObj;
conceptRoleObj = variableInstance.getValue();
}
if (!(conceptRoleObj instanceof IdmConceptRoleRequestDto)) {
throw new CoreException(MessageFormat.format("For this delegation type [{0}], must workflow task contains '{1}' [WorkflowTaskInstanceDto] variable", NAME, ROLE_CONCEPT_PROPERTY));
}
IdmConceptRoleRequestDto conceptRole = (IdmConceptRoleRequestDto) conceptRoleObj;
UUID contractFromRequest = conceptRole.getIdentityContract();
Assert.notNull(contractFromRequest, "Contract ID from the concept of the request cannot be null!");
// Filtering managers contracts by subordinates contract.
IdmIdentityContractFilter contractFilter = new IdmIdentityContractFilter();
contractFilter.setIdentity(delegatorId);
contractFilter.setManagersByContract(contractFromRequest);
List<IdmIdentityContractDto> managersContracts = identityContractService.find(contractFilter, null).getContent();
if (managersContracts.isEmpty()) {
return null;
}
Set<IdmDelegationDefinitionDto> resultDelegations = Sets.newHashSet();
// Find all valid delegation definitions for manager's contract.
managersContracts.stream().map((managersContract) -> super.findDelegation(delegatorId, managersContract.getId(), owner)).filter((delegations) -> (!CollectionUtils.isEmpty(delegations))).forEachOrdered((delegations) -> {
resultDelegations.addAll(delegations);
});
return Lists.newArrayList(resultDelegations);
}
use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto in project CzechIdMng by bcvsolutions.
the class DelegationDefinitionByDelegatorContractEvaluatorTest method testRead.
@Test
public void testRead() {
IdmIdentityDto delegatorOne = getHelper().createIdentity();
IdmIdentityDto delegatorTwo = getHelper().createIdentity();
IdmIdentityContractDto primeContactDelegatorOne = getHelper().getPrimeContract(delegatorOne);
IdmIdentityContractDto primeContactDelegatorTwo = getHelper().getPrimeContract(delegatorTwo);
IdmIdentityDto delegateOne = getHelper().createIdentity();
IdmIdentityDto delegateTwo = getHelper().createIdentity();
// Create default delegation One.
IdmDelegationDefinitionDto definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(ApproveRoleByManagerDelegationType.NAME);
definitionOne.setDelegator(delegatorOne.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne.setDelegatorContract(primeContactDelegatorOne.getId());
definitionOne = delegationDefinitionService.save(definitionOne);
// Create default delegation Two.
IdmDelegationDefinitionDto definitionTwo = new IdmDelegationDefinitionDto();
definitionTwo.setType(ApproveRoleByManagerDelegationType.NAME);
definitionTwo.setDelegator(delegatorTwo.getId());
definitionTwo.setDelegate(delegateTwo.getId());
definitionTwo.setDelegatorContract(primeContactDelegatorTwo.getId());
delegationDefinitionService.save(definitionTwo);
//
List<IdmIdentityDto> identities;
List<IdmIdentityContractDto> contracts;
List<IdmDelegationDefinitionDto> delegationDefinitions;
IdmRoleDto roleWithPermissions = getHelper().createRole();
//
getHelper().createIdentityRole(delegatorOne, roleWithPermissions);
getHelper().createIdentityRole(delegatorTwo, roleWithPermissions);
// check - read without policy
try {
getHelper().login(delegatorOne.getUsername(), delegatorOne.getPassword());
//
identities = identityService.find(null, IdmBasePermission.READ).getContent();
Assert.assertTrue(identities.isEmpty());
contracts = contractService.find(null, IdmBasePermission.READ).getContent();
Assert.assertTrue(contracts.isEmpty());
delegationDefinitions = delegationDefinitionService.find(null, IdmBasePermission.READ).getContent();
Assert.assertTrue(delegationDefinitions.isEmpty());
} finally {
logout();
}
//
// without login
contracts = contractService.find(null, IdmBasePermission.READ).getContent();
Assert.assertTrue(contracts.isEmpty());
//
// create authorization policy - assign to role
// identity
getHelper().createAuthorizationPolicy(roleWithPermissions.getId(), CoreGroupPermission.IDENTITY, IdmIdentity.class, SelfIdentityEvaluator.class, IdmBasePermission.READ);
getHelper().createAuthorizationPolicy(roleWithPermissions.getId(), CoreGroupPermission.IDENTITYCONTRACT, IdmIdentityContract.class, IdentityContractByIdentityEvaluator.class, IdmBasePermission.READ);
// delegation transitively
getHelper().createAuthorizationPolicy(roleWithPermissions.getId(), CoreGroupPermission.DELEGATIONDEFINITION, IdmDelegationDefinition.class, DelegationDefinitionByDelegatorContractEvaluator.class);
//
try {
getHelper().login(delegatorOne.getUsername(), delegatorOne.getPassword());
//
// without update permission
identities = identityService.find(null, IdmBasePermission.UPDATE).getContent();
Assert.assertTrue(identities.isEmpty());
contracts = contractService.find(null, IdmBasePermission.UPDATE).getContent();
Assert.assertTrue(contracts.isEmpty());
delegationDefinitions = delegationDefinitionService.find(null, IdmBasePermission.UPDATE).getContent();
Assert.assertTrue(delegationDefinitions.isEmpty());
//
// evaluate access
identities = identityService.find(null, IdmBasePermission.READ).getContent();
Assert.assertEquals(1, identities.size());
Assert.assertEquals(delegatorOne.getId(), identities.get(0).getId());
contracts = contractService.find(null, IdmBasePermission.READ).getContent();
Assert.assertEquals(1, contracts.size());
Assert.assertEquals(primeContactDelegatorOne.getId(), contracts.get(0).getId());
delegationDefinitions = delegationDefinitionService.find(null, IdmBasePermission.READ).getContent();
Assert.assertEquals(1, delegationDefinitions.size());
Assert.assertEquals(definitionOne.getId(), delegationDefinitions.get(0).getId());
//
Set<String> permissions = identityService.getPermissions(delegatorOne);
Assert.assertEquals(1, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.name())));
permissions = contractService.getPermissions(primeContactDelegatorOne);
Assert.assertEquals(1, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.name())));
permissions = delegationDefinitionService.getPermissions(definitionOne);
Assert.assertEquals(1, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.name())));
} finally {
logout();
}
}
use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto in project CzechIdMng by bcvsolutions.
the class DelegationIntegrationTest method testDelegationDefinitionValiditySameDelegate.
@Test
public void testDelegationDefinitionValiditySameDelegate() {
IdmIdentityDto delegator = getHelper().createIdentity();
IdmIdentityDto delegateOne = getHelper().createIdentity();
// Create default delegations.
IdmDelegationDefinitionDto definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne = delegationDefinitionService.save(definitionOne);
IdmDelegationDefinitionDto definitionTwo = new IdmDelegationDefinitionDto();
definitionTwo.setType(DefaultDelegationType.NAME);
definitionTwo.setDelegator(delegator.getId());
definitionTwo.setDelegate(delegateOne.getId());
definitionTwo = delegationDefinitionService.save(definitionTwo);
List<IdmDelegationDefinitionDto> delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// Two delegations are valid for this delegator.
assertEquals(2, delegations.size());
// Set definition Two as unvalid.
delegationDefinitionService.delete(definitionTwo);
definitionTwo = new IdmDelegationDefinitionDto();
definitionTwo.setDelegator(delegator.getId());
definitionTwo.setDelegate(delegateOne.getId());
definitionTwo.setType(DefaultDelegationType.NAME);
definitionTwo.setValidFrom(LocalDate.now().minusDays(10));
definitionTwo.setValidTill(LocalDate.now().minusDays(1));
delegationDefinitionService.save(definitionTwo);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// Only delegation One is valid now.
assertEquals(1, delegations.size());
assertEquals(definitionOne.getId(), delegations.get(0).getId());
// Set definition One as unvalid.
delegationDefinitionService.delete(definitionOne);
definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne.setValidFrom(LocalDate.now().plusDays(20));
definitionOne.setValidTill(LocalDate.now().plusDays(10));
definitionOne = delegationDefinitionService.save(definitionOne);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// No delegation is valid now.
Assert.assertNull(delegations);
// Set definition One valid in future.
delegationDefinitionService.delete(definitionOne);
definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setValidFrom(LocalDate.now().plusDays(20));
definitionOne.setValidTill(null);
definitionOne = delegationDefinitionService.save(definitionOne);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// No delegation is valid now.
Assert.assertNull(delegations);
// Set definition One as valid.
delegationDefinitionService.delete(definitionOne);
definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne.setValidFrom(LocalDate.now());
definitionOne.setValidTill(LocalDate.now().plusDays(10));
definitionOne = delegationDefinitionService.save(definitionOne);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// Only delegation One is valid now.
assertEquals(1, delegations.size());
assertEquals(definitionOne.getId(), delegations.get(0).getId());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto in project CzechIdMng by bcvsolutions.
the class DelegationIntegrationTest method testDelegationDefinitionValidityDifferentDelegate.
@Test
public void testDelegationDefinitionValidityDifferentDelegate() {
IdmIdentityDto delegator = getHelper().createIdentity();
IdmIdentityDto delegateOne = getHelper().createIdentity();
IdmIdentityDto delegateTwo = getHelper().createIdentity();
// Create default delegations.
IdmDelegationDefinitionDto definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne = delegationDefinitionService.save(definitionOne);
IdmDelegationDefinitionDto definitionTwo = new IdmDelegationDefinitionDto();
definitionTwo.setType(DefaultDelegationType.NAME);
definitionTwo.setDelegator(delegator.getId());
definitionTwo.setDelegate(delegateTwo.getId());
definitionTwo = delegationDefinitionService.save(definitionTwo);
List<IdmDelegationDefinitionDto> delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// Two delegations are valid for this delegator.
assertEquals(2, delegations.size());
// Set definition Two as unvalid.
delegationDefinitionService.delete(definitionTwo);
definitionTwo = new IdmDelegationDefinitionDto();
definitionTwo.setType(DefaultDelegationType.NAME);
definitionTwo.setDelegator(delegator.getId());
definitionTwo.setDelegate(delegateTwo.getId());
definitionTwo.setValidFrom(LocalDate.now().minusDays(10));
definitionTwo.setValidTill(LocalDate.now().minusDays(1));
delegationDefinitionService.save(definitionTwo);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// Only delegation One is valid now.
assertEquals(1, delegations.size());
assertEquals(definitionOne.getId(), delegations.get(0).getId());
// Set definition One as unvalid.
delegationDefinitionService.delete(definitionOne);
definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne.setValidFrom(LocalDate.now().plusDays(20));
definitionOne.setValidTill(LocalDate.now().plusDays(10));
definitionOne = delegationDefinitionService.save(definitionOne);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// No delegation is valid now.
Assert.assertNull(delegations);
// Set definition One valid in future.
delegationDefinitionService.delete(definitionOne);
definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne.setValidFrom(LocalDate.now().plusDays(20));
definitionOne.setValidTill(null);
definitionOne = delegationDefinitionService.save(definitionOne);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// No delegation is valid now.
Assert.assertNull(delegations);
// Set definition One as valid.
delegationDefinitionService.delete(definitionOne);
definitionOne = new IdmDelegationDefinitionDto();
definitionOne.setType(DefaultDelegationType.NAME);
definitionOne.setDelegator(delegator.getId());
definitionOne.setDelegate(delegateOne.getId());
definitionOne.setValidFrom(LocalDate.now());
definitionOne.setValidTill(LocalDate.now().plusDays(10));
definitionOne = delegationDefinitionService.save(definitionOne);
delegations = delegationManager.findDelegation(DefaultDelegationType.NAME, delegator.getId(), null, null);
// Only delegation One is valid now.
assertEquals(1, delegations.size());
assertEquals(definitionOne.getId(), delegations.get(0).getId());
}
Aggregations