Skip to content

Commit

Permalink
Split out admin repository queries for notification and deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
KianKwa committed Oct 10, 2024
1 parent 2d0d344 commit 8097549
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ List<PiUser> findExistingByProvenanceId(@Param("provUserId") String provenanceUs
nativeQuery = true)
List<String> getAccManDataForMI();


@Query(value = "SELECT * FROM pi_user WHERE CAST(last_verified_date AS DATE) = CURRENT_DATE - (interval '1' day)"
+ " * :daysAgo AND user_provenance = 'PI_AAD' AND roles = 'VERIFIED'", nativeQuery = true)
List<PiUser> findVerifiedUsersByLastVerifiedDate(@Param("daysAgo") int daysSinceLastVerified);

@Query(value = "SELECT * FROM pi_user WHERE user_provenance = 'PI_AAD' AND roles <> 'VERIFIED' AND "
+ "CAST(last_signed_in_date AS DATE) = CURRENT_DATE - (interval '1' day) * :aadDays", nativeQuery = true)
List<PiUser> findAdminUsersFortNotificationByLastSignedInDate(@Param("aadDays") int aadNumberOfDays);

@Query(value = "SELECT * FROM pi_user WHERE (user_provenance = 'PI_AAD' AND roles <> 'VERIFIED' AND "
+ "CAST(last_signed_in_date AS DATE) <= CURRENT_DATE - (interval '1' day) * :aadDays) OR "
+ "(user_provenance = 'SSO' AND :ssoDays > 0 AND "
+ "(user_provenance = 'SSO' AND "
+ "CAST(last_signed_in_date AS DATE) <= CURRENT_DATE - (interval '1' day) * :ssoDays)", nativeQuery = true)
List<PiUser> findAdminUsersByLastSignedInDate(@Param("aadDays") int aadNumberOfDays,
@Param("ssoDays") int ssoNumberOfDays);
List<PiUser> findAdminUsersForDeletionByLastSignedInDate(@Param("aadDays") int aadNumberOfDays,
@Param("ssoDays") int ssoNumberOfDays);

@Query(value = "SELECT * FROM pi_user WHERE (CAST(last_signed_in_date AS DATE) = CURRENT_DATE - (interval '1' day)"
+ " * :cftDaysAgo AND user_provenance = 'CFT_IDAM') "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
@Slf4j
@Service
public class InactiveAccountManagementService {

private static final int SSO_ADMIN_ACCOUNT_SIGN_IN_NOTIFICATION_DAYS = 0;

private final UserRepository userRepository;
private final AzureUserService azureUserService;
private final PublicationService publicationService;
Expand Down Expand Up @@ -79,9 +76,7 @@ public void sendMediaUsersForVerification() {
* Then send their details on to publication services to send them a notification email.
*/
public void notifyAdminUsersToSignIn() {
userRepository.findAdminUsersByLastSignedInDate(aadAdminAccountSignInNotificationDays,
SSO_ADMIN_ACCOUNT_SIGN_IN_NOTIFICATION_DAYS
)
userRepository.findAdminUsersFortNotificationByLastSignedInDate(aadAdminAccountSignInNotificationDays)
.forEach(user -> {
try {
publicationService.sendInactiveAccountSignInNotificationEmail(
Expand Down Expand Up @@ -126,7 +121,8 @@ public void findMediaAccountsForDeletion() {
* Account service handles the deletion of their AAD, P&I user and subscriptions.
*/
public void findAdminAccountsForDeletion() {
userRepository.findAdminUsersByLastSignedInDate(aadAdminAccountDeletionDays, ssoAdminAccountDeletionDays)
userRepository.findAdminUsersForDeletionByLastSignedInDate(aadAdminAccountDeletionDays,
ssoAdminAccountDeletionDays)
.forEach(user -> accountService.deleteAccount(user.getUserId()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void testNoMediaUsersForVerification() {

@Test
void testNotifyAdminUsersToSignIn() throws AzureCustomException {
when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), eq(0)))
when(userRepository.findAdminUsersFortNotificationByLastSignedInDate(anyInt()))
.thenReturn(Collections.singletonList(AAD_ADMIN_USER));
when(azureUserService.getUser(AAD_ADMIN_USER_EMAIL)).thenReturn(azureAdminUser);

Expand All @@ -118,7 +118,7 @@ void testNotifyAdminUsersToSignIn() throws AzureCustomException {

@Test
void testNoNotificationOfAdminUsersToSignIn() {
when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), anyInt()))
when(userRepository.findAdminUsersFortNotificationByLastSignedInDate(anyInt()))
.thenReturn(Collections.emptyList());

inactiveAccountManagementService.notifyAdminUsersToSignIn();
Expand Down Expand Up @@ -169,7 +169,7 @@ void testNoMediaAccountDeletion() {

@Test
void testAdminAccountDeletion() {
when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), anyInt()))
when(userRepository.findAdminUsersForDeletionByLastSignedInDate(anyInt(), anyInt()))
.thenReturn(List.of(AAD_ADMIN_USER, SSO_ADMIN_USER));

inactiveAccountManagementService.findAdminAccountsForDeletion();
Expand All @@ -179,7 +179,7 @@ void testAdminAccountDeletion() {

@Test
void testNoAdminAccountDeletion() {
when(userRepository.findAdminUsersByLastSignedInDate(anyInt(), anyInt()))
when(userRepository.findAdminUsersForDeletionByLastSignedInDate(anyInt(), anyInt()))
.thenReturn(Collections.emptyList());

inactiveAccountManagementService.findAdminAccountsForDeletion();
Expand Down

0 comments on commit 8097549

Please sign in to comment.