use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class DefaultPasswordFilterManager method evaluateUsernameToIdentity.
/**
* Process all given information from {@link AccPasswordFilterRequestDto} and {@link AccUniformPasswordDto} and then
* evaluate {@link IdmIdentityDto} thought very defensive behavior:
*
* 1. - check script for identity transformation,
* 2. - check if exist UID in given system,
* 3. - check identities username's.
*
* @param system
* @param request
* @param passwordFilterAttribute
* @return
*/
protected IdmIdentityDto evaluateUsernameToIdentity(SysSystemDto system, AccPasswordFilterRequestDto request, SysSystemAttributeMappingDto passwordFilterAttribute) {
String script = passwordFilterAttribute.getTransformationUidScript();
String usernameRequest = request.getUsername();
if (StringUtils.isBlank(script)) {
// First we will try find account by uid
AccAccountDto account = accountService.getAccount(usernameRequest, system.getId());
if (account == null) {
// Second we will try find direct identity by username
IdmIdentityDto identityDto = identityService.getByUsername(usernameRequest);
if (identityDto == null) {
LOG.error("Identity for request for username [{}] and system [{}] cannot be found. {}", usernameRequest, system.getId(), request.getLogMetadata());
throw new ResultCodeException(AccResultCode.PASSWORD_FILTER_IDENTITY_NOT_FOUND, ImmutableMap.of("identifier", usernameRequest));
}
return identityDto;
}
IdmIdentityDto identityDto = identityService.get(account.getTargetEntityId());
if (identityDto == null) {
LOG.error("Identity for request for username [{}], system [{}] and account id [{}] cannot be found. {}", usernameRequest, system.getId(), account.getId(), request.getLogMetadata());
throw new ResultCodeException(AccResultCode.PASSWORD_FILTER_IDENTITY_NOT_FOUND, ImmutableMap.of("identifier", usernameRequest));
}
return identityDto;
}
// Standard behavior with script
Map<String, Object> variables = new HashMap<>();
variables.put(SCRIPT_SYSTEM_PARAMETER, system);
variables.put(SCRIPT_USERNAME_PARAMETER, request.getUsername());
variables.put(SCRIPT_LOG_IDENTIFIER_PARAMETER, request.getLogIdentifier());
variables.put(SCRIPT_SYSTEM_ATTRIBUTE_MAPPING_PARAMETER, passwordFilterAttribute);
// Add system script evaluator for call another scripts
variables.put(AbstractScriptEvaluator.SCRIPT_EVALUATOR, scriptEvaluator);
// Add access for script evaluator
List<Class<?>> extraClass = new ArrayList<>();
extraClass.add(AbstractScriptEvaluator.Builder.class);
extraClass.add(IdmIdentityDto.class);
extraClass.add(SysSystemDto.class);
extraClass.add(SysSystemAttributeMappingDto.class);
Object result = groovyScriptService.evaluate(script, variables, extraClass);
if (result instanceof IdmIdentityDto) {
return (IdmIdentityDto) result;
} else {
throw new ResultCodeException(AccResultCode.PASSWORD_FILTER_IDENTITY_NOT_FOUND, ImmutableMap.of("identifier", usernameRequest));
}
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class DefaultPasswordFilterManager method getAttributeMappingForPasswordFilter.
/**
* Get {@link SysSystemAttributeMappingDto} that define configuration for password filter.
*
* @param system
* @return
*/
private SysSystemAttributeMappingDto getAttributeMappingForPasswordFilter(SysSystemDto system) {
SysSystemAttributeMappingFilter filter = new SysSystemAttributeMappingFilter();
filter.setSystemId(system.getId());
filter.setPasswordAttribute(Boolean.TRUE);
filter.setPasswordFilter(Boolean.TRUE);
List<SysSystemAttributeMappingDto> content = systemAttributeMappingService.find(filter, null).getContent();
if (content.isEmpty()) {
throw new ResultCodeException(AccResultCode.PASSWORD_FILTER_DEFINITION_NOT_FOUND, ImmutableMap.of("systemId", system.getId()));
}
// Attribute with password filter may be only one!
return content.get(0);
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class AbstractReadWriteDtoController method patch.
/**
* Patch is not implemented yet
*
* @param backendId
* @param nativeRequest
* @return
* @throws HttpMessageNotReadableException
*/
public ResponseEntity<?> patch(String backendId, HttpServletRequest nativeRequest) throws HttpMessageNotReadableException {
DTO updateDto = getDto(backendId);
if (updateDto == null) {
throw new EntityNotFoundException(getService().getEntityClass(), backendId);
}
//
ServletServerHttpRequest request = new ServletServerHttpRequest(nativeRequest);
try {
modelMapper.map(getMapper().readerForUpdating(updateDto).readValue(request.getBody()), updateDto);
} catch (IOException ex) {
throw new ResultCodeException(CoreResultCode.BAD_REQUEST, ex);
}
updateDto = patchDto(updateDto);
return new ResponseEntity<>(toResource(updateDto), HttpStatus.OK);
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class AbstractFormProjectionLookup method getFormDefinition.
/**
* Get overriden / configured form definition by projection.
* @param dto projection owner
* @param formDefinition form definition to load
* @return overriden form definition
*
* @since 12.0.0
*/
protected IdmFormDefinitionDto getFormDefinition(DTO dto, IdmFormDefinitionDto formDefinition) {
IdmFormProjectionDto formProjection = lookupProjection(dto);
if (formProjection == null) {
return null;
}
String formValidations = formProjection.getFormValidations();
if (StringUtils.isEmpty(formValidations)) {
return null;
}
//
if (formDefinition == null) {
// ~ basic fields
formDefinition = new IdmFormDefinitionDto();
formDefinition.setCode(FormService.FORM_DEFINITION_CODE_BASIC_FIELDS);
}
// clone ~ prevent to change input (e.g. cache can be modified)
IdmFormDefinitionDto overridenDefinition = new IdmFormDefinitionDto();
overridenDefinition.setId(formDefinition.getId());
overridenDefinition.setCode(formDefinition.getCode());
// transform form attributes from json
try {
List<IdmFormAttributeDto> attributes = mapper.readValue(formValidations, new TypeReference<List<IdmFormAttributeDto>>() {
});
attributes.stream().filter(attribute -> Objects.equals(attribute.getFormDefinition(), overridenDefinition.getId())).forEach(attribute -> {
if (attribute.getId() == null) {
// we need artificial id to find attributes in definition / instance
attribute.setId(UUID.randomUUID());
}
overridenDefinition.addFormAttribute(attribute);
});
//
return overridenDefinition;
} catch (IOException ex) {
throw new ResultCodeException(CoreResultCode.FORM_PROJECTION_WRONG_VALIDATION_CONFIGURATION, ImmutableMap.of("formProjection", formProjection.getCode()), ex);
}
}
use of eu.bcvsolutions.idm.core.api.exception.ResultCodeException in project CzechIdMng by bcvsolutions.
the class AbstractFormableService method getOwner.
/**
* Prepares new owner instance
*
* TODO: move to form service, should be in api?
*
* @param formDefinition
* @return
*/
private FormableEntity getOwner(DTO dto) {
Assert.notNull(dto, "DTO is required for get owner.");
//
FormableEntity formableEntity = null;
if (dto.getId() != null) {
formableEntity = (FormableEntity) lookupService.lookupEntity(dto.getClass(), dto.getId());
}
// prepare empty owner
if (formableEntity == null) {
try {
formableEntity = (FormableEntity) lookupService.getEntityClass(dto.getClass()).getDeclaredConstructor().newInstance();
// FIXME: #978 - map dto to entity. Some evaluator could intercept something else than class and identifier ...
formableEntity.setId(dto.getId());
} catch (ReflectiveOperationException ex) {
throw new ResultCodeException(CoreResultCode.BAD_VALUE, ImmutableMap.of("identifiableType", dto.getClass()), ex);
}
}
//
return formableEntity;
}
Aggregations