use of com.faforever.api.error.Error in project faf-java-api by FAForever.
the class UserService method buildSteamLinkUrl.
public String buildSteamLinkUrl(User user, String callbackUrl) {
log.debug("Building Steam link url for user id: {}", user.getId());
if (user.getSteamId() != null && !Objects.equals(user.getSteamId(), "")) {
log.debug("User with id '{}' already linked to steam", user.getId());
throw new ApiException(new Error(ErrorCode.STEAM_ID_UNCHANGEABLE));
}
String token = fafTokenService.createToken(FafTokenType.LINK_TO_STEAM, Duration.ofHours(1), ImmutableMap.of(KEY_USER_ID, String.valueOf(user.getId()), KEY_STEAM_LINK_CALLBACK_URL, callbackUrl));
return steamService.buildLoginUrl(String.format(properties.getLinkToSteam().getSteamRedirectUrlFormat(), token));
}
use of com.faforever.api.error.Error in project faf-java-api by FAForever.
the class UserService method changeLogin.
@Transactional
public void changeLogin(String newLogin, User user, String ipAddress) {
validateUsername(newLogin);
int minDaysBetweenChange = properties.getUser().getMinimumDaysBetweenUsernameChange();
nameRecordRepository.getDaysSinceLastNewRecord(user.getId(), minDaysBetweenChange).ifPresent(daysSinceLastRecord -> {
throw new ApiException(new Error(ErrorCode.USERNAME_CHANGE_TOO_EARLY, minDaysBetweenChange - daysSinceLastRecord.intValue()));
});
int usernameReservationTimeInMonths = properties.getUser().getUsernameReservationTimeInMonths();
nameRecordRepository.getLastUsernameOwnerWithinMonths(newLogin, usernameReservationTimeInMonths).ifPresent(reservedByUserId -> {
if (reservedByUserId != user.getId()) {
throw new ApiException(new Error(ErrorCode.USERNAME_RESERVED, newLogin, usernameReservationTimeInMonths));
}
});
log.debug("Changing username for user ''{}'' to ''{}''", user, newLogin);
NameRecord nameRecord = new NameRecord().setName(user.getLogin()).setPlayer(playerRepository.getOne(user.getId()));
nameRecordRepository.save(nameRecord);
user.setLogin(newLogin);
createOrUpdateMauticContact(userRepository.save(user), ipAddress);
}
use of com.faforever.api.error.Error in project faf-java-api by FAForever.
the class UserService method resetPassword.
void resetPassword(String identifier, String newPassword) {
log.debug("Password reset requested for user-identifier: {}", identifier);
User user = userRepository.findOneByLoginIgnoreCase(identifier).orElseGet(() -> userRepository.findOneByEmailIgnoreCase(identifier).orElseThrow(() -> new ApiException(new Error(ErrorCode.UNKNOWN_IDENTIFIER))));
String token = fafTokenService.createToken(FafTokenType.PASSWORD_RESET, Duration.ofSeconds(properties.getRegistration().getLinkExpirationSeconds()), ImmutableMap.of(KEY_USER_ID, String.valueOf(user.getId()), KEY_PASSWORD, newPassword));
String passwordResetUrl = String.format(properties.getPasswordReset().getPasswordResetUrlFormat(), token);
emailService.sendPasswordResetMail(user.getLogin(), user.getEmail(), passwordResetUrl);
}
use of com.faforever.api.error.Error in project faf-java-api by FAForever.
the class UserService method linkToSteam.
@SneakyThrows
public SteamLinkResult linkToSteam(String token, String steamId) {
log.debug("linkToSteam requested for steamId '{}' with token: {}", steamId, token);
List<Error> errors = new ArrayList<>();
Map<String, String> attributes = fafTokenService.resolveToken(FafTokenType.LINK_TO_STEAM, token);
User user = userRepository.findById(Integer.parseInt(attributes.get(KEY_USER_ID))).orElseThrow(() -> new ApiException(new Error(TOKEN_INVALID)));
if (!steamService.ownsForgedAlliance(steamId)) {
errors.add(new Error(ErrorCode.STEAM_LINK_NO_FA_GAME));
}
Optional<User> userWithSameSteamIdOptional = userRepository.findOneBySteamIdIgnoreCase(steamId);
userWithSameSteamIdOptional.ifPresent(userWithSameId -> errors.add(new Error(ErrorCode.STEAM_ID_ALREADY_LINKED, userWithSameId.getLogin())));
if (errors.isEmpty()) {
user.setSteamId(steamId);
userRepository.save(user);
}
String callbackUrl = attributes.get(KEY_STEAM_LINK_CALLBACK_URL);
return new SteamLinkResult(callbackUrl, errors);
}
use of com.faforever.api.error.Error in project faf-java-api by FAForever.
the class UserService method claimPasswordResetToken.
@SneakyThrows
void claimPasswordResetToken(String token) {
log.debug("Trying to reset password with token: {}", token);
Map<String, String> claims = fafTokenService.resolveToken(FafTokenType.PASSWORD_RESET, token);
int userId = Integer.parseInt(claims.get(KEY_USER_ID));
String newPassword = claims.get(KEY_PASSWORD);
User user = userRepository.findById(userId).orElseThrow(() -> new ApiException(new Error(TOKEN_INVALID)));
setPassword(user, newPassword);
}
Aggregations