use of com.faforever.api.error.ApiException 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);
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class VotingService method ableToVote.
private List<Error> ableToVote(Player player, int votingSubjectId) {
VotingSubject subject = votingSubjectRepository.findById(votingSubjectId).orElseThrow(() -> new ApiException(new Error(ErrorCode.VOTING_SUBJECT_DOES_NOT_EXIST, votingSubjectId)));
List<Error> errors = new ArrayList<>();
Optional<Vote> byPlayerAndVotingSubject = voteRepository.findByPlayerAndVotingSubjectId(player, votingSubjectId);
if (byPlayerAndVotingSubject.isPresent()) {
errors.add(new Error(ErrorCode.VOTED_TWICE));
}
int gamesPlayed = gamePlayerStatsRepository.countByPlayerAndGameValidity(player, Validity.VALID);
if (subject.getBeginOfVoteTime().isAfter(OffsetDateTime.now())) {
errors.add(new Error(ErrorCode.VOTE_DID_NOT_START_YET, subject.getBeginOfVoteTime()));
}
if (subject.getEndOfVoteTime().isBefore(OffsetDateTime.now())) {
errors.add(new Error(ErrorCode.VOTE_ALREADY_ENDED, subject.getEndOfVoteTime()));
}
if (gamesPlayed < subject.getMinGamesToVote()) {
errors.add(new Error(ErrorCode.NOT_ENOUGH_GAMES, gamesPlayed, subject.getMinGamesToVote()));
}
return errors;
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class ModService method processUploadedMod.
@SneakyThrows
@Transactional
@CacheEvict(value = { Mod.TYPE_NAME, ModVersion.TYPE_NAME }, allEntries = true)
public void processUploadedMod(Path uploadedFile, Player uploader) {
log.debug("Player '{}' uploaded a mod", uploader);
ModReader modReader = new ModReader();
com.faforever.commons.mod.Mod modInfo = modReader.readZip(uploadedFile);
validateModInfo(modInfo);
validateModStructure(uploadedFile);
log.debug("Mod uploaded by user '{}' is valid: {}", uploader, modInfo);
String displayName = modInfo.getName().trim();
short version = (short) Integer.parseInt(modInfo.getVersion().toString());
if (!canUploadMod(displayName, uploader)) {
Mod mod = modRepository.findOneByDisplayName(displayName).orElseThrow(() -> new IllegalStateException("Mod could not be found"));
throw new ApiException(new Error(ErrorCode.MOD_NOT_ORIGINAL_AUTHOR, mod.getAuthor(), displayName));
}
if (modExists(displayName, version)) {
throw new ApiException(new Error(ErrorCode.MOD_VERSION_EXISTS, displayName, version));
}
String uuid = modInfo.getUid();
if (modUidExists(uuid)) {
throw new ApiException(new Error(ErrorCode.MOD_UID_EXISTS, uuid));
}
String zipFileName = generateZipFileName(displayName, version);
Path targetPath = properties.getMod().getTargetDirectory().resolve(zipFileName);
if (Files.exists(targetPath)) {
throw new ApiException(new Error(ErrorCode.MOD_NAME_CONFLICT, zipFileName));
}
Optional<Path> thumbnailPath = extractThumbnail(uploadedFile, version, displayName, modInfo.getIcon());
log.debug("Moving uploaded mod '{}' to: {}", modInfo.getName(), targetPath);
Files.createDirectories(targetPath.getParent(), FilePermissionUtil.directoryPermissionFileAttributes());
Files.move(uploadedFile, targetPath);
FilePermissionUtil.setDefaultFilePermission(targetPath);
try {
store(modInfo, thumbnailPath, uploader, zipFileName);
} catch (Exception exception) {
try {
Files.delete(targetPath);
} catch (IOException ioException) {
log.warn("Could not delete file " + targetPath, ioException);
}
throw exception;
}
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class MapService method updateMapEntities.
private void updateMapEntities(MapUploadData progressData) {
LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
Map map = progressData.getMapEntity();
if (map == null) {
map = new Map();
}
map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString()).setMapType(scenarioInfo.get(ScenarioMapInfo.TYPE).tojstring()).setBattleType(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_NAME).tojstring()).setAuthor(progressData.getAuthorEntity());
LuaValue size = scenarioInfo.get(ScenarioMapInfo.SIZE);
MapVersion version = new MapVersion().setDescription(scenarioInfo.get(ScenarioMapInfo.DESCRIPTION).tojstring().replaceAll("<LOC .*?>", "")).setWidth(size.get(1).toint()).setHeight(size.get(2).toint()).setHidden(false).setRanked(progressData.isRanked()).setMaxPlayers(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_ARMIES).length()).setVersion(scenarioInfo.get(ScenarioMapInfo.MAP_VERSION).toint());
map.getVersions().add(version);
version.setMap(map);
progressData.setMapEntity(map);
progressData.setMapVersionEntity(version);
version.setFilename(STUPID_MAP_FOLDER_PREFIX + progressData.getFinalZipName());
progressData.setFinalZipFile(this.fafApiProperties.getMap().getTargetDirectory().resolve(progressData.getFinalZipName()));
if (Files.exists(progressData.getFinalZipFile())) {
throw new ApiException(new Error(ErrorCode.MAP_NAME_CONFLICT, progressData.getFinalZipName()));
}
// this triggers validation
mapRepository.save(map);
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class MapService method postProcessLuaFile.
private void postProcessLuaFile(MapUploadData progressData) {
LuaValue scenarioInfo = progressData.getLuaScenarioInfo();
Optional<Map> mapEntity = mapRepository.findOneByDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString());
if (!mapEntity.isPresent()) {
return;
}
Player author = mapEntity.get().getAuthor();
if (author == null) {
return;
}
if (author.getId() != progressData.getAuthorEntity().getId()) {
throw new ApiException(new Error(ErrorCode.MAP_NOT_ORIGINAL_AUTHOR, mapEntity.get().getDisplayName()));
}
int newVersion = scenarioInfo.get(ScenarioMapInfo.MAP_VERSION).toint();
if (mapEntity.get().getVersions().stream().anyMatch(mapVersion -> mapVersion.getVersion() == newVersion)) {
throw new ApiException(new Error(ErrorCode.MAP_VERSION_EXISTS, mapEntity.get().getDisplayName(), newVersion));
}
progressData.setMapEntity(mapEntity.get());
}
Aggregations