Search in sources :

Example 6 with UserUsageConditions

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();
}
Also used : UserUsageConditions(org.haiku.haikudepotserver.dataobjects.UserUsageConditions) User(org.haiku.haikudepotserver.dataobjects.User) UserUsageConditionsAgreement(org.haiku.haikudepotserver.dataobjects.UserUsageConditionsAgreement) ObjectContext(org.apache.cayenne.ObjectContext)

Aggregations

UserUsageConditions (org.haiku.haikudepotserver.dataobjects.UserUsageConditions)6 ObjectContext (org.apache.cayenne.ObjectContext)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions (org.haiku.haikudepotserver.dataobjects.auto._UserUsageConditions)2 Resource (org.springframework.core.io.Resource)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Charsets (com.google.common.base.Charsets)1 Preconditions (com.google.common.base.Preconditions)1 Files (com.google.common.io.Files)1 Document (com.vladsch.flexmark.util.ast.Document)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Set (java.util.Set)1