use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManager method getEnabledDistinctPolicies.
/**
* Cache decorator - get or load current identity authorization policies.
* Distinct policies are returned only
*
* @param identityId
* @param entityType
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected List<IdmAuthorizationPolicyDto> getEnabledDistinctPolicies(UUID identityId, Class<? extends Identifiable> entityType) {
if (identityId == null) {
// TODO: support setting policies to not logged user - e.g. public endpoints.
return Lists.newArrayList();
}
Assert.notNull(entityType, "Entity type is required.");
//
// try to get cached policies
Map<Class<? extends Identifiable>, List<UUID>> cachedPolicies;
ValueWrapper value = cacheManager.getValue(AUTHORIZATION_POLICY_CACHE_NAME, identityId);
if (value != null) {
// cache value is never null - create copy
cachedPolicies = new HashMap<>((Map) value.get());
} else {
cachedPolicies = new HashMap<>();
}
if (cachedPolicies.containsKey(entityType)) {
// cache contains policy identifiers only -> get policy dto
return cachedPolicies.get(entityType).stream().map(policyId -> getAuthorizationPolicy(policyId)).collect(Collectors.toList());
}
// distinct policies
List<IdmAuthorizationPolicyDto> enabledDistinctPolicies = new ArrayList<>();
// load policies
service.getEnabledPolicies(identityId, entityType).stream().filter(// TODO: compatibility issues - agendas without authorization support
p -> supportsEntityType(p, entityType)).forEach(policy -> {
boolean contains = false;
for (IdmAuthorizationPolicyDto registeredPolicy : enabledDistinctPolicies) {
if (isDuplicate(policy, registeredPolicy)) {
// policy with the same configuration is already registered
contains = true;
break;
}
}
// register policy
if (!contains) {
enabledDistinctPolicies.add(policy);
}
// cache all policies (even duplicates to prevent select)
cacheManager.cacheValue(AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policy.getId(), policy);
});
// cache policies as uuid
cachedPolicies.put(entityType, // dto => uuid
enabledDistinctPolicies.stream().map(AbstractDto::getId).collect(Collectors.toList()));
cacheManager.cacheValue(AUTHORIZATION_POLICY_CACHE_NAME, identityId, cachedPolicies);
//
return enabledDistinctPolicies;
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto 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.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method isDuplicate.
/**
* Returns true, when events are duplicates
* - event type, parent event type, properties and original source is compared => all properties, which can be used in processors.
*
* @param olderEvent
* @param event
* @return
*/
protected boolean isDuplicate(IdmEntityEventDto olderEvent, IdmEntityEventDto event) {
Assert.notNull(olderEvent, "Older event is required.");
Assert.notNull(event, "Event is required.");
//
boolean result = Objects.equal(olderEvent.getEventType(), event.getEventType()) && Objects.equal(olderEvent.getParentEventType(), event.getParentEventType()) && Objects.equal(getProperties(olderEvent), getProperties(event));
if (!result) {
// we can end - events are different
return false;
}
if (olderEvent.getOriginalSource() == null && event.getOriginalSource() == null) {
return true;
}
// previous content to processing (e.g. contract processors).
if (!Objects.equal(olderEvent.getOriginalSource(), event.getOriginalSource())) {
return false;
}
if (!(olderEvent.getOriginalSource() instanceof AbstractDto)) {
// Evaluated already by 'equal' method above.
return false;
}
// If both original sources are DTOs, then we are comparing original sources (DTOs) as JSON without embedded.
AbstractDto olderOriginalSource = (AbstractDto) olderEvent.getOriginalSource();
AbstractDto originalSource = (AbstractDto) event.getOriginalSource();
try {
// Prevent to change event setting => defensive copy by mapper.
AbstractDto olderOriginalSourceCopy = olderOriginalSource.getClass().getDeclaredConstructor().newInstance();
modelMapper.map(olderOriginalSource, olderOriginalSourceCopy);
AbstractDto originalSourceCopy = originalSource.getClass().getDeclaredConstructor().newInstance();
modelMapper.map(originalSource, originalSourceCopy);
// Embedded is ignored.
olderOriginalSourceCopy.setEmbedded(null);
originalSourceCopy.setEmbedded(null);
// Audit fields are ignored.
DtoUtils.clearAuditFields(olderOriginalSourceCopy);
DtoUtils.clearAuditFields(originalSourceCopy);
//
return mapper.writeValueAsString(olderOriginalSourceCopy).equals(mapper.writeValueAsString(originalSourceCopy));
} catch (JsonProcessingException | ReflectiveOperationException ex) {
LOG.warn("Comparing json for checking duplicate events failed - both events [{}]-[{}] will be executed!", olderEvent, event, ex);
//
return false;
}
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class DefaultEntityEventManager method process.
@Override
@Transactional
@SuppressWarnings("unchecked")
public <E extends Serializable> EventContext<E> process(EntityEvent<E> event, EntityEvent<?> parentEvent) {
Assert.notNull(event, "Event is required for processing.");
Serializable content = event.getContent();
//
LOG.info("Publishing event [{}]", event);
//
// continue suspended event
event.getContext().setSuspended(false);
//
if (parentEvent != null) {
event.setParentId(parentEvent.getId());
event.setRootId(parentEvent.getRootId() == null ? parentEvent.getId() : parentEvent.getRootId());
if (parentEvent.getPriority() != null && (event.getPriority() == null || event.getPriority().getPriority() < parentEvent.getPriority().getPriority())) {
// parent has higher priority ... execute with the same priority as parent
event.setPriority(parentEvent.getPriority());
}
// parent event type can be preset manually
if (StringUtils.isEmpty(event.getParentType())) {
event.setParentType(parentEvent.getType().name());
}
// propagate properties from parent to child event.
// properties need for internal event processing are ignored (see {@link EntityEvent} properties)
propagateProperties(event, parentEvent);
}
// read previous (original) dto source - usable in "check modification" processors
if (event.getOriginalSource() == null && (content instanceof AbstractDto)) {
// original source could be set externally
AbstractDto contentDto = (AbstractDto) content;
// works only for dto modification
if (contentDto.getId() != null && lookupService.getDtoLookup(contentDto.getClass()) != null) {
event.setOriginalSource((E) lookupService.lookupDto(contentDto.getClass(), contentDto.getId()));
}
}
// event is persisted automatically, when parent event is persisted
try {
if (content instanceof BaseDto && event.getId() == null && event.getParentId() != null && lookupService.getEntityClass(((BaseDto) content).getClass()) != null) {
// entity event can be persisted into queue only
BaseDto dto = (BaseDto) content;
if (dto.getId() == null) {
// prepare id for new content - event is persisted before entity is persisted.
dto.setId(UUID.randomUUID());
}
//
IdmEntityEventDto preparedEvent = toDto(dto, (EntityEvent<AbstractDto>) event);
// RUNNING => prevent to start by async task
preparedEvent.setResult(new OperationResultDto.Builder(OperationState.RUNNING).build());
preparedEvent.setRootId(event.getRootId() == null ? event.getParentId() : event.getRootId());
preparedEvent = entityEventService.save(preparedEvent);
event.setId(preparedEvent.getId());
//
// prepared event is be executed
CoreEvent<IdmEntityEventDto> executeEvent = new CoreEvent<>(EntityEventType.EXECUTE, preparedEvent);
publisher.publishEvent(executeEvent);
//
// fill original event result
E processedContent = (E) preparedEvent.getContent();
if (processedContent != null) {
event.setContent(processedContent);
}
event.getContext().addResult(new DefaultEventResult<E>(event, new EmptyEntityEventProcessor<E>()));
//
return completeEvent(event);
} else {
publisher.publishEvent(event);
//
return completeEvent(event);
}
} catch (Exception ex) {
completeEvent(event);
//
throw ex;
}
}
use of eu.bcvsolutions.idm.core.api.dto.AbstractDto in project CzechIdMng by bcvsolutions.
the class AbstractConnectorType method load.
@Override
public ConnectorTypeDto load(ConnectorTypeDto connectorType) {
if (!connectorType.isReopened()) {
// Load default value for new system.
connectorType.setMetadata(this.getMetadata());
return connectorType;
}
// Load the system.
SysSystemDto systemDto = (SysSystemDto) connectorType.getEmbedded().get(SYSTEM_DTO_KEY);
Assert.notNull(systemDto, "System must exists!");
connectorType.getEmbedded().put(SYSTEM_DTO_KEY, systemDto);
// Set remote server ID to the connector type.
connectorType.setRemoteServer(systemDto.getRemoteServer());
// Load the mapping.
SysSystemMappingFilter mappingFilter = new SysSystemMappingFilter();
mappingFilter.setSystemId(systemDto.getId());
List<SysSystemMappingDto> mappingDtos = systemMappingService.find(mappingFilter, null).getContent().stream().sorted(Comparator.comparing(SysSystemMappingDto::getCreated)).collect(Collectors.toList());
// Show alert if more mappings exists.
if (mappingDtos.size() > 1) {
connectorType.getMetadata().put(ALERT_MORE_MAPPINGS, Boolean.TRUE.toString());
}
SysSystemMappingDto mappingDto = mappingDtos.stream().findFirst().orElse(null);
connectorType.getEmbedded().put(MAPPING_DTO_KEY, mappingDto);
// Load the sync.
SysSyncConfigFilter syncFilter = new SysSyncConfigFilter();
syncFilter.setSystemId(systemDto.getId());
if (mappingDto != null) {
syncFilter.setSystemMappingId(mappingDto.getId());
}
AbstractSysSyncConfigDto syncDto = syncConfigService.find(syncFilter, null).getContent().stream().min(Comparator.comparing(AbstractDto::getCreated)).orElse(null);
connectorType.getEmbedded().put(SYNC_DTO_KEY, syncDto);
return connectorType;
}
Aggregations