Search in sources :

Example 16 with ResultCodeException

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));
    }
}
Also used : HashMap(java.util.HashMap) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ArrayList(java.util.ArrayList) AbstractScriptEvaluator(eu.bcvsolutions.idm.core.script.evaluator.AbstractScriptEvaluator) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) AccAccountDto(eu.bcvsolutions.idm.acc.dto.AccAccountDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)

Example 17 with ResultCodeException

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);
}
Also used : SysSystemAttributeMappingFilter(eu.bcvsolutions.idm.acc.dto.filter.SysSystemAttributeMappingFilter) SysSystemAttributeMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException)

Example 18 with ResultCodeException

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);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ResponseEntity(org.springframework.http.ResponseEntity) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IOException(java.io.IOException)

Example 19 with ResultCodeException

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);
    }
}
Also used : IdmFormProjectionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormProjectionDto) ImmutableMap(com.google.common.collect.ImmutableMap) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) IdmFormProjectionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormProjectionDto) UUID(java.util.UUID) FormService(eu.bcvsolutions.idm.core.eav.api.service.FormService) StringUtils(org.apache.commons.lang3.StringUtils) Serializable(java.io.Serializable) PersistentType(eu.bcvsolutions.idm.core.eav.api.domain.PersistentType) Objects(java.util.Objects) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) List(java.util.List) IdmFormValueDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormValueDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) LocalDate(java.time.LocalDate) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) TypeReference(com.fasterxml.jackson.core.type.TypeReference) GenericTypeResolver(org.springframework.core.GenericTypeResolver) Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) List(java.util.List) IOException(java.io.IOException)

Example 20 with ResultCodeException

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;
}
Also used : ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) FormableEntity(eu.bcvsolutions.idm.core.eav.api.entity.FormableEntity)

Aggregations

ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)430 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)107 ApiOperation (io.swagger.annotations.ApiOperation)104 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)101 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)99 UUID (java.util.UUID)90 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)89 Test (org.junit.Test)70 Transactional (org.springframework.transaction.annotation.Transactional)54 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)53 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)53 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)49 IOException (java.io.IOException)48 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)47 ResponseEntity (org.springframework.http.ResponseEntity)43 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)38 ArrayList (java.util.ArrayList)33 HashMap (java.util.HashMap)31 IdmPasswordPolicyDto (eu.bcvsolutions.idm.core.api.dto.IdmPasswordPolicyDto)27 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)26