From ece1824bd4f682733d3e3fe53b312eae883b167c Mon Sep 17 00:00:00 2001 From: paul-lawlor-tco Date: Wed, 17 Apr 2024 16:35:38 +0100 Subject: [PATCH 1/3] Clean up logic to make call to user service regardless of role --- .../gap/adminbackend/services/UserService.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java index 452c32f1..8e1d2bb5 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java @@ -2,11 +2,14 @@ import gov.cabinetoffice.gap.adminbackend.client.UserServiceClient; import gov.cabinetoffice.gap.adminbackend.config.UserServiceConfig; +import gov.cabinetoffice.gap.adminbackend.dtos.RoleDto; +import gov.cabinetoffice.gap.adminbackend.dtos.UserDTO; import gov.cabinetoffice.gap.adminbackend.dtos.UserV2DTO; import gov.cabinetoffice.gap.adminbackend.dtos.ValidateSessionsRolesRequestBodyDTO; import gov.cabinetoffice.gap.adminbackend.dtos.user.UserDto; import gov.cabinetoffice.gap.adminbackend.dtos.user.UserEmailResponseDto; import gov.cabinetoffice.gap.adminbackend.entities.FundingOrganisation; +import gov.cabinetoffice.gap.adminbackend.entities.GapUser; import gov.cabinetoffice.gap.adminbackend.entities.GrantAdmin; import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException; import gov.cabinetoffice.gap.adminbackend.exceptions.UnauthorizedException; @@ -34,6 +37,7 @@ import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.stream.Collectors; import static gov.cabinetoffice.gap.adminbackend.utils.HelperUtils.encryptSecret; @@ -117,10 +121,8 @@ public Boolean verifyAdminRoles(final String emailAddress, final String roles) { @PreAuthorize("hasAnyRole('ADMIN', 'SUPER_ADMIN')") public GrantAdmin getGrantAdminIdFromUserServiceEmail(final String email, final String jwt) { try { - String roles = HelperUtils.getAdminSessionForAuthenticatedUser().getRoles(); - String uriRole = roles.contains("SUPER_ADMIN") ? "SUPER_ADMIN" : "ADMIN"; UserV2DTO response = webClientBuilder.build().get() - .uri(userServiceConfig.getDomain() + "/user/email/" + email + "?role=" + uriRole) + .uri(userServiceConfig.getDomain() + "/user/email/" + email) .cookie(userServiceConfig.getCookieName(), jwt).retrieve().bodyToMono(UserV2DTO.class).block(); return grantAdminRepository.findByGapUserUserSub(response.sub()).orElseThrow(() -> new NotFoundException( From 198b1047cb042b033aa57430f20254453989835b Mon Sep 17 00:00:00 2001 From: paul-lawlor-tco Date: Wed, 17 Apr 2024 16:43:28 +0100 Subject: [PATCH 2/3] Make test more generic, removing uneccesary URI concatenation --- .../gap/adminbackend/services/UserServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/UserServiceTest.java b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/UserServiceTest.java index 0a7ef912..4c097965 100644 --- a/src/test/java/gov/cabinetoffice/gap/adminbackend/services/UserServiceTest.java +++ b/src/test/java/gov/cabinetoffice/gap/adminbackend/services/UserServiceTest.java @@ -218,7 +218,7 @@ void getGrantAdminIdFromEmailReturnsAValidGrantAdminIdForAdmin() { when(userServiceConfig.getDomain()).thenReturn("http://localhost:8080"); when(webClientBuilder.build()).thenReturn(mockWebClient); when(mockWebClient.get()).thenReturn(mockRequestHeaderUriSpec); - when(mockRequestHeaderUriSpec.uri("http://localhost:8080/user/email/" + email + "?role=ADMIN")) + when(mockRequestHeaderUriSpec.uri("http://localhost:8080/user/email/" + email)) .thenReturn(mockRequestHeadersSpec); when(mockRequestHeadersSpec.cookie(anyString(), anyString())).thenReturn(mockRequestHeadersSpec); when(mockRequestHeadersSpec.retrieve()).thenReturn(mockResponseSpec); @@ -254,7 +254,7 @@ void getGrantAdminIdFromEmailReturnsAValidGrantAdminIdForSuperAdmin() { when(userServiceConfig.getDomain()).thenReturn("http://localhost:8080"); when(webClientBuilder.build()).thenReturn(mockWebClient); when(mockWebClient.get()).thenReturn(mockRequestHeaderUriSpec); - when(mockRequestHeaderUriSpec.uri("http://localhost:8080/user/email/" + email + "?role=SUPER_ADMIN")) + when(mockRequestHeaderUriSpec.uri("http://localhost:8080/user/email/" + email)) .thenReturn(mockRequestHeadersSpec); when(mockRequestHeadersSpec.cookie(anyString(), anyString())).thenReturn(mockRequestHeadersSpec); when(mockRequestHeadersSpec.retrieve()).thenReturn(mockResponseSpec); From cbfa726f324ddcf6cb960013a7fa1e4fa2801049 Mon Sep 17 00:00:00 2001 From: paul-lawlor-tco Date: Wed, 17 Apr 2024 16:58:34 +0100 Subject: [PATCH 3/3] optimize imports --- .../cabinetoffice/gap/adminbackend/services/UserService.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java index 8e1d2bb5..e3bc182d 100644 --- a/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java +++ b/src/main/java/gov/cabinetoffice/gap/adminbackend/services/UserService.java @@ -2,14 +2,11 @@ import gov.cabinetoffice.gap.adminbackend.client.UserServiceClient; import gov.cabinetoffice.gap.adminbackend.config.UserServiceConfig; -import gov.cabinetoffice.gap.adminbackend.dtos.RoleDto; -import gov.cabinetoffice.gap.adminbackend.dtos.UserDTO; import gov.cabinetoffice.gap.adminbackend.dtos.UserV2DTO; import gov.cabinetoffice.gap.adminbackend.dtos.ValidateSessionsRolesRequestBodyDTO; import gov.cabinetoffice.gap.adminbackend.dtos.user.UserDto; import gov.cabinetoffice.gap.adminbackend.dtos.user.UserEmailResponseDto; import gov.cabinetoffice.gap.adminbackend.entities.FundingOrganisation; -import gov.cabinetoffice.gap.adminbackend.entities.GapUser; import gov.cabinetoffice.gap.adminbackend.entities.GrantAdmin; import gov.cabinetoffice.gap.adminbackend.exceptions.NotFoundException; import gov.cabinetoffice.gap.adminbackend.exceptions.UnauthorizedException; @@ -18,7 +15,6 @@ import gov.cabinetoffice.gap.adminbackend.repositories.GrantAdminRepository; import gov.cabinetoffice.gap.adminbackend.repositories.GrantApplicantRepository; import gov.cabinetoffice.gap.adminbackend.services.encryption.AwsEncryptionServiceImpl; -import gov.cabinetoffice.gap.adminbackend.utils.HelperUtils; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.springframework.core.ParameterizedTypeReference; @@ -37,7 +33,6 @@ import java.util.List; import java.util.Optional; import java.util.UUID; -import java.util.stream.Collectors; import static gov.cabinetoffice.gap.adminbackend.utils.HelperUtils.encryptSecret;