use of eu.bcvsolutions.idm.acc.domain.MappingContext in project CzechIdMng by bcvsolutions.
the class AbstractProvisioningExecutor method prepareProvisioning.
private SysProvisioningOperationDto prepareProvisioning(SysSystemEntityDto systemEntity, DTO dto, UUID entityId, ProvisioningOperationType operationType, List<? extends AttributeMapping> attributes) {
Assert.notNull(systemEntity, "System entity is required.");
Assert.notNull(systemEntity.getUid(), "System entity uid is required.");
Assert.notNull(systemEntity.getEntityType(), "System entity type is required.");
SysSystemDto system = DtoUtils.getEmbedded(systemEntity, SysSystemEntity_.system);
// If are input attributes null, then we load default mapped attributes
if (attributes == null) {
attributes = findAttributeMappings(system, systemEntity.getEntityType());
}
if (attributes == null || attributes.isEmpty()) {
return null;
}
// One IDM object can be mapped to one connector object (= one connector
// class).
SysSystemMappingDto mapping = getMapping(system, systemEntity.getEntityType());
if (mapping == null) {
// TODO: delete operation?
return null;
}
// Create mapping context from the script defined on the mapping and by checked options.
// This context will be propagate to all attributes (transformation to the system).
MappingContext mappingContext = systemMappingService.getMappingContext(mapping, systemEntity, dto, system);
Map<ProvisioningAttributeDto, Object> accountAttributes = prepareMappedAttributesValues(dto, operationType, systemEntity, attributes, mappingContext);
UUID roleRequestId = null;
if (ProvisioningOperationType.DELETE == operationType) {
// Return ID of role-request from system-entity's context.
roleRequestId = getRoleRequestIdFromContext(systemEntity);
} else {
// Return ID of role-request from DTO's context.
roleRequestId = getRoleRequestIdFromContext(dto);
}
// public provisioning event
SysSchemaObjectClassDto schemaObjectClassDto = schemaObjectClassService.get(mapping.getObjectClass());
IcConnectorObject connectorObject = new IcConnectorObjectImpl(systemEntity.getUid(), new IcObjectClassImpl(schemaObjectClassDto.getObjectClassName()), null);
// Propagate the role-request ID to the connector (for virtual systems ...)
connectorObject.getObjectClass().setRoleRequestId(roleRequestId);
SysProvisioningOperationDto.Builder operationBuilder = new SysProvisioningOperationDto.Builder().setOperationType(//
operationType).setSystemEntity(//
systemEntity).setEntityIdentifier(//
entityId).setRoleRequestId(roleRequestId).setProvisioningContext(new ProvisioningContext(accountAttributes, connectorObject));
//
return operationBuilder.build();
}
use of eu.bcvsolutions.idm.acc.domain.MappingContext in project CzechIdMng by bcvsolutions.
the class MappingContextTest method testMappingContextIdentityRolesForSystem.
@Test
public void testMappingContextIdentityRolesForSystem() {
SysSystemDto system = helper.createTestResourceSystem(true);
Assert.assertNotNull(system);
SysSystemMappingDto mapping = systemMappingService.findProvisioningMapping(system.getId(), SystemEntityType.IDENTITY);
Assert.assertNotNull(mapping);
// Create the description attribute (print context as string).
createDescriptionAttribute(system, mapping);
// Set context transformation to the mapping.
// Add identity roles for this system to the context.
mapping.setAddContextIdentityRolesForSystem(true);
mapping = initContextForMapping(mapping);
IdmRoleDto roleWithSystem = helper.createRole();
IdmRoleDto roleWithoutSystem = helper.createRole();
helper.createRoleSystem(roleWithSystem, system);
IdmIdentityDto identity = helper.createIdentity();
helper.createIdentityRole(identity, roleWithoutSystem, null, null);
IdmIdentityRoleDto identityRoleWithSystem = helper.createIdentityRole(identity, roleWithSystem, null, null);
IdmIdentityRoleFilter identityRoleFilter = new IdmIdentityRoleFilter();
identityRoleFilter.setIdentityId(identity.getId());
List<IdmIdentityRoleDto> identityRoles = identityRoleService.find(identityRoleFilter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmIdentityRole_.created.getName()))).getContent();
Assert.assertEquals(2, identityRoles.size());
TestResource resource = helper.findResource(identity.getUsername());
assertNotNull(resource);
assertEquals(identity.getFirstName(), resource.getFirstname());
MappingContext context = new MappingContext();
context.put("test", "TestValueOne");
context.setIdentityRolesForSystem(Lists.newArrayList(identityRoleWithSystem));
assertEquals(context.toString(), resource.getDescrip());
// Delete role mapping
systemMappingService.delete(mapping);
}
use of eu.bcvsolutions.idm.acc.domain.MappingContext in project CzechIdMng by bcvsolutions.
the class DefaultSysSystemAttributeMappingService method getAttributeValue.
/**
* Find value for this mapped attribute by property name. Returned value can be
* list of objects. Returns transformed value.
*
* @param uid - Account identifier
* @param entity
* @param attributeHandling
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
@Override
public Object getAttributeValue(String uid, AbstractDto entity, AttributeMapping attributeHandling, MappingContext mappingContext) {
Object idmValue = null;
//
if (attributeHandling.isPasswordAttribute()) {
// there but in PrepareConnectorObjectProcessor
return null;
}
//
SysSchemaAttributeDto schemaAttributeDto = getSchemaAttribute(attributeHandling);
//
if (attributeHandling.isExtendedAttribute() && entity != null && formService.isFormable(entity.getClass())) {
List<IdmFormValueDto> formValues = formService.getValues(entity, attributeHandling.getIdmPropertyName());
if (formValues.isEmpty()) {
idmValue = null;
} else if (schemaAttributeDto.isMultivalued()) {
// Multiple value extended attribute
List<Object> values = new ArrayList<>();
formValues.stream().forEachOrdered(formValue -> {
values.add(this.resolveFormValue(formValue));
});
idmValue = values;
} else {
// Single value extended attribute
IdmFormValueDto formValue = formValues.get(0);
if (formValue.isConfidential()) {
Object confidentialValue = formService.getConfidentialPersistentValue(formValue);
// then convert to GuardedString will be did.
if (confidentialValue instanceof String && schemaAttributeDto.getClassType().equals(GuardedString.class.getName())) {
idmValue = new GuardedString((String) confidentialValue);
} else {
idmValue = confidentialValue;
}
} else {
idmValue = this.resolveFormValue(formValue);
}
}
} else // Find value from entity
if (attributeHandling.isEntityAttribute()) {
if (attributeHandling.isConfidentialAttribute()) {
// If is attribute isConfidential, then we will find value in
// secured storage
idmValue = confidentialStorage.getGuardedString(entity.getId(), entity.getClass(), attributeHandling.getIdmPropertyName());
} else {
try {
// We will search value directly in entity by property name
idmValue = EntityUtils.getEntityValue(entity, attributeHandling.getIdmPropertyName());
} catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ProvisioningException ex) {
throw new ProvisioningException(AccResultCode.PROVISIONING_IDM_FIELD_NOT_FOUND, ImmutableMap.of("property", attributeHandling.getIdmPropertyName(), "entityType", entity.getClass(), "schemaAtribute", attributeHandling.getSchemaAttribute().toString()), ex);
}
}
} else {
// If Attribute value is not in entity nor in extended attribute, then idmValue
// is null.
// It means attribute is static ... we will call transformation to resource.
}
return this.transformValueToResource(uid, idmValue, attributeHandling, entity, mappingContext);
}
use of eu.bcvsolutions.idm.acc.domain.MappingContext in project CzechIdMng by bcvsolutions.
the class DefaultSysSystemMappingService method getMappingContext.
@Override
public MappingContext getMappingContext(SysSystemMappingDto mapping, SysSystemEntityDto systemEntity, AbstractDto dto, SysSystemDto system) {
Assert.notNull(mapping, "Mapping cannot be null!");
Assert.notNull(systemEntity, "System entity cannot be null!");
Assert.notNull(system, "System cannot be null!");
// Create new context.
MappingContext mappingContext = new MappingContext();
if (dto == null) {
return mappingContext;
}
if ((mapping.isAddContextIdentityRoles() || mapping.isAddContextIdentityRolesForSystem()) && dto instanceof IdmIdentityDto) {
IdmIdentityRoleFilter identityRoleFilter = new IdmIdentityRoleFilter();
identityRoleFilter.setIdentityId(dto.getId());
List<IdmIdentityRoleDto> identityRoles = identityRoleService.find(identityRoleFilter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmIdentityRole_.created.getName()))).getContent();
if (mapping.isAddContextIdentityRoles()) {
// Set all identity-roles to the context.
mappingContext.setIdentityRoles(identityRoles);
}
if (mapping.isAddContextIdentityRolesForSystem()) {
Assert.notNull(system.getId(), "System identifier is required.");
List<IdmIdentityRoleDto> identityRolesForSystem = Lists.newArrayList();
AccIdentityAccountFilter identityAccountFilter = new AccIdentityAccountFilter();
identityAccountFilter.setIdentityId(dto.getId());
identityAccountFilter.setSystemId(system.getId());
List<AccIdentityAccountDto> identityAccounts = identityAccountService.find(identityAccountFilter, null).getContent();
// Filtering only identity-roles for this system.
identityAccounts.forEach(identityAccount -> {
identityRolesForSystem.addAll(identityRoles.stream().filter(identityRole -> identityRole.getId().equals(identityAccount.getIdentityRole())).collect(Collectors.toList()));
});
// Set identity-roles for this system to the context.
mappingContext.setIdentityRolesForSystem(identityRolesForSystem);
}
}
if (mapping.isAddContextContracts() && dto instanceof IdmIdentityDto) {
// Set all identity contracts to the context.
mappingContext.setContracts(identityContractService.findAllByIdentity(dto.getId()));
}
if (mapping.isAddContextConnectorObject()) {
// Set connector object to the context.
mappingContext.setConnectorObject(systemEntityService.getConnectorObject(systemEntity));
}
String script = mapping.getMappingContextScript();
if (StringUtils.isEmpty(script)) {
return mappingContext;
} else {
Map<String, Object> variables = new HashMap<>();
variables.put(SysSystemAttributeMappingService.ACCOUNT_UID, systemEntity.getUid());
variables.put(SysSystemAttributeMappingService.SYSTEM_KEY, system);
variables.put(SysSystemAttributeMappingService.ENTITY_KEY, dto);
variables.put(SysSystemAttributeMappingService.CONTEXT_KEY, mappingContext);
// Add default script evaluator, for call another scripts
variables.put(AbstractScriptEvaluator.SCRIPT_EVALUATOR, pluginExecutors.getPluginFor(IdmScriptCategory.MAPPING_CONTEXT));
// Add access for script evaluator
List<Class<?>> extraClass = new ArrayList<>();
extraClass.add(AbstractScriptEvaluator.Builder.class);
extraClass.add(IcConnectorObject.class);
//
Object result = groovyScriptService.evaluate(script, variables, extraClass);
if (result instanceof MappingContext) {
return (MappingContext) result;
} else {
throw new ProvisioningException(AccResultCode.MAPPING_CONTEXT_SCRIPT_RETURNS_WRONG_TYPE, ImmutableMap.of("system", system.getCode()));
}
}
}
use of eu.bcvsolutions.idm.acc.domain.MappingContext in project CzechIdMng by bcvsolutions.
the class MappingContextTest method testMappingContextContract.
@Test
public void testMappingContextContract() {
SysSystemDto system = helper.createTestResourceSystem(true);
Assert.assertNotNull(system);
SysSystemMappingDto mapping = systemMappingService.findProvisioningMapping(system.getId(), SystemEntityType.IDENTITY);
Assert.assertNotNull(mapping);
// Create the description attribute (print context as string).
createDescriptionAttribute(system, mapping);
// Set context transformation to the mapping
// Add identity contracts to the context.
mapping.setAddContextContracts(true);
mapping = initContextForMapping(mapping);
IdmRoleDto roleWithSystem = helper.createRole();
helper.createRoleSystem(roleWithSystem, system);
IdmIdentityDto identity = helper.createIdentity();
helper.createContract(identity, null, LocalDate.now(), null);
List<IdmIdentityContractDto> contracts = identityContractService.findAllByIdentity(identity.getId());
Assert.assertEquals(2, contracts.size());
helper.createIdentityRole(identity, roleWithSystem, null, null);
TestResource resource = helper.findResource(identity.getUsername());
assertNotNull(resource);
assertEquals(identity.getFirstName(), resource.getFirstname());
MappingContext context = new MappingContext();
context.put("test", "TestValueOne");
context.setContracts(contracts);
assertEquals(context.toString(), resource.getDescrip());
// Delete role mapping
systemMappingService.delete(mapping);
}
Aggregations