Skip to content

Commit

Permalink
Add user active status checking
Browse files Browse the repository at this point in the history
* Add user status update for SAML

* Add user status checking for GitHub flow
  • Loading branch information
raikbitters authored Dec 4, 2024
1 parent 94089aa commit 92b8c34
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,29 @@ 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) {
this.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())) {
Expand All @@ -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();
}
}
}
}

0 comments on commit 92b8c34

Please sign in to comment.