Skip to content

Commit

Permalink
[backend] Fix regression in reset password
Browse files Browse the repository at this point in the history
  • Loading branch information
savacano28 committed Jan 2, 2025
1 parent ea7f3b1 commit 508c1c7
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ public Document updateDocumentInformation(
!exercise.isUserHasAccess(
userRepository
.findById(currentUser().getId())
.orElseThrow(ElementNotFoundException::new)))
.orElseThrow(
() -> new ElementNotFoundException("Current user not found"))))
.map(Exercise::getId);
List<String> askExerciseIds =
Stream.concat(askExerciseIdsStream, input.getExerciseIds().stream()).distinct().toList();
Expand All @@ -370,7 +371,8 @@ public Document updateDocumentInformation(
!scenario.isUserHasAccess(
userRepository
.findById(currentUser().getId())
.orElseThrow(ElementNotFoundException::new)))
.orElseThrow(
() -> new ElementNotFoundException("Current user not found"))))
.map(Scenario::getId);
List<String> askScenarioIds =
Stream.concat(askScenarioIdsStream, input.getScenarioIds().stream()).distinct().toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public Log createLog(@PathVariable String exerciseId, @Valid @RequestBody LogCre
log.setExercise(exercise);
log.setTags(iterableToSet(tagRepository.findAllById(input.getTagIds())));
log.setUser(
userRepository.findById(currentUser().getId()).orElseThrow(ElementNotFoundException::new));
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found")));
return exerciseLogRepository.save(log);
}

Expand Down Expand Up @@ -172,7 +174,7 @@ public Dryrun createDryrun(
? List.of(
userRepository
.findById(currentUser().getId())
.orElseThrow(ElementNotFoundException::new))
.orElseThrow(() -> new ElementNotFoundException("Current user not found")))
: fromIterable(userRepository.findAllById(userIds));
return dryrunService.provisionDryrun(exercise, users, input.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public Evaluation createEvaluation(
Objective objective = resolveRelation(objectiveId, objectiveRepository);
evaluation.setObjective(objective);
evaluation.setUser(
userRepository.findById(currentUser().getId()).orElseThrow(ElementNotFoundException::new));
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found")));
Evaluation result = evaluationRepository.save(evaluation);
objective.setUpdatedAt(now());
objectiveRepository.save(objective);
Expand Down
4 changes: 3 additions & 1 deletion openbas-api/src/main/java/io/openbas/rest/team/TeamApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public Iterable<TeamSimple> getTeams() {
} else {
// We get the teams that are linked to the organizations we are part of
User local =
userRepository.findById(currentUser.getId()).orElseThrow(ElementNotFoundException::new);
userRepository
.findById(currentUser.getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found"));
List<String> organizationIds =
local.getGroups().stream()
.flatMap(group -> group.getOrganizations().stream())
Expand Down
18 changes: 13 additions & 5 deletions openbas-api/src/main/java/io/openbas/rest/user/MeApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ public ResponseEntity<Object> logout() {
public User me() {
return userRepository
.findById(currentUser().getId())
.orElseThrow(ElementNotFoundException::new);
.orElseThrow(() -> new ElementNotFoundException("Current user not found"));
}

@Secured(ROLE_USER)
@PutMapping("/api/me/profile")
public User updateProfile(@Valid @RequestBody UpdateProfileInput input) {
User user =
userRepository.findById(currentUser().getId()).orElseThrow(ElementNotFoundException::new);
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found"));
user.setUpdateAttributes(input);
user.setOrganization(
updateRelation(input.getOrganizationId(), user.getOrganization(), organizationRepository));
Expand All @@ -91,7 +93,9 @@ public User updateProfile(@Valid @RequestBody UpdateProfileInput input) {
@PutMapping("/api/me/information")
public User updateInformation(@Valid @RequestBody UpdateUserInfoInput input) {
User user =
userRepository.findById(currentUser().getId()).orElseThrow(ElementNotFoundException::new);
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found"));
user.setUpdateAttributes(input);
User savedUser = userRepository.save(user);
sessionManager.refreshUserSessions(savedUser);
Expand All @@ -103,7 +107,9 @@ public User updateInformation(@Valid @RequestBody UpdateUserInfoInput input) {
public User updatePassword(@Valid @RequestBody UpdateMePasswordInput input)
throws InputValidationException {
User user =
userRepository.findById(currentUser().getId()).orElseThrow(ElementNotFoundException::new);
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found"));
if (userService.isUserPasswordValid(user, input.getCurrentPassword())) {
user.setPassword(userService.encodeUserPassword(input.getPassword()));
return userRepository.save(user);
Expand All @@ -118,7 +124,9 @@ public User updatePassword(@Valid @RequestBody UpdateMePasswordInput input)
public Token renewToken(@Valid @RequestBody RenewTokenInput input)
throws InputValidationException {
User user =
userRepository.findById(currentUser().getId()).orElseThrow(ElementNotFoundException::new);
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found"));
Token token =
tokenRepository.findById(input.getTokenId()).orElseThrow(ElementNotFoundException::new);
if (!user.equals(token.getUser())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public Iterable<RawPlayer> players() {
players = fromIterable(userRepository.rawAllPlayers());
} else {
User local =
userRepository.findById(currentUser.getId()).orElseThrow(ElementNotFoundException::new);
userRepository
.findById(currentUser.getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found"));
List<String> organizationIds =
local.getGroups().stream()
.flatMap(group -> group.getOrganizations().stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public InjectResultOverviewOutput createOrUpdate(AtomicTestingInput input, Strin
injectToSave.setAllTeams(input.isAllTeams());
injectToSave.setDescription(input.getDescription());
injectToSave.setDependsDuration(0L);
injectToSave.setUser(userRepository.findById(currentUser().getId()).orElseThrow());
injectToSave.setUser(
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found")));
injectToSave.setExercise(null);

// Set dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,10 @@ private ImportRow importRow(
mapPatternByAllTeams));
});
// The user is the one doing the import
inject.setUser(userRepository.findById(currentUser().getId()).orElseThrow());
inject.setUser(
userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found")));
// No exercise yet
inject.setExercise(null);
// No dependencies
Expand Down
11 changes: 10 additions & 1 deletion openbas-api/src/main/java/io/openbas/service/MailingService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.openbas.service;

import static io.openbas.config.OpenBASAnonymous.ANONYMOUS;
import static io.openbas.config.SessionHelper.currentUser;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -74,7 +75,15 @@ public void sendEmail(
.ifPresent(
injectorContract -> {
inject.setContent(this.mapper.valueToTree(emailContent));
inject.setUser(this.userRepository.findById(currentUser().getId()).orElseThrow());

// When resetting the password, the user is not logged in (anonymous),
// so there's no need to add the user to the inject.
if (!ANONYMOUS.equals(currentUser().getId())) {
inject.setUser(
this.userRepository
.findById(currentUser().getId())
.orElseThrow(() -> new ElementNotFoundException("Current user not found")));
}

exercise.ifPresent(inject::setExercise);

Expand Down

0 comments on commit 508c1c7

Please sign in to comment.