From 92b8c34d03e8932fd9dea95c2faf46080274de13 Mon Sep 17 00:00:00 2001 From: Reingold Shekhtel <13565058+raikbitters@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:26:30 +0100 Subject: [PATCH] Add user active status checking * Add user status update for SAML * Add user status checking for GitHub flow --- .../UiAuthenticationSuccessEventHandler.java | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/epam/reportportal/auth/event/UiAuthenticationSuccessEventHandler.java b/src/main/java/com/epam/reportportal/auth/event/UiAuthenticationSuccessEventHandler.java index 7594440a..c72e3e2e 100644 --- a/src/main/java/com/epam/reportportal/auth/event/UiAuthenticationSuccessEventHandler.java +++ b/src/main/java/com/epam/reportportal/auth/event/UiAuthenticationSuccessEventHandler.java @@ -45,6 +45,10 @@ public class UiAuthenticationSuccessEventHandler { private PersonalProjectService personalProjectService; + /** + * Event handler for successful UI authentication events. Updates the last login date for the user + * and generates a personal project if the user has no projects. + */ @Autowired public UiAuthenticationSuccessEventHandler(UserRepository userRepository, PersonalProjectService personalProjectService) { @@ -52,14 +56,18 @@ public UiAuthenticationSuccessEventHandler(UserRepository userRepository, this.personalProjectService = personalProjectService; } + /** + * Handles the UI user signed-in event. Updates the last login date for the user + * and generates a personal project if the user has no projects. + * Also, if the user is inactive, it will be activated for SAML authentication. + * + * @param event the UI user signed-in event + */ @EventListener @Transactional public void onApplicationEvent(UiUserSignedInEvent event) { String username = event.getAuthentication().getName(); - if (!((ReportPortalUser) event.getAuthentication().getPrincipal()).isEnabled()) { - SecurityContextHolder.clearContext(); - throw new LockedException("User account is locked"); - } + userRepository.updateLastLoginDate(username); if (MapUtils.isEmpty(acquireUser(event.getAuthentication()).getProjectDetails())) { @@ -72,11 +80,22 @@ public void onApplicationEvent(UiUserSignedInEvent event) { 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())); + .orElseThrow(() -> new ReportPortalException( + ErrorType.USER_NOT_FOUND, rpAuth.getPrincipal() + )); } else { + if (!((ReportPortalUser) authentication.getPrincipal()).isEnabled()) { + SecurityContextHolder.clearContext(); + throw new LockedException("User account is locked"); + } return (ReportPortalUser) authentication.getPrincipal(); } } -} +} \ No newline at end of file