use of eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto in project CzechIdMng by bcvsolutions.
the class DefaultAutomaticRoleManager method changeAutomaticRoleRules.
@Override
public IdmAutomaticRoleAttributeDto changeAutomaticRoleRules(IdmAutomaticRoleAttributeDto automaticRole, boolean executeImmediately, IdmAutomaticRoleAttributeRuleDto... newRules) {
Assert.notNull(automaticRole, "Automatic role is required.");
Assert.notNull(automaticRole.getId(), "Automatic role must exists!");
IdmAutomaticRoleRequestDto request = new IdmAutomaticRoleRequestDto();
request.setOperation(RequestOperationType.UPDATE);
request.setRequestType(AutomaticRoleRequestType.ATTRIBUTE);
request.setExecuteImmediately(executeImmediately);
request.setAutomaticRole(automaticRole.getId());
request.setName(automaticRole.getName());
request.setRole(automaticRole.getRole());
final IdmAutomaticRoleRequestDto createdRequest = roleRequestService.save(request);
ArrayList<IdmAutomaticRoleAttributeRuleDto> rules = Lists.newArrayList(newRules);
if (rules != null) {
// Creates request for change or add rule
rules.forEach(rule -> {
IdmAutomaticRoleAttributeRuleRequestDto ruleRequest = new IdmAutomaticRoleAttributeRuleRequestDto();
ruleRequest.setRequest(createdRequest.getId());
ruleRequest.setOperation(rule.getId() != null ? RequestOperationType.UPDATE : RequestOperationType.ADD);
ruleRequest.setAttributeName(rule.getAttributeName());
ruleRequest.setComparison(rule.getComparison());
ruleRequest.setType(rule.getType());
ruleRequest.setFormAttribute(rule.getFormAttribute());
ruleRequest.setValue(rule.getValue());
ruleRequest.setRule(rule.getId());
ruleRequest = ruleRequestService.save(ruleRequest);
});
}
IdmAutomaticRoleAttributeRuleFilter ruleFilter = new IdmAutomaticRoleAttributeRuleFilter();
ruleFilter.setAutomaticRoleAttributeId(automaticRole.getId());
List<IdmAutomaticRoleAttributeRuleDto> currentRules = ruleService.find(ruleFilter, null).getContent();
currentRules.stream().filter(currentRule -> {
return rules == null || !rules.contains(currentRule);
}).forEach(ruleToDelete -> {
// Creates request for remove rule
IdmAutomaticRoleAttributeRuleRequestDto ruleRequest = new IdmAutomaticRoleAttributeRuleRequestDto();
ruleRequest.setRequest(createdRequest.getId());
ruleRequest.setOperation(RequestOperationType.REMOVE);
ruleRequest.setAttributeName(ruleToDelete.getAttributeName());
ruleRequest.setComparison(ruleToDelete.getComparison());
ruleRequest.setType(ruleToDelete.getType());
ruleRequest.setFormAttribute(ruleToDelete.getFormAttribute());
ruleRequest.setValue(ruleToDelete.getValue());
ruleRequest.setRule(ruleToDelete.getId());
ruleRequest = ruleRequestService.save(ruleRequest);
});
IdmAutomaticRoleRequestDto executedRequest = roleRequestService.startRequestInternal(createdRequest.getId(), true);
if (RequestState.EXECUTED == executedRequest.getState()) {
UUID createdAutomaticRoleId = executedRequest.getAutomaticRole();
Assert.notNull(createdAutomaticRoleId, "Automatic role identifier is required.");
return automaticRoleAttributeService.get(executedRequest.getAutomaticRole());
}
if (RequestState.IN_PROGRESS == executedRequest.getState()) {
throw new AcceptedException(executedRequest.getId().toString());
}
if (RequestState.EXCEPTION == executedRequest.getState()) {
throw new CoreException(executedRequest.getResult().getCause());
}
return null;
}
use of eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmAutomaticRoleAttributeServiceIntegrationTest method testRemoveLastRuleWithoutCheck.
@Test
public void testRemoveLastRuleWithoutCheck() {
String eavCode = "testingEav";
Long testEavIdentityValue = System.currentTimeMillis();
UUID testEavContractValue = UUID.randomUUID();
IdmIdentityDto identity = getHelper().createIdentity();
IdmRoleDto role = getHelper().createRole();
IdmIdentityContractDto primeContract = getHelper().getPrimeContract(identity.getId());
// create two eav attributes (for identity and contract)
IdmFormAttributeDto eavAttributeIdentity = getHelper().createEavAttribute(eavCode + System.currentTimeMillis(), IdmIdentity.class, PersistentType.LONG);
getHelper().setEavValue(identity, eavAttributeIdentity, IdmIdentity.class, testEavIdentityValue, PersistentType.LONG);
IdmFormAttributeDto eavAttributeContract = getHelper().createEavAttribute(eavCode + System.currentTimeMillis(), IdmIdentityContract.class, PersistentType.UUID);
getHelper().setEavValue(primeContract, eavAttributeContract, IdmIdentityContract.class, testEavContractValue, PersistentType.UUID);
IdmAutomaticRoleAttributeDto automaticRole = getHelper().createAutomaticRole(role.getId());
IdmAutomaticRoleAttributeRuleDto rule1 = getHelper().createAutomaticRoleRule(automaticRole.getId(), AutomaticRoleAttributeRuleComparison.EQUALS, AutomaticRoleAttributeRuleType.IDENTITY_EAV, null, eavAttributeIdentity.getId(), testEavIdentityValue.toString());
IdmAutomaticRoleAttributeRuleDto rule2 = getHelper().createAutomaticRoleRule(automaticRole.getId(), AutomaticRoleAttributeRuleComparison.EQUALS, AutomaticRoleAttributeRuleType.CONTRACT_EAV, null, eavAttributeContract.getId(), testEavContractValue.toString());
List<IdmIdentityRoleDto> identityRoles = identityRoleService.findAllByIdentity(identity.getId());
assertEquals(0, identityRoles.size());
this.recalculateSync(automaticRole.getId());
identityRoles = identityRoleService.findAllByIdentity(identity.getId());
assertEquals(1, identityRoles.size());
automaticRoleAttributeRuleService.delete(rule1);
this.recalculateSync(automaticRole.getId());
identityRoles = identityRoleService.findAllByIdentity(identity.getId());
assertEquals(1, identityRoles.size());
automaticRoleAttributeRuleService.delete(rule2);
// in this case we not able remove the last automatic role from identity
identityRoles = identityRoleService.findAllByIdentity(identity.getId());
assertEquals(0, identityRoles.size());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmAutomaticRoleAttributeServiceIntegrationTest method testFilterHasRules.
@Test
public void testFilterHasRules() {
long totalElements = automaticRoleAttributeService.find(null).getTotalElements();
assertEquals(0, totalElements);
//
IdmRoleDto role = getHelper().createRole();
IdmAutomaticRoleAttributeDto automaticRole = new IdmAutomaticRoleAttributeDto();
automaticRole.setRole(role.getId());
automaticRole.setName(getHelper().createName());
automaticRole = automaticRoleAttributeService.save(automaticRole);
//
IdmAutomaticRoleFilter filter = new IdmAutomaticRoleFilter();
filter.setHasRules(true);
totalElements = automaticRoleAttributeService.find(filter, null).getNumberOfElements();
assertEquals(0, totalElements);
//
filter.setHasRules(false);
List<IdmAutomaticRoleAttributeDto> content = automaticRoleAttributeService.find(filter, null).getContent();
assertEquals(1, content.size());
IdmAutomaticRoleAttributeDto found = content.get(0);
assertEquals(automaticRole.getId(), found.getId());
//
automaticRoleAttributeService.deleteInternal(found);
//
automaticRole = new IdmAutomaticRoleAttributeDto();
automaticRole.setRole(role.getId());
automaticRole.setName(getHelper().createName());
automaticRole = automaticRoleAttributeService.save(automaticRole);
//
IdmAutomaticRoleAttributeRuleDto rule1 = new IdmAutomaticRoleAttributeRuleDto();
rule1.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
rule1.setType(AutomaticRoleAttributeRuleType.IDENTITY);
rule1.setValue("test");
rule1.setAttributeName(IdmIdentity_.username.getName());
rule1.setAutomaticRoleAttribute(automaticRole.getId());
automaticRoleAttributeRuleService.save(rule1);
//
filter = new IdmAutomaticRoleFilter();
filter.setHasRules(true);
content = automaticRoleAttributeService.find(filter, null).getContent();
assertEquals(1, content.size());
found = content.get(0);
assertEquals(automaticRole.getId(), found.getId());
//
// try add next rules
IdmAutomaticRoleAttributeRuleDto rule2 = new IdmAutomaticRoleAttributeRuleDto();
rule2.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
rule2.setType(AutomaticRoleAttributeRuleType.IDENTITY);
rule2.setValue("test");
rule2.setAttributeName(IdmIdentity_.username.getName());
rule2.setAutomaticRoleAttribute(automaticRole.getId());
automaticRoleAttributeRuleService.save(rule2);
//
IdmAutomaticRoleAttributeRuleDto rule3 = new IdmAutomaticRoleAttributeRuleDto();
rule3.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
rule3.setType(AutomaticRoleAttributeRuleType.IDENTITY);
rule3.setValue("test");
rule3.setAttributeName(IdmIdentity_.username.getName());
rule3.setAutomaticRoleAttribute(automaticRole.getId());
automaticRoleAttributeRuleService.save(rule3);
//
filter = new IdmAutomaticRoleFilter();
filter.setHasRules(true);
content = automaticRoleAttributeService.find(filter, null).getContent();
assertEquals(1, content.size());
found = content.get(0);
assertEquals(automaticRole.getId(), found.getId());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmAutomaticRoleRequestServiceIntegrationTest method testAddRule.
@Test
public void testAddRule() {
IdmRoleDto role = prepareRole();
IdmIdentityDto identity = getHelper().createIdentity();
IdmIdentityDto identityTwo = getHelper().createIdentity();
IdmAutomaticRoleAttributeDto automaticRole = new IdmAutomaticRoleAttributeDto();
automaticRole.setRole(role.getId());
automaticRole.setName(role.getCode());
IdmAutomaticRoleAttributeRuleDto rule = new IdmAutomaticRoleAttributeRuleDto();
rule.setAttributeName(IdmIdentity_.username.getName());
rule.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
rule.setType(AutomaticRoleAttributeRuleType.IDENTITY);
rule.setValue(identity.getUsername());
// Create automatic role via manager
automaticRole = automaticRoleManager.createAutomaticRoleByAttribute(automaticRole, true, rule);
Assert.assertNotNull(automaticRole.getId());
IdmAutomaticRoleAttributeRuleFilter ruleFilter = new IdmAutomaticRoleAttributeRuleFilter();
ruleFilter.setAutomaticRoleAttributeId(automaticRole.getId());
List<IdmAutomaticRoleAttributeRuleDto> rules = ruleService.find(ruleFilter, null).getContent();
Assert.assertEquals(1, rules.size());
rule = rules.get(0);
// Create new rule
IdmAutomaticRoleAttributeRuleDto newRule = new IdmAutomaticRoleAttributeRuleDto();
newRule.setAttributeName(IdmIdentity_.username.getName());
newRule.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
newRule.setType(AutomaticRoleAttributeRuleType.IDENTITY);
newRule.setValue(identityTwo.getUsername());
// Change automatic role via manager
automaticRole = automaticRoleManager.changeAutomaticRoleRules(automaticRole, true, newRule);
// Find current rules
rules = ruleService.find(ruleFilter, null).getContent();
Assert.assertEquals(1, rules.size());
// We created new rule and deleted old
Assert.assertNotEquals(rule.getId(), rules.get(0).getId());
Assert.assertEquals(identityTwo.getUsername(), rules.get(0).getValue());
}
use of eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto in project CzechIdMng by bcvsolutions.
the class DefaultIdmAutomaticRoleRequestServiceIntegrationTest method testCreateAutomaticAttributeRoleWithApproval.
@Test
public void testCreateAutomaticAttributeRoleWithApproval() {
IdmRoleDto role = prepareRole();
IdmIdentityDto identity = getHelper().createIdentity();
IdmIdentityDto guaranteeIdentity = getHelper().createIdentity();
getHelper().createRoleGuarantee(role, guaranteeIdentity);
IdmAutomaticRoleAttributeDto automaticRole = new IdmAutomaticRoleAttributeDto();
automaticRole.setRole(role.getId());
automaticRole.setName(role.getCode());
IdmAutomaticRoleAttributeRuleDto rule = new IdmAutomaticRoleAttributeRuleDto();
rule.setAttributeName(IdmIdentity_.username.getName());
rule.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
rule.setType(AutomaticRoleAttributeRuleType.IDENTITY);
rule.setValue(identity.getUsername());
// Create automatic role via manager
try {
automaticRole = automaticRoleManager.createAutomaticRoleByAttribute(automaticRole, false, rule);
} catch (AcceptedException ex) {
// The request is in approval
Assert.assertNotNull(ex.getIdentifier());
UUID requestId = UUID.fromString(ex.getIdentifier());
loginAsNoAdmin(guaranteeIdentity.getUsername());
try {
completeTasksFromUsers(guaranteeIdentity.getUsername(), "approve");
} catch (ResultCodeException e) {
fail("User has permission to approve task. Error message: " + e.getLocalizedMessage());
} catch (Exception e) {
fail("Some problem: " + e.getLocalizedMessage());
}
IdmAutomaticRoleRequestDto request = roleRequestService.get(requestId);
Assert.assertEquals(RequestState.EXECUTED, request.getState());
Assert.assertNotNull(request.getAutomaticRole());
automaticRole = automaticRoleAttributeService.get(request.getAutomaticRole());
Assert.assertNotNull(automaticRole);
Assert.assertEquals(role.getId(), automaticRole.getRole());
return;
}
fail("Automatic role request have to be approving by gurantee!");
}
Aggregations