Search in sources :

Example 1 with ValidationContext

use of org.summerb.approaches.validation.ValidationContext in project summerb by skarpushin.

the class UsersServiceFacadeImpl method getUserStatusByEmail.

@Override
public UserStatus getUserStatusByEmail(String email) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    ctx.validateNotEmpty(email, LoginParams.FN_EMAIL);
    ctx.throwIfHasErrors();
    // Check if user have record
    User user = null;
    try {
        user = userService.getUserByEmail(email);
    } catch (UserNotFoundException nfe) {
        return UserStatus.NotExists;
    }
    // Check if user has ROLE_USER
    List<String> permissions = permissionService.findUserPermissionsForSubject(SecurityConstants.DOMAIN, user.getUuid(), null);
    if (permissions.contains(SecurityConstants.ROLE_USER)) {
        return UserStatus.NormalUser;
    }
    if (permissions.contains(SecurityConstants.MARKER_AWAITING_ACTIVATION)) {
        return UserStatus.AwaitingActivation;
    }
    return UserStatus.Provisioned;
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) User(org.summerb.microservices.users.api.dto.User) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 2 with ValidationContext

use of org.summerb.approaches.validation.ValidationContext in project summerb by skarpushin.

the class UsersServiceFacadeImpl method validateUserIsEligableForPasswordReset.

protected void validateUserIsEligableForPasswordReset(String email) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    if (!ctx.validateEmailFormat(email, User.FN_EMAIL)) {
        throw new FieldValidationException(ctx.getErrors());
    }
    // now see if this user exists
    UserStatus userStatus = getUserStatusByEmail(email);
    if (userStatus == UserStatus.NotExists || userStatus == UserStatus.Provisioned) {
        throw new FieldValidationException(new RegistrationRequiredValidationError());
    }
    // Sanity check
    if (userStatus != UserStatus.NormalUser && userStatus != UserStatus.AwaitingActivation) {
        throw new RuntimeException("Password Reset scenario is not supported for user whose status is: " + userStatus);
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) RegistrationRequiredValidationError(org.summerb.approaches.springmvc.security.ve.RegistrationRequiredValidationError) UserStatus(org.summerb.approaches.springmvc.security.dto.UserStatus) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 3 with ValidationContext

use of org.summerb.approaches.validation.ValidationContext in project summerb by skarpushin.

the class UsersServiceFacadeImpl method validateRegistration.

protected void validateRegistration(Registration registration) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    validateDisplayName(registration.getDisplayName(), ctx);
    validatePassword(registration.getPassword(), ctx);
    ctx.validateEmailFormat(registration.getEmail(), User.FN_EMAIL);
    ctx.throwIfHasErrors();
}
Also used : ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 4 with ValidationContext

use of org.summerb.approaches.validation.ValidationContext in project summerb by skarpushin.

the class UsersServiceFacadeImpl method validatePasswordChangeRequestValid.

protected User validatePasswordChangeRequestValid(String email, PasswordChange passwordChange) throws UserNotFoundException, FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    ctx.lengthEqOrGreater(passwordChange.getPassword(), 4, LoginParams.FN_PASSWORD);
    ctx.equals(passwordChange.getPassword(), SecurityMessageCodes.NEW_PASSWORD, passwordChange.getNewPasswordAgain(), SecurityMessageCodes.NEW_PASSWORD_AGAIN, PasswordChange.FN_NEW_PASSWORD_AGAIN);
    User user = null;
    if (ctx.hasText(passwordChange.getCurrentPassword(), PasswordChange.FN_CURRENT_PASSWORD)) {
        user = userService.getUserByEmail(email);
        ctx.isTrue(passwordService.isUserPasswordValid(user.getUuid(), passwordChange.getCurrentPassword()), InvalidPasswordException.ERROR_LOGIN_INVALID_PASSWORD, PasswordChange.FN_CURRENT_PASSWORD);
    }
    ctx.throwIfHasErrors();
    return user;
}
Also used : User(org.summerb.microservices.users.api.dto.User) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 5 with ValidationContext

use of org.summerb.approaches.validation.ValidationContext in project summerb by skarpushin.

the class StringTemplateValidationUtilsTest method testValidateStringTemplate_blackbox_expectValidationErrorForWrongExpression.

@Test
public void testValidateStringTemplate_blackbox_expectValidationErrorForWrongExpression() throws Exception {
    StringTemplateCompiler stringTemplateCompiler = new StringTemplateCompilerlImpl();
    String stringTemplate = "Constant text plus ${vars['4444";
    ValidationContext ctx = new ValidationContext();
    String fieldToken = "fieldToken";
    StringTemplateValidationUtils.validateStringTemplate(stringTemplateCompiler, stringTemplate, ctx, fieldToken);
    assertEquals(true, ctx.getHasErrors());
}
Also used : StringTemplateCompilerlImpl(org.summerb.utils.stringtemplate.impl.StringTemplateCompilerlImpl) StringTemplateCompiler(org.summerb.utils.stringtemplate.api.StringTemplateCompiler) ValidationContext(org.summerb.approaches.validation.ValidationContext) Test(org.junit.Test)

Aggregations

ValidationContext (org.summerb.approaches.validation.ValidationContext)12 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)5 Test (org.junit.Test)3 StringTemplateCompiler (org.summerb.utils.stringtemplate.api.StringTemplateCompiler)3 StringTemplateCompilerlImpl (org.summerb.utils.stringtemplate.impl.StringTemplateCompilerlImpl)3 User (org.summerb.microservices.users.api.dto.User)2 UserStatus (org.summerb.approaches.springmvc.security.dto.UserStatus)1 PasswordsDontMatchValidationError (org.summerb.approaches.springmvc.security.ve.PasswordsDontMatchValidationError)1 RegistrationRequiredValidationError (org.summerb.approaches.springmvc.security.ve.RegistrationRequiredValidationError)1 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)1