use of org.haiku.haikudepotserver.dataobjects.UserUsageConditions in project haikudepotserver by haiku.
the class UserApiImpl method createUser.
@Override
public CreateUserResult createUser(CreateUserRequest createUserRequest) throws InvalidUserUsageConditionsException {
Preconditions.checkNotNull(createUserRequest);
Preconditions.checkState(!Strings.isNullOrEmpty(createUserRequest.nickname));
Preconditions.checkState(!Strings.isNullOrEmpty(createUserRequest.passwordClear));
Preconditions.checkState(!Strings.isNullOrEmpty(createUserRequest.captchaToken));
Preconditions.checkState(!Strings.isNullOrEmpty(createUserRequest.captchaResponse), "a capture response is required to create a user");
Preconditions.checkState(!Strings.isNullOrEmpty(createUserRequest.naturalLanguageCode));
if (!userAuthenticationService.validatePassword(createUserRequest.passwordClear)) {
throw new ValidationException(new ValidationFailure("passwordClear", "invalid"));
}
if (!captchaService.verify(createUserRequest.captchaToken, createUserRequest.captchaResponse)) {
throw new CaptchaBadResponseException();
}
if (Strings.isNullOrEmpty(createUserRequest.nickname)) {
throw new ValidationException(new ValidationFailure(User.NICKNAME.getName(), "required"));
}
if (StringUtils.isBlank(createUserRequest.userUsageConditionsCode)) {
throw new InvalidUserUsageConditionsException();
}
final ObjectContext context = serverRuntime.newContext();
String latestUserUsageConditionsCode = UserUsageConditions.getLatest(context).getCode();
if (!latestUserUsageConditionsCode.equals(createUserRequest.userUsageConditionsCode)) {
throw new InvalidUserUsageConditionsException();
}
UserUsageConditions userUsageConditions = UserUsageConditions.getByCode(context, createUserRequest.userUsageConditionsCode);
if (User.tryGetByNickname(context, createUserRequest.nickname).isPresent()) {
throw new ValidationException(new ValidationFailure(User.NICKNAME.getName(), "notunique"));
}
User user = context.newObject(User.class);
user.setNaturalLanguage(getNaturalLanguage(context, createUserRequest.naturalLanguageCode));
user.setNickname(createUserRequest.nickname);
user.setEmail(createUserRequest.email);
userAuthenticationService.setPassword(user, createUserRequest.passwordClear);
UserUsageConditionsAgreement agreement = context.newObject(UserUsageConditionsAgreement.class);
agreement.setUser(user);
agreement.setTimestampAgreed();
agreement.setUserUsageConditions(userUsageConditions);
context.commitChanges();
LOGGER.info("data create user; {}", user.getNickname());
return new CreateUserResult();
}
Aggregations