Search in sources :

Example 16 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class AuthTokenServiceImpl method createAuthToken.

@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken createAuthToken(String userEmail, String clientIp, String tokenUuid, String tokenValueUuid) throws UserNotFoundException, FieldValidationException {
    Preconditions.checkArgument(userEmail != null);
    Preconditions.checkArgument(clientIp != null);
    Preconditions.checkArgument(StringUtils.hasText(tokenUuid));
    Preconditions.checkArgument(StringUtils.hasText(tokenValueUuid));
    try {
        User user = userService.getUserByEmail(userEmail);
        AuthToken authToken = buildNewAuthToken(user, clientIp, tokenUuid, tokenValueUuid);
        authTokenDao.createAuthToken(authToken);
        return authToken;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) AuthToken(org.summerb.microservices.users.api.dto.AuthToken) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class AuthTokenServiceImpl method validateAndGetUser.

private User validateAndGetUser(String userEmail, String passwordPlain) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
    try {
        User user = userService.getUserByEmail(userEmail);
        boolean isPasswordValid = passwordService.isUserPasswordValid(user.getUuid(), passwordPlain);
        if (!isPasswordValid) {
            throw new InvalidPasswordException();
        }
        return user;
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
        Throwables.throwIfInstanceOf(t, FieldValidationException.class);
        Throwables.throwIfInstanceOf(t, InvalidPasswordException.class);
        String msg = String.format("Failed to validate user '%s' and password '%s'", userEmail, passwordPlain);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) InvalidPasswordException(org.summerb.microservices.users.api.exceptions.InvalidPasswordException)

Example 18 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class PasswordServiceImpl method getNewRestorationTokenForUser.

@Override
@Transactional(rollbackFor = Throwable.class)
public String getNewRestorationTokenForUser(String userUuid) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    assertUserExists(userUuid);
    try {
        String restorationToken = UUID.randomUUID().toString();
        int updateResult = passwordDao.setRestorationToken(userUuid, restorationToken);
        if (updateResult != 1) {
            throw new RuntimeException("createRestorationToken returned unexpected result = " + updateResult);
        }
        return restorationToken;
    } catch (Throwable t) {
        String msg = String.format("Failed to create restoration token for user '%s'", userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class PasswordServiceImpl method isUserPasswordValid.

@Override
public boolean isUserPasswordValid(String userUuid, String passwordPlain) throws UserNotFoundException {
    Preconditions.checkArgument(userUuid != null);
    Preconditions.checkArgument(passwordPlain != null);
    assertUserExists(userUuid);
    try {
        Password password = passwordDao.findPasswordByUserUuid(userUuid);
        if (password == null) {
            return false;
        }
        if (!isPasswordMatch(passwordPlain, password.getPasswordHash())) {
            return false;
        }
    } catch (Throwable t) {
        String msg = String.format("Failed to validate user '%s' password", userUuid);
        throw new UserServiceUnexpectedException(msg, t);
    }
    return true;
}
Also used : UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) Password(org.summerb.microservices.users.impl.dom.Password)

Example 20 with UserServiceUnexpectedException

use of org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException in project summerb by skarpushin.

the class PermissionServiceImpl method revokePermission.

@Override
@Transactional(rollbackFor = Throwable.class)
public void revokePermission(String optionalDomainName, String userUuid, String optionalSubjectId, String permissionKey) {
    Preconditions.checkArgument(StringUtils.hasText(permissionKey));
    Preconditions.checkArgument(StringUtils.hasText(userUuid));
    String domainName = getOptionalParamValue(optionalDomainName);
    String subjectId = getOptionalParamValue(optionalSubjectId);
    try {
        permissionDao.revokePermission(domainName, userUuid, subjectId, permissionKey);
    } catch (Throwable t) {
        String msg = String.format("Failed to revoke permission '%s' from user '%s' for subject '%s' in domain '%s'", permissionKey, userUuid, subjectId, domainName);
        throw new UserServiceUnexpectedException(msg, t);
    }
}
Also used : UserServiceUnexpectedException(org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)27 Transactional (org.springframework.transaction.annotation.Transactional)18 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)11 User (org.summerb.microservices.users.api.dto.User)11 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)9 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)3 AuthTokenNotFoundException (org.summerb.microservices.users.api.exceptions.AuthTokenNotFoundException)3 InvalidPasswordException (org.summerb.microservices.users.api.exceptions.InvalidPasswordException)2 DuplicateUserValidationError (org.summerb.microservices.users.api.validation.DuplicateUserValidationError)2 Password (org.summerb.microservices.users.impl.dom.Password)2 Date (java.util.Date)1 UserStatus (org.summerb.approaches.springmvc.security.dto.UserStatus)1 RegistrationAlreadyRequestedValidationError (org.summerb.approaches.springmvc.security.ve.RegistrationAlreadyRequestedValidationError)1 ValidationError (org.summerb.approaches.validation.ValidationError)1 FieldRequiredValidationError (org.summerb.approaches.validation.errors.FieldRequiredValidationError)1