Skip to content

Commit

Permalink
Refactor UiAuthenticationSuccessEventHandler to streamline user retri…
Browse files Browse the repository at this point in the history
…eval and project assignment logic
  • Loading branch information
raikbitters committed Dec 4, 2024
1 parent 6f962b2 commit b3479d0
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,28 @@ public void onApplicationEvent(UiUserSignedInEvent event) {

userRepository.updateLastLoginDate(username);

if (MapUtils.isEmpty(acquireUser(event.getAuthentication()).getProjectDetails())) {
User user = userRepository.findByLogin(username)
.orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, username));
User user = acquireUser(event.getAuthentication());

if (user.getProjects().isEmpty()) {
Project project = personalProjectService.generatePersonalProject(user);
user.getProjects().addAll(project.getUsers());
}
}

private ReportPortalUser acquireUser(Authentication authentication) {
if (authentication instanceof ReportPortalSamlAuthentication rpAuth) {
userRepository.findByLogin(rpAuth.getPrincipal())
.filter(user -> !user.getActive())
.ifPresent(user -> {
user.setActive(true);
userRepository.save(user);
});
return userRepository.findUserDetails(rpAuth.getPrincipal()).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, rpAuth.getPrincipal()));
} else {
return (ReportPortalUser) authentication.getPrincipal();
}
private User acquireUser (Authentication authentication){
if (authentication instanceof ReportPortalSamlAuthentication rpAuth) {
User user = userRepository.findByLogin(rpAuth.getPrincipal())
.orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, rpAuth.getPrincipal()));

if (!user.getActive()) {
user.setActive(true);
userRepository.save(user);
}

return user;
} else {
return userRepository.findByLogin(authentication.getName())
.orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, authentication.getName()));
}
}
}

0 comments on commit b3479d0

Please sign in to comment.