use of org.summerb.microservices.users.api.exceptions.UserNotFoundException in project summerb by skarpushin.
the class UsersServiceFacadeImpl method registerUser.
@Transactional(rollbackFor = Throwable.class)
@Override
public User registerUser(Registration registration) throws FieldValidationException {
try {
Preconditions.checkArgument(registration != null, "Registration param must be not null");
// Validate display name
validateRegistration(registration);
// Validate user status
UserStatus userStatus = getUserStatusByEmail(registration.getEmail());
if (userStatus == UserStatus.AwaitingActivation) {
throw new FieldValidationException(new RegistrationAlreadyRequestedValidationError());
}
// Create user
User user = null;
if (userStatus == UserStatus.Provisioned) {
user = userService.getUserByEmail(registration.getEmail());
user.setDisplayName(registration.getDisplayName());
user.setLocale(CurrentRequestUtils.getLocale().toString());
user.setTimeZone(TimeZone.getDefault().getID());
userService.updateUser(user);
} else {
user = new User();
user.setEmail(registration.getEmail());
user.setDisplayName(registration.getDisplayName());
user.setLocale(CurrentRequestUtils.getLocale().toString());
user.setTimeZone(TimeZone.getDefault().getID());
user = userService.createUser(user);
}
// Create password
passwordService.setUserPassword(user.getUuid(), registration.getPassword());
// Create user account permissions
permissionService.grantPermission(SecurityConstants.DOMAIN, user.getUuid(), null, SecurityConstants.MARKER_AWAITING_ACTIVATION);
runUserRegisteredHandler(user);
//
return user;
} catch (UserNotFoundException e) {
throw new UserServiceUnexpectedException("User was just created, but not found", e);
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
throw new RuntimeException("Unexpected error while registering user", t);
}
}
use of org.summerb.microservices.users.api.exceptions.UserNotFoundException 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;
}
use of org.summerb.microservices.users.api.exceptions.UserNotFoundException in project summerb by skarpushin.
the class AuthTokenServiceImpl method authenticate.
@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken authenticate(String userEmail, String passwordPlain, String clientIp) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
Preconditions.checkArgument(userEmail != null);
Preconditions.checkArgument(passwordPlain != null);
Preconditions.checkArgument(clientIp != null);
try {
User user = validateAndGetUser(userEmail, passwordPlain);
return createAuthToken(user.getEmail(), clientIp, UUID.randomUUID().toString(), UUID.randomUUID().toString());
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
Throwables.throwIfInstanceOf(t, InvalidPasswordException.class);
String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.microservices.users.api.exceptions.UserNotFoundException in project summerb by skarpushin.
the class AuthTokenServiceImpl method isAuthTokenValid.
@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken isAuthTokenValid(String userUuid, String authTokenUuid, String tokenValue) throws UserNotFoundException {
Preconditions.checkArgument(userUuid != null);
Preconditions.checkArgument(authTokenUuid != null);
Preconditions.checkArgument(StringUtils.hasText(tokenValue), "TokenValue is mandatory");
try {
// First - check token itself
AuthToken authToken = getAuthTokenByUuid(authTokenUuid);
if (authToken.getExpiresAt() < getNow()) {
authTokenDao.deleteAuthToken(authTokenUuid);
return null;
}
if (!tokenValue.equals(authToken.getTokenValue())) {
return null;
}
// Check reference to user
User user = userService.getUserByUuid(userUuid);
if (!authToken.getUserUuid().equals(user.getUuid())) {
return null;
}
// Now we need to update time when token was checked
authToken.setTokenValue(UUID.randomUUID().toString());
authToken.setLastVerifiedAt(getNow());
authTokenDao.updateToken(authTokenUuid, authToken.getLastVerifiedAt(), authToken.getTokenValue());
return authToken;
} catch (AuthTokenNotFoundException nfe) {
return null;
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
String msg = String.format("Failed to check auth token '%s' validity for user '%s'", authTokenUuid, userUuid);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.microservices.users.api.exceptions.UserNotFoundException in project summerb by skarpushin.
the class UserServiceImpl method getUserByEmail.
@Override
public User getUserByEmail(String userEmail) throws FieldValidationException, UserNotFoundException {
Assert.notNull(userEmail, "user email must be provided");
validateEmail(userEmail);
User foundUser;
try {
foundUser = userDao.findUserByEmail(userEmail);
} catch (Throwable t) {
String msg = String.format("Failed to find user by email '%s'", userEmail);
throw new UserServiceUnexpectedException(msg, t);
}
if (foundUser == null) {
throw new UserNotFoundException(userEmail);
}
return foundUser;
}
Aggregations