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();
}
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();
}
Aggregations