if (isEmpty(password) || isEmpty(alias) || isEmpty(fullname) || isEmpty(accountName)) {
return error(ErrorMessages.MISSING_REQUIRED_FIELD, Response.status(Response.Status.BAD_REQUEST));
}
User user;
try {
// validate password
if (!SecurityUtil.isValidPassword(password)) {
return error(ErrorMessages.INVALID_PASSWORD_ERROR, Response.status(Response.Status.BAD_REQUEST));
}
user = userService.getUserFromSignupKey(signupKey, userId);
// this account is already active!
if (user.getUserStatus().equals(User.UserStatus.ACTIVATED)) {
return error("User is already activated", Response.status(Response.Status.NOT_ACCEPTABLE));
}
user.setPassword(password);
user.setFullname(fullname);
user.setAlias(alias); // no need to check, you are the first user
// user can now login, the account is "activated"
user.setUserLocked(false);
// we want to make this a one time thing, blow away the secret key
user.setSecretKey(null);
Account account = new Account.Builder()
.name(accountName)
.build();
// create the account and fire events
userService.createAccount(account);
user.setAccount(account);
// create the user and fire events
userService.createUser(user);
} catch (UserNotFoundException e) {
return error("Invalid Signup " + signupKey + ":" + userId, Response.status(Response.Status.NOT_FOUND));
} catch (InvalidUserAliasException e) {
ConstraintViolationExceptionResponseDTO dto = new ConstraintViolationExceptionResponseDTO();
dto.setViolations(ImmutableMap.of("alias", e.getMessage()));
return Response.status(Response.Status.BAD_REQUEST).entity(dto).build();
}
return Response
.status(Response.Status.CREATED)
.entity(user.getUsername()) // This should probably be some sort of response DTO but due to time this is what you'll get.
.build();
}