Search in sources :

Example 1 with UserUsageConditionsAgreement

use of org.haiku.haikudepotserver.dataobjects.UserUsageConditionsAgreement 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)

Example 2 with UserUsageConditionsAgreement

use of org.haiku.haikudepotserver.dataobjects.UserUsageConditionsAgreement in project haikudepotserver by haiku.

the class UserApiImpl method agreeUserUsageConditions.

@Override
public AgreeUserUsageConditionsResult agreeUserUsageConditions(AgreeUserUsageConditionsRequest request) {
    Preconditions.checkNotNull(request);
    Preconditions.checkArgument(StringUtils.isNotBlank(request.nickname));
    Preconditions.checkArgument(StringUtils.isNotBlank(request.userUsageConditionsCode));
    final ObjectContext context = serverRuntime.newContext();
    User user = User.getByNickname(context, request.nickname);
    if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), user, Permission.USER_AGREE_USAGE_CONDITIONS)) {
        throw new AccessDeniedException("unable to agree user usage conditions for user [" + user + "]");
    }
    // remove any existing agreement
    user.getUserUsageConditionsAgreements().forEach(uuca -> uuca.setActive(false));
    UserUsageConditionsAgreement agreement = context.newObject(UserUsageConditionsAgreement.class);
    agreement.setUser(user);
    agreement.setTimestampAgreed();
    agreement.setUserUsageConditions(UserUsageConditions.getByCode(context, request.userUsageConditionsCode));
    context.commitChanges();
    LOGGER.info("did agree to user usage conditions [{}]", request.userUsageConditionsCode);
    return new AgreeUserUsageConditionsResult();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) UserUsageConditionsAgreement(org.haiku.haikudepotserver.dataobjects.UserUsageConditionsAgreement) ObjectContext(org.apache.cayenne.ObjectContext)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)2 User (org.haiku.haikudepotserver.dataobjects.User)2 UserUsageConditionsAgreement (org.haiku.haikudepotserver.dataobjects.UserUsageConditionsAgreement)2 UserUsageConditions (org.haiku.haikudepotserver.dataobjects.UserUsageConditions)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1