Search in sources :

Example 1 with Error

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));
}
Also used : Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException)

Example 2 with Error

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);
}
Also used : NameRecord(com.faforever.api.data.domain.NameRecord) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Error

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);
}
Also used : User(com.faforever.api.data.domain.User) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException)

Example 4 with Error

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);
}
Also used : User(com.faforever.api.data.domain.User) ArrayList(java.util.ArrayList) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 5 with Error

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);
}
Also used : User(com.faforever.api.data.domain.User) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Aggregations

ApiException (com.faforever.api.error.ApiException)29 Error (com.faforever.api.error.Error)29 ProgrammingError (com.faforever.api.error.ProgrammingError)13 SneakyThrows (lombok.SneakyThrows)11 Path (java.nio.file.Path)6 ArrayList (java.util.ArrayList)6 Player (com.faforever.api.data.domain.Player)5 NotFoundApiException (com.faforever.api.error.NotFoundApiException)5 LuaValue (org.luaj.vm2.LuaValue)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Clan (com.faforever.api.data.domain.Clan)3 User (com.faforever.api.data.domain.User)3 InvitationResult (com.faforever.api.clan.result.InvitationResult)2 Achievement (com.faforever.api.data.domain.Achievement)2 Avatar (com.faforever.api.data.domain.Avatar)2 ClanMembership (com.faforever.api.data.domain.ClanMembership)2 Map (com.faforever.api.data.domain.Map)2 PlayerAchievement (com.faforever.api.data.domain.PlayerAchievement)2 Vote (com.faforever.api.data.domain.Vote)2 VotingSubject (com.faforever.api.data.domain.VotingSubject)2