From dd242b0cc75cb9deb82bba8aa48711a9c4b2fc5e Mon Sep 17 00:00:00 2001 From: Russell Dodd Date: Mon, 26 Feb 2024 09:45:14 +0000 Subject: [PATCH] Populate 'postedByAad' in Add/SaveNote --- .../AuthenticationInternalUserController.java | 9 +- .../service/AccessTokenService.java | 42 ++++++++-- .../opal/authorisation/model/Permission.java | 2 +- .../opal/authorisation/model/UserState.java | 8 ++ .../service/AuthorisationService.java | 12 +-- .../AccountTransferController.java | 2 +- .../ApplicationFunctionController.java | 2 +- .../controllers/BusinessUnitController.java | 2 +- .../BusinessUnitUserController.java | 2 +- .../CommittalWarrantProgressController.java | 2 +- .../opal/controllers/CourtController.java | 2 +- .../controllers/DebtorDetailController.java | 2 +- .../DefendantAccountController.java | 50 +++++------ .../DefendantTransactionController.java | 2 +- .../opal/controllers/DocumentController.java | 2 +- .../DocumentInstanceController.java | 2 +- .../controllers/EnforcementController.java | 2 +- .../opal/controllers/EnforcerController.java | 2 +- .../LocalJusticeAreaController.java | 2 +- .../opal/controllers/MisDebtorController.java | 2 +- .../opal/controllers/NoteController.java | 25 +++++- .../opal/controllers/PartyController.java | 2 +- .../opal/controllers/PaymentInController.java | 2 +- .../controllers/PaymentTermsController.java | 2 +- .../opal/controllers/PrisonController.java | 2 +- .../opal/controllers/TemplateController.java | 2 +- .../TemplateMappingController.java | 2 +- .../controllers/TestingSupportController.java | 2 +- .../opal/controllers/TillController.java | 2 +- .../opal/controllers/UserController.java | 2 +- .../UserEntitlementController.java | 2 +- .../java/uk/gov/hmcts/opal/dto/NoteDto.java | 4 +- .../opal/entity/BusinessUnitUserEntity.java | 2 + .../opal/entity/UserEntitlementEntity.java | 5 +- .../uk/gov/hmcts/opal/entity/UserEntity.java | 1 + .../BusinessUnitUserRepository.java | 4 + .../repository/UserEntitlementRepository.java | 5 ++ .../hmcts/opal/repository/UserRepository.java | 2 + .../hmcts/opal/repository/jpa/UserSpecs.java | 15 ++++ .../service/opal/BusinessUnitUserService.java | 16 ++++ .../hmcts/opal/service/opal/NoteService.java | 2 + .../service/opal/UserEntitlementService.java | 10 +++ .../hmcts/opal/service/opal/UserService.java | 12 +++ .../java/uk/gov/hmcts/opal/util/HttpUtil.java | 44 ++++++++++ .../uk/gov/hmcts/opal/util/ResponseUtil.java | 24 ------ ...henticationInternalUserControllerTest.java | 11 ++- .../service/AccessTokenServiceTest.java | 83 +++++++++++++++---- .../service/AuthorisationServiceTest.java | 17 ++-- .../DefendantAccountControllerTest.java | 18 +++- .../opal/controllers/NoteControllerTest.java | 18 +++- .../TestingSupportControllerTest.java | 2 +- .../opal/BusinessUnitUserServiceTest.java | 25 ++++++ .../opal/UserEntitlementServiceTest.java | 21 +++++ .../opal/service/opal/UserServiceTest.java | 20 +++++ .../gov/hmcts/opal/util/ResponseUtilTest.java | 10 +-- 55 files changed, 438 insertions(+), 129 deletions(-) create mode 100644 src/main/java/uk/gov/hmcts/opal/util/HttpUtil.java delete mode 100644 src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java diff --git a/src/main/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserController.java b/src/main/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserController.java index b560ccad6..672c55c06 100644 --- a/src/main/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserController.java +++ b/src/main/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserController.java @@ -51,11 +51,12 @@ public SecurityToken handleOauthCode(@RequestParam("code") String code) { String accessToken = authenticationService.handleOauthCode(code); var securityTokenBuilder = SecurityToken.builder() .accessToken(accessToken); - Optional emailAddressOptional = Optional.ofNullable(accessTokenService.extractUserEmail(accessToken)); + Optional preferredUsernameOptional = Optional.ofNullable( + accessTokenService.extractPreferredUsername(accessToken)); - if (emailAddressOptional.isPresent()) { - Optional userStateOptional = authorisationService.getAuthorisation(emailAddressOptional.get()); - securityTokenBuilder.userState(userStateOptional.orElse(null)); + if (preferredUsernameOptional.isPresent()) { + UserState userStateOptional = authorisationService.getAuthorisation(preferredUsernameOptional.get()); + securityTokenBuilder.userState(userStateOptional); } return securityTokenBuilder.build(); } diff --git a/src/main/java/uk/gov/hmcts/opal/authentication/service/AccessTokenService.java b/src/main/java/uk/gov/hmcts/opal/authentication/service/AccessTokenService.java index 183b0a7dc..6b65fcb97 100644 --- a/src/main/java/uk/gov/hmcts/opal/authentication/service/AccessTokenService.java +++ b/src/main/java/uk/gov/hmcts/opal/authentication/service/AccessTokenService.java @@ -1,6 +1,7 @@ package uk.gov.hmcts.opal.authentication.service; import com.nimbusds.jwt.JWT; +import com.nimbusds.jwt.JWTClaimsSet; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -19,6 +20,14 @@ @RequiredArgsConstructor public class AccessTokenService { + public static final String AUTH_HEADER = "authorization"; + public static final String PREFERRED_USERNAME_KEY = "preferred_username"; + public static final String NAME_KEY = "name"; + public static final String SCP_KEY = "scp"; + public static final String UNIQUE_NAME_KEY = "unique_name"; + public static final String UPN_NAME_KEY = "upn"; + public static final String BEARER_PREFIX = "Bearer "; + private final InternalAuthConfigurationProperties configuration; private final AzureTokenClient azureTokenClient; private final TokenValidator tokenValidator; @@ -48,13 +57,37 @@ public AccessTokenResponse getAccessToken(String userName, String password) { ); } - public String extractUserEmail(String authorizationHeader) { + public String extractPreferredUsername(String accessToken) { + return extractClaim(accessToken, PREFERRED_USERNAME_KEY); + } + + public String extractName(String accessToken) { + return extractClaim(accessToken, NAME_KEY); + } + + public String extractScp(String accessToken) { + return extractClaim(accessToken, SCP_KEY); + } + + public String extractUniqueName(String accessToken) { + return extractClaim(accessToken, UNIQUE_NAME_KEY); + } + + public String extractUpn(String accessToken) { + return extractClaim(accessToken, UPN_NAME_KEY); + } + + public String extractClaim(String accessToken, String claimKey) { + return extractClaims(accessToken).getClaim(claimKey).toString(); + } + + public JWTClaimsSet extractClaims(String accessToken) { try { - String token = extractToken(authorizationHeader); + String token = extractToken(accessToken); JWT parsedJwt = tokenValidator.parse(token); - return parsedJwt.getJWTClaimsSet().getClaim("preferred_username").toString(); + return parsedJwt.getJWTClaimsSet(); } catch (ParseException e) { - log.error("Unable to parse token: " + e.getMessage()); + log.error(":extractClaim: Unable to extract claims from JWT Token: {}", e.getMessage()); throw new OpalApiException(AuthenticationError.FAILED_TO_PARSE_ACCESS_TOKEN, e); } } @@ -66,4 +99,3 @@ public String extractToken(String accessToken) { return accessToken; } } - diff --git a/src/main/java/uk/gov/hmcts/opal/authorisation/model/Permission.java b/src/main/java/uk/gov/hmcts/opal/authorisation/model/Permission.java index 22bf1ca49..81efcf4ab 100644 --- a/src/main/java/uk/gov/hmcts/opal/authorisation/model/Permission.java +++ b/src/main/java/uk/gov/hmcts/opal/authorisation/model/Permission.java @@ -9,7 +9,7 @@ public class Permission { @NonNull - Integer permissionId; + Long permissionId; @NonNull String permissionName; diff --git a/src/main/java/uk/gov/hmcts/opal/authorisation/model/UserState.java b/src/main/java/uk/gov/hmcts/opal/authorisation/model/UserState.java index 453fa3a67..c7f9c4169 100644 --- a/src/main/java/uk/gov/hmcts/opal/authorisation/model/UserState.java +++ b/src/main/java/uk/gov/hmcts/opal/authorisation/model/UserState.java @@ -5,6 +5,7 @@ import lombok.NonNull; import lombok.Value; +import java.util.Optional; import java.util.Set; @Value @@ -20,4 +21,11 @@ public class UserState { @EqualsAndHashCode.Exclude Set roles; + public Optional getFirstRole() { + return roles.stream().findFirst(); + } + + public Optional getFirstRoleBusinessUserId() { + return getFirstRole().map(Role::getBusinessUserId); + } } diff --git a/src/main/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationService.java b/src/main/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationService.java index fbddf5c22..a629b0b38 100644 --- a/src/main/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationService.java +++ b/src/main/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationService.java @@ -4,20 +4,16 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import uk.gov.hmcts.opal.authorisation.model.UserState; - -import java.util.Optional; +import uk.gov.hmcts.opal.service.opal.UserService; @Slf4j @Service @RequiredArgsConstructor public class AuthorisationService { + private final UserService userService; - public Optional getAuthorisation(String emailAddress) { - //TODO: populate user state from the database. - return Optional.of(UserState.builder() - .userId(emailAddress) - .userName("some name") - .build()); + public UserState getAuthorisation(String username) { + return userService.getUserStateByUsername(username); } } diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/AccountTransferController.java b/src/main/java/uk/gov/hmcts/opal/controllers/AccountTransferController.java index c02ecea76..abcaa4c5f 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/AccountTransferController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/AccountTransferController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/ApplicationFunctionController.java b/src/main/java/uk/gov/hmcts/opal/controllers/ApplicationFunctionController.java index 13a25b952..869d08245 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/ApplicationFunctionController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/ApplicationFunctionController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitController.java b/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitController.java index 96c85b813..750528e8b 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitUserController.java b/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitUserController.java index 3ad53114c..c5b2c6e64 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitUserController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitUserController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/CommittalWarrantProgressController.java b/src/main/java/uk/gov/hmcts/opal/controllers/CommittalWarrantProgressController.java index e674b02c0..143c8421b 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/CommittalWarrantProgressController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/CommittalWarrantProgressController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/CourtController.java b/src/main/java/uk/gov/hmcts/opal/controllers/CourtController.java index ce6b0eaab..c6e9e413e 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/CourtController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/CourtController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/DebtorDetailController.java b/src/main/java/uk/gov/hmcts/opal/controllers/DebtorDetailController.java index 509de0901..20ea552d2 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/DebtorDetailController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/DebtorDetailController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java b/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java index 46a7dbbbe..83f8447b4 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/DefendantAccountController.java @@ -5,7 +5,6 @@ import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -16,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import uk.gov.hmcts.opal.authentication.service.AccessTokenService; +import uk.gov.hmcts.opal.authorisation.model.UserState; import uk.gov.hmcts.opal.dto.AccountDetailsDto; import uk.gov.hmcts.opal.dto.AccountEnquiryDto; import uk.gov.hmcts.opal.dto.search.AccountSearchDto; @@ -26,11 +27,14 @@ import uk.gov.hmcts.opal.entity.DefendantAccountEntity; import uk.gov.hmcts.opal.service.DefendantAccountServiceInterface; import uk.gov.hmcts.opal.service.NoteServiceInterface; +import uk.gov.hmcts.opal.service.opal.UserService; import java.time.LocalDateTime; import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildCreatedResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.extractPreferredUsername; @RestController @RequestMapping("/api/defendant-account") @@ -44,12 +48,19 @@ public class DefendantAccountController { private final NoteServiceInterface noteService; + private final AccessTokenService tokenService; + + private final UserService userService; + public DefendantAccountController( @Qualifier("defendantAccountServiceProxy") DefendantAccountServiceInterface defendantAccountService, - @Qualifier("noteServiceProxy") NoteServiceInterface noteService) { + @Qualifier("noteServiceProxy") NoteServiceInterface noteService, AccessTokenService tokenService, + UserService userService) { this.defendantAccountService = defendantAccountService; this.noteService = noteService; + this.tokenService = tokenService; + this.userService = userService; } @GetMapping @@ -65,11 +76,7 @@ public ResponseEntity getDefendantAccount( DefendantAccountEntity response = defendantAccountService.getDefendantAccount(request); - if (response == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(response); + return buildResponse(response); } @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @@ -79,7 +86,7 @@ public ResponseEntity putDefendantAccount( DefendantAccountEntity response = defendantAccountService.putDefendantAccount(defendantAccountEntity); - return ResponseEntity.ok(response); + return buildResponse(response); } @GetMapping(value = "/{defendantAccountId}") @@ -88,11 +95,7 @@ public ResponseEntity getAccountDetailsByAccountSummary(@Path AccountDetailsDto response = defendantAccountService.getAccountDetailsByDefendantAccountId(defendantAccountId); - if (response == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(response); + return buildResponse(response); } @PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE) @@ -103,11 +106,7 @@ public ResponseEntity postDefendantAccountSearch( AccountSearchResultsDto response = defendantAccountService.searchDefendantAccounts(accountSearchDto); - if (response == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(response); + return buildResponse(response); } @PostMapping(value = "/addNote", consumes = MediaType.APPLICATION_JSON_VALUE) @@ -116,22 +115,25 @@ public ResponseEntity addNote( @RequestBody AddNoteDto addNote, HttpServletRequest request) { log.info(":POST:addNote: {}", addNote.toPrettyJson()); + String preferredUsername = extractPreferredUsername(request, tokenService); + UserState userState = userService.getUserStateByUsername(preferredUsername); + NoteDto noteDto = NoteDto.builder() .associatedRecordId(addNote.getAssociatedRecordId()) .noteText(addNote.getNoteText()) .associatedRecordType(NOTE_ASSOC_REC_TYPE) .noteType("AA") // TODO - This will probably need to part of the AddNoteDto in future - .postedBy(request.getRemoteUser()) + .postedBy(userState.getFirstRoleBusinessUserId().orElse(preferredUsername)) // TODO - not always 'first'? + .postedByAAD(userState.getUserId()) .postedDate(LocalDateTime.now()) .build(); NoteDto response = noteService.saveNote(noteDto); - if (response == null) { - return ResponseEntity.noContent().build(); - } + log.info(":POST:addNote: response: {}", response); + + return buildCreatedResponse(response); - return new ResponseEntity<>(response, HttpStatus.CREATED); } @GetMapping(value = "/notes/{defendantId}") diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/DefendantTransactionController.java b/src/main/java/uk/gov/hmcts/opal/controllers/DefendantTransactionController.java index 66e51e1bf..5447ccf65 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/DefendantTransactionController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/DefendantTransactionController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/DocumentController.java b/src/main/java/uk/gov/hmcts/opal/controllers/DocumentController.java index 6a93022bf..ee1c703d8 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/DocumentController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/DocumentController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/DocumentInstanceController.java b/src/main/java/uk/gov/hmcts/opal/controllers/DocumentInstanceController.java index 22ff0aa5e..1ace138c1 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/DocumentInstanceController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/DocumentInstanceController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/EnforcementController.java b/src/main/java/uk/gov/hmcts/opal/controllers/EnforcementController.java index bbcdfcd1a..ed6a342f6 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/EnforcementController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/EnforcementController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/EnforcerController.java b/src/main/java/uk/gov/hmcts/opal/controllers/EnforcerController.java index dcc1c6521..09664b2a9 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/EnforcerController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/EnforcerController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/LocalJusticeAreaController.java b/src/main/java/uk/gov/hmcts/opal/controllers/LocalJusticeAreaController.java index 286d3641a..9c5c92041 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/LocalJusticeAreaController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/LocalJusticeAreaController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/MisDebtorController.java b/src/main/java/uk/gov/hmcts/opal/controllers/MisDebtorController.java index fd3cb2e20..da2760574 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/MisDebtorController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/MisDebtorController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java b/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java index 872c9123b..86e57b63b 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/NoteController.java @@ -2,6 +2,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; @@ -13,13 +14,17 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import uk.gov.hmcts.opal.authentication.service.AccessTokenService; +import uk.gov.hmcts.opal.authorisation.model.UserState; import uk.gov.hmcts.opal.dto.NoteDto; import uk.gov.hmcts.opal.dto.search.NoteSearchDto; import uk.gov.hmcts.opal.service.NoteServiceInterface; +import uk.gov.hmcts.opal.service.opal.UserService; import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.extractPreferredUsername; @RestController @@ -30,13 +35,27 @@ public class NoteController { private final NoteServiceInterface noteService; - public NoteController(@Qualifier("noteServiceProxy") NoteServiceInterface noteService) { + private final AccessTokenService tokenService; + + private final UserService userService; + + public NoteController(@Qualifier("noteServiceProxy") NoteServiceInterface noteService, + AccessTokenService tokenService, UserService userService) { this.noteService = noteService; + this.tokenService = tokenService; + this.userService = userService; } @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) @Operation(summary = "Creates a new note in the Opal Fines Notes table assigning an ID.") - public ResponseEntity createNote(@RequestBody NoteDto noteDto) { + public ResponseEntity createNote(@RequestBody NoteDto noteDto, HttpServletRequest request) { + log.info(":POST:createNote: {}", noteDto.toPrettyJson()); + + String preferredUsername = extractPreferredUsername(request, tokenService); + UserState userState = userService.getUserStateByUsername(preferredUsername); + + noteDto.setPostedBy(userState.getFirstRoleBusinessUserId().orElse(preferredUsername)); // TODO - 'first'? + noteDto.setPostedByAAD(userState.getUserId()); NoteDto savedNoteDto = noteService.saveNote(noteDto); return new ResponseEntity<>(savedNoteDto, HttpStatus.CREATED); } diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/PartyController.java b/src/main/java/uk/gov/hmcts/opal/controllers/PartyController.java index e57b5c7a4..7c6c0d6ae 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/PartyController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/PartyController.java @@ -20,7 +20,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/PaymentInController.java b/src/main/java/uk/gov/hmcts/opal/controllers/PaymentInController.java index 1ca322617..561783ff6 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/PaymentInController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/PaymentInController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/PaymentTermsController.java b/src/main/java/uk/gov/hmcts/opal/controllers/PaymentTermsController.java index 96230b878..d2eac3a3e 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/PaymentTermsController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/PaymentTermsController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/PrisonController.java b/src/main/java/uk/gov/hmcts/opal/controllers/PrisonController.java index 25057226f..784baa64b 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/PrisonController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/PrisonController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/TemplateController.java b/src/main/java/uk/gov/hmcts/opal/controllers/TemplateController.java index 76a972bbe..2664d3b3a 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/TemplateController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/TemplateController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/TemplateMappingController.java b/src/main/java/uk/gov/hmcts/opal/controllers/TemplateMappingController.java index d56ce4c24..3e9153336 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/TemplateMappingController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/TemplateMappingController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/TestingSupportController.java b/src/main/java/uk/gov/hmcts/opal/controllers/TestingSupportController.java index 605bd64db..e8ff441b6 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/TestingSupportController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/TestingSupportController.java @@ -67,6 +67,6 @@ public ResponseEntity getTokenForUser(@RequestHeader(value @GetMapping("/token/parse") public ResponseEntity parseToken(@RequestHeader("Authorization") String authorization) { - return ResponseEntity.ok(this.accessTokenService.extractUserEmail(authorization)); + return ResponseEntity.ok(this.accessTokenService.extractPreferredUsername(authorization)); } } diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/TillController.java b/src/main/java/uk/gov/hmcts/opal/controllers/TillController.java index 98a8b96ff..dc89c9fb2 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/TillController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/TillController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/UserController.java b/src/main/java/uk/gov/hmcts/opal/controllers/UserController.java index 0be139441..8a53126c7 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/UserController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/UserController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/UserEntitlementController.java b/src/main/java/uk/gov/hmcts/opal/controllers/UserEntitlementController.java index d8cbab3e4..cc2c1044e 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/UserEntitlementController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/UserEntitlementController.java @@ -18,7 +18,7 @@ import java.util.List; -import static uk.gov.hmcts.opal.util.ResponseUtil.buildResponse; +import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @RestController diff --git a/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java b/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java index 383748756..a7be74836 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java @@ -15,7 +15,7 @@ @NoArgsConstructor @AllArgsConstructor @Jacksonized -public class NoteDto { +public class NoteDto implements ToJsonString { private Long noteId; @@ -32,4 +32,6 @@ public class NoteDto { private String postedBy; + private String postedByAAD; + } diff --git a/src/main/java/uk/gov/hmcts/opal/entity/BusinessUnitUserEntity.java b/src/main/java/uk/gov/hmcts/opal/entity/BusinessUnitUserEntity.java index 4798de575..2a0e286e6 100644 --- a/src/main/java/uk/gov/hmcts/opal/entity/BusinessUnitUserEntity.java +++ b/src/main/java/uk/gov/hmcts/opal/entity/BusinessUnitUserEntity.java @@ -11,11 +11,13 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; + @Entity @Table(name = "business_unit_users") @Data diff --git a/src/main/java/uk/gov/hmcts/opal/entity/UserEntitlementEntity.java b/src/main/java/uk/gov/hmcts/opal/entity/UserEntitlementEntity.java index 44e6120b6..df90452cd 100644 --- a/src/main/java/uk/gov/hmcts/opal/entity/UserEntitlementEntity.java +++ b/src/main/java/uk/gov/hmcts/opal/entity/UserEntitlementEntity.java @@ -1,6 +1,8 @@ package uk.gov.hmcts.opal.entity; +import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -24,6 +26,7 @@ @AllArgsConstructor @Builder @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userEntitlementId") public class UserEntitlementEntity { @Id @@ -37,7 +40,7 @@ public class UserEntitlementEntity { @JoinColumn(name = "business_unit_user_id", nullable = false) private BusinessUnitUserEntity businessUnitUser; - @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "application_function_id", nullable = false) private ApplicationFunctionEntity applicationFunction; } diff --git a/src/main/java/uk/gov/hmcts/opal/entity/UserEntity.java b/src/main/java/uk/gov/hmcts/opal/entity/UserEntity.java index 107cc97f8..1f98a8635 100644 --- a/src/main/java/uk/gov/hmcts/opal/entity/UserEntity.java +++ b/src/main/java/uk/gov/hmcts/opal/entity/UserEntity.java @@ -12,6 +12,7 @@ import lombok.Data; import lombok.NoArgsConstructor; + @Entity @Table(name = "users") @Data diff --git a/src/main/java/uk/gov/hmcts/opal/repository/BusinessUnitUserRepository.java b/src/main/java/uk/gov/hmcts/opal/repository/BusinessUnitUserRepository.java index c0b8b2611..a84e6955a 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/BusinessUnitUserRepository.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/BusinessUnitUserRepository.java @@ -5,7 +5,11 @@ import org.springframework.stereotype.Repository; import uk.gov.hmcts.opal.entity.BusinessUnitUserEntity; +import java.util.List; + @Repository public interface BusinessUnitUserRepository extends JpaRepository, JpaSpecificationExecutor { + + List findAllByUser_UserId(String userId); } diff --git a/src/main/java/uk/gov/hmcts/opal/repository/UserEntitlementRepository.java b/src/main/java/uk/gov/hmcts/opal/repository/UserEntitlementRepository.java index b0c2df29a..080e628f0 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/UserEntitlementRepository.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/UserEntitlementRepository.java @@ -5,7 +5,12 @@ import org.springframework.stereotype.Repository; import uk.gov.hmcts.opal.entity.UserEntitlementEntity; +import java.util.List; + @Repository public interface UserEntitlementRepository extends JpaRepository, JpaSpecificationExecutor { + + List findAllByBusinessUnitUser_BusinessUnitUserId(String businessUnitUserId); + } diff --git a/src/main/java/uk/gov/hmcts/opal/repository/UserRepository.java b/src/main/java/uk/gov/hmcts/opal/repository/UserRepository.java index b7d3665bd..3a878548e 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/UserRepository.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/UserRepository.java @@ -8,4 +8,6 @@ @Repository public interface UserRepository extends JpaRepository, JpaSpecificationExecutor { + + UserEntity findByUsername(String username); } diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/UserSpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/UserSpecs.java index 9d9c32f94..10dd52020 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/jpa/UserSpecs.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/jpa/UserSpecs.java @@ -18,6 +18,17 @@ public Specification findBySearchCriteria(UserSearchDto criteria) { )); } + public Specification findByUserIdOrName(String userIdOrName) { + return Specification.allOf(specificationList( + notBlank(userIdOrName).map(UserSpecs::equalsUserId), + notBlank(userIdOrName).map(UserSpecs::equalsUsername) + )); + } + + public static Specification equalsUserId(String userId) { + return (root, query, builder) -> builder.equal(root.get(UserEntity_.userId), userId); + } + public static Specification likeUserId(String userId) { return (root, query, builder) -> userIdPredicate(root, builder, userId); } @@ -26,6 +37,10 @@ public static Predicate userIdPredicate(From from, CriteriaBuilde return likeWildcardPredicate(from.get(UserEntity_.userId), builder, userId); } + public static Specification equalsUsername(String username) { + return (root, query, builder) -> builder.equal(root.get(UserEntity_.username), username); + } + public static Specification likeUsername(String username) { return (root, query, builder) -> usernamePredicate(root, builder, username); } diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserService.java index 9c06b1cba..89e412a2b 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserService.java @@ -5,6 +5,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import uk.gov.hmcts.opal.authorisation.model.Role; import uk.gov.hmcts.opal.dto.search.BusinessUnitUserSearchDto; import uk.gov.hmcts.opal.entity.BusinessUnitUserEntity; import uk.gov.hmcts.opal.repository.BusinessUnitUserRepository; @@ -12,6 +13,8 @@ import uk.gov.hmcts.opal.service.BusinessUnitUserServiceInterface; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -19,6 +22,8 @@ public class BusinessUnitUserService implements BusinessUnitUserServiceInterface private final BusinessUnitUserRepository businessUnitUserRepository; + private final UserEntitlementService userEntitlementService; + private final BusinessUnitUserSpecs specs = new BusinessUnitUserSpecs(); @Override @@ -35,4 +40,15 @@ public List searchBusinessUnitUsers(BusinessUnitUserSear return page.getContent(); } + public Set getAuthorisationRolesByUserId(String userId) { + List buuList = businessUnitUserRepository.findAllByUser_UserId(userId); + + return buuList.stream().map(buu -> Role.builder() + .businessUserId(buu.getBusinessUnitUserId()) + .businessUnit(buu.getBusinessUnit().getBusinessUnitId().toString()) + .permissions(userEntitlementService.getPermissionsByBusinessUnitUserId(buu.getBusinessUnitUserId())) + .build()).collect(Collectors.toSet()); + + } + } diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/NoteService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/NoteService.java index 9ceb97efc..55f524ef9 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/opal/NoteService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/opal/NoteService.java @@ -64,6 +64,7 @@ public NoteEntity toNoteEntity(NoteDto noteDto) { .noteText(noteDto.getNoteText()) .postedDate(noteDto.getPostedDate() == null ? LocalDateTime.now() : noteDto.getPostedDate()) .postedBy(noteDto.getPostedBy()) + .postedByAad(noteDto.getPostedByAAD()) .build(); } @@ -76,6 +77,7 @@ public NoteDto toNoteDto(NoteEntity entity) { .noteText(entity.getNoteText()) .postedDate(entity.getPostedDate()) .postedBy(entity.getPostedBy()) + .postedByAAD(entity.getPostedByAad()) .build(); } } diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/UserEntitlementService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/UserEntitlementService.java index fd98c8cd1..6aad88bcf 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/opal/UserEntitlementService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/opal/UserEntitlementService.java @@ -5,6 +5,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import uk.gov.hmcts.opal.authorisation.model.Permission; import uk.gov.hmcts.opal.dto.search.UserEntitlementSearchDto; import uk.gov.hmcts.opal.entity.UserEntitlementEntity; import uk.gov.hmcts.opal.repository.UserEntitlementRepository; @@ -12,6 +13,8 @@ import uk.gov.hmcts.opal.service.UserEntitlementServiceInterface; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -35,4 +38,11 @@ public List searchUserEntitlements(UserEntitlementSearchD return page.getContent(); } + public Set getPermissionsByBusinessUnitUserId(String businessUnitUserId) { + return userEntitlementRepository.findAllByBusinessUnitUser_BusinessUnitUserId(businessUnitUserId) + .stream().map(uee -> Permission.builder() + .permissionId(uee.getApplicationFunction().getApplicationFunctionId()) + .permissionName(uee.getApplicationFunction().getFunctionName()) + .build()).collect(Collectors.toSet()); + } } diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/UserService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/UserService.java index b44b55db5..0a4ec92cc 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/opal/UserService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/opal/UserService.java @@ -5,6 +5,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import uk.gov.hmcts.opal.authorisation.model.UserState; import uk.gov.hmcts.opal.dto.search.UserSearchDto; import uk.gov.hmcts.opal.entity.UserEntity; import uk.gov.hmcts.opal.repository.UserRepository; @@ -19,6 +20,8 @@ public class UserService implements UserServiceInterface { private final UserRepository userRepository; + private final BusinessUnitUserService businessUnitUserService; + private final UserSpecs specs = new UserSpecs(); @Override @@ -35,4 +38,13 @@ public List searchUsers(UserSearchDto criteria) { return page.getContent(); } + public UserState getUserStateByUsername(String username) { + UserEntity user = userRepository.findByUsername(username); + return UserState.builder() + .userId(user.getUserId()) + .userName(user.getUsername()) + .roles(businessUnitUserService.getAuthorisationRolesByUserId(user.getUserId())) + .build(); + } + } diff --git a/src/main/java/uk/gov/hmcts/opal/util/HttpUtil.java b/src/main/java/uk/gov/hmcts/opal/util/HttpUtil.java new file mode 100644 index 000000000..0f8b31afc --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/util/HttpUtil.java @@ -0,0 +1,44 @@ +package uk.gov.hmcts.opal.util; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import uk.gov.hmcts.opal.authentication.service.AccessTokenService; + +import java.util.List; +import java.util.Optional; + +import static uk.gov.hmcts.opal.authentication.service.AccessTokenService.AUTH_HEADER; + +public class HttpUtil { + + public static ResponseEntity> buildResponse(List contents) { + if (contents == null || contents.isEmpty()) { + return ResponseEntity.noContent().build(); + } + + return ResponseEntity.ok(contents); + } + + public static ResponseEntity buildResponse(T contents) { + if (contents == null) { + return ResponseEntity.noContent().build(); + } + + return ResponseEntity.ok(contents); + } + + public static ResponseEntity buildCreatedResponse(T contents) { + if (contents == null) { + return ResponseEntity.noContent().build(); + } + + return new ResponseEntity<>(contents, HttpStatus.CREATED); + } + + public static String extractPreferredUsername(HttpServletRequest request, AccessTokenService tokenService) { + return Optional.ofNullable(request.getHeader(AUTH_HEADER)) + .map(tokenService::extractPreferredUsername) + .orElse(null); + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java b/src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java deleted file mode 100644 index c28cee918..000000000 --- a/src/main/java/uk/gov/hmcts/opal/util/ResponseUtil.java +++ /dev/null @@ -1,24 +0,0 @@ -package uk.gov.hmcts.opal.util; - -import org.springframework.http.ResponseEntity; - -import java.util.List; - -public class ResponseUtil { - - public static ResponseEntity> buildResponse(List contents) { - if (contents == null || contents.isEmpty()) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(contents); - } - - public static ResponseEntity buildResponse(T contents) { - if (contents == null) { - return ResponseEntity.noContent().build(); - } - - return ResponseEntity.ok(contents); - } -} diff --git a/src/test/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserControllerTest.java b/src/test/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserControllerTest.java index c217cc254..fed83235e 100644 --- a/src/test/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserControllerTest.java +++ b/src/test/java/uk/gov/hmcts/opal/authentication/controller/AuthenticationInternalUserControllerTest.java @@ -23,7 +23,6 @@ import java.net.URI; import java.util.Date; -import java.util.Optional; import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -114,8 +113,8 @@ void handleOauthCode_ReturnsSecurityToken_WhenValidCodeProvided() { UserState userState = UserState.builder().userId(userEmail).userName("some name").build(); when(authenticationService.handleOauthCode(code)).thenReturn(accessToken); - when(accessTokenService.extractUserEmail(accessToken)).thenReturn(userEmail); - when(authorisationService.getAuthorisation(userEmail)).thenReturn(Optional.of(userState)); + when(accessTokenService.extractPreferredUsername(accessToken)).thenReturn(userEmail); + when(authorisationService.getAuthorisation(userEmail)).thenReturn(userState); // Act SecurityToken result = controller.handleOauthCode(code); @@ -127,7 +126,7 @@ void handleOauthCode_ReturnsSecurityToken_WhenValidCodeProvided() { // Verify interactions verify(authenticationService).handleOauthCode(code); - verify(accessTokenService).extractUserEmail(accessToken); + verify(accessTokenService).extractPreferredUsername(accessToken); verify(authorisationService).getAuthorisation(userEmail); } @@ -138,7 +137,7 @@ void handleOauthCode_ReturnsSecurityTokenWithoutUserState_WhenUserEmailNotPresen String accessToken = "accessToken"; when(authenticationService.handleOauthCode(code)).thenReturn(accessToken); - when(accessTokenService.extractUserEmail(accessToken)).thenReturn(null); + when(accessTokenService.extractPreferredUsername(accessToken)).thenReturn(null); // Act SecurityToken result = controller.handleOauthCode(code); @@ -150,7 +149,7 @@ void handleOauthCode_ReturnsSecurityTokenWithoutUserState_WhenUserEmailNotPresen // Verify interactions verify(authenticationService).handleOauthCode(code); - verify(accessTokenService).extractUserEmail(accessToken); + verify(accessTokenService).extractPreferredUsername(accessToken); verifyNoInteractions(authorisationService); } diff --git a/src/test/java/uk/gov/hmcts/opal/authentication/service/AccessTokenServiceTest.java b/src/test/java/uk/gov/hmcts/opal/authentication/service/AccessTokenServiceTest.java index e894d9072..841a6cf15 100644 --- a/src/test/java/uk/gov/hmcts/opal/authentication/service/AccessTokenServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/authentication/service/AccessTokenServiceTest.java @@ -111,7 +111,7 @@ public void testExtractToken_ValidAuthorizationHeader_ReturnsToken() { @Nested class ExtractUserEmail { @Test - void testExtractUserEmail_invalidToken() throws Exception { + void testExtractPreferredUsername_invalidToken() throws Exception { // Given String invalidToken = "invalidToken"; @@ -119,35 +119,90 @@ void testExtractUserEmail_invalidToken() throws Exception { assertThrows( OpalApiException.class, - () -> accessTokenService.extractUserEmail("Bearer " + invalidToken) + () -> accessTokenService.extractPreferredUsername("Bearer " + invalidToken) ); } @Test - void testExtractUserEmail_validToken() throws Exception { + void testExtractPreferredUsername_validToken() throws Exception { String token = "validToken"; String expectedEmail = "test@example.com"; - JWTClaimsSet.Builder claimsSetBuilder = new JWTClaimsSet.Builder(); + PlainJWT jwt = new PlainJWT(buildJwt()); - claimsSetBuilder.issuer("example.com") - .subject("john.doe@example.com") - .audience("client123") - .claim("preferred_username", "test@example.com"); + when(tokenValidator.parse(token)).thenReturn(jwt); + + // When + String username = accessTokenService.extractPreferredUsername("Bearer " + token); - JWTClaimsSet claimsSet = claimsSetBuilder.build(); + // Then + assertEquals(expectedEmail, username); + } - PlainJWT jwt = new PlainJWT(claimsSet); + @Test + void testExtractNameClaim_validToken() throws Exception { + // Given + PlainJWT jwt = new PlainJWT(buildJwt()); + when(tokenValidator.parse(any())).thenReturn(jwt); - when(tokenValidator.parse(token)).thenReturn(jwt); + // When + String claim = accessTokenService.extractName("Bearer encryptedToken"); + + // Then + assertEquals("opal-test", claim); + } + + @Test + void testExtractScpClaim_validToken() throws Exception { + // Given + PlainJWT jwt = new PlainJWT(buildJwt()); + when(tokenValidator.parse(any())).thenReturn(jwt); + + // When + String claim = accessTokenService.extractScp("Bearer encryptedToken"); + + // Then + assertEquals("opalinternaluser", claim); + } + + @Test + void testExtractUniqueNameClaim_validToken() throws Exception { + // Given + PlainJWT jwt = new PlainJWT(buildJwt()); + when(tokenValidator.parse(any())).thenReturn(jwt); // When - String email = accessTokenService.extractUserEmail("Bearer " + token); + String claim = accessTokenService.extractUniqueName("Bearer encryptedToken"); // Then - assertEquals(expectedEmail, email); + assertEquals("opal-test@example.com", claim); + } + + @Test + void testExtractUpnClaim_validToken() throws Exception { + // Given + PlainJWT jwt = new PlainJWT(buildJwt()); + when(tokenValidator.parse(any())).thenReturn(jwt); + + // When + String claim = accessTokenService.extractUpn("Bearer encryptedToken"); + + // Then + assertEquals("opal-test@example.com", claim); + } + + private JWTClaimsSet buildJwt() { + return new JWTClaimsSet.Builder() + .issuer("example.com") + .subject("john.doe@example.com") + .audience("client123") + .claim("preferred_username", "test@example.com") + .claim("name", "opal-test") + .claim("scp", "opalinternaluser") + .claim("unique_name", "opal-test@example.com") + .claim("upn", "opal-test@example.com") + .build(); } } } - diff --git a/src/test/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationServiceTest.java b/src/test/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationServiceTest.java index fd3927daf..7451958f6 100644 --- a/src/test/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/authorisation/service/AuthorisationServiceTest.java @@ -3,16 +3,21 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import uk.gov.hmcts.opal.authorisation.model.UserState; +import uk.gov.hmcts.opal.service.opal.UserService; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class AuthorisationServiceTest { + @Mock + UserService userService; + @InjectMocks private AuthorisationService authorisationService; @@ -20,12 +25,14 @@ class AuthorisationServiceTest { void getAuthorisation_ReturnsUserState_WhenUserFound() { // Arrange String emailAddress = "test@example.com"; + UserState userState = UserState.builder().userId("JS001").userName("John Smith").build(); + when(userService.getUserStateByUsername(any())).thenReturn(userState); // Act - Optional result = authorisationService.getAuthorisation(emailAddress); + UserState result = authorisationService.getAuthorisation(emailAddress); // Assert - assertTrue(result.isPresent()); + assertEquals(userState, result); // Add more assertions as needed to validate the returned UserState } } diff --git a/src/test/java/uk/gov/hmcts/opal/controllers/DefendantAccountControllerTest.java b/src/test/java/uk/gov/hmcts/opal/controllers/DefendantAccountControllerTest.java index b126212d9..b28cb7e20 100644 --- a/src/test/java/uk/gov/hmcts/opal/controllers/DefendantAccountControllerTest.java +++ b/src/test/java/uk/gov/hmcts/opal/controllers/DefendantAccountControllerTest.java @@ -8,6 +8,8 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import uk.gov.hmcts.opal.authentication.service.AccessTokenService; +import uk.gov.hmcts.opal.authorisation.model.UserState; import uk.gov.hmcts.opal.dto.AccountDetailsDto; import uk.gov.hmcts.opal.dto.AccountEnquiryDto; import uk.gov.hmcts.opal.dto.search.AccountSearchDto; @@ -18,7 +20,9 @@ import uk.gov.hmcts.opal.entity.DefendantAccountEntity; import uk.gov.hmcts.opal.service.opal.DefendantAccountService; import uk.gov.hmcts.opal.service.opal.NoteService; +import uk.gov.hmcts.opal.service.opal.UserService; +import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -37,6 +41,12 @@ class DefendantAccountControllerTest { @Mock private NoteService noteService; + @Mock + private AccessTokenService accessTokenService; // Injected to avoid NPE + + @Mock + private UserService userService; + @InjectMocks private DefendantAccountController defendantAccountController; @@ -135,9 +145,11 @@ public void testAddNote_Success() { // Arrange HttpServletRequest request = mock(HttpServletRequest.class); NoteDto mockResponse = new NoteDto(); + UserState userState = UserState.builder() + .userId("JS001").userName("John Smith").roles(Collections.emptySet()).build(); - when(request.getRemoteUser()).thenReturn("REMOTE_USER"); when(noteService.saveNote(any(NoteDto.class))).thenReturn(mockResponse); + when(userService.getUserStateByUsername(any())).thenReturn(userState); // Act AddNoteDto addNote = AddNoteDto.builder().build(); @@ -154,8 +166,10 @@ public void testAddNote_Success() { @Test public void testAddNote_NoContent() { HttpServletRequest request = mock(HttpServletRequest.class); + UserState userState = UserState.builder() + .userId("JS001").userName("John Smith").roles(Collections.emptySet()).build(); when(noteService.saveNote(any(NoteDto.class))).thenReturn(null); - when(request.getRemoteUser()).thenReturn("REMOTE_USER"); + when(userService.getUserStateByUsername(any())).thenReturn(userState); // Act AddNoteDto addNote = AddNoteDto.builder().build(); diff --git a/src/test/java/uk/gov/hmcts/opal/controllers/NoteControllerTest.java b/src/test/java/uk/gov/hmcts/opal/controllers/NoteControllerTest.java index f88b6d04b..3dea29935 100644 --- a/src/test/java/uk/gov/hmcts/opal/controllers/NoteControllerTest.java +++ b/src/test/java/uk/gov/hmcts/opal/controllers/NoteControllerTest.java @@ -1,5 +1,6 @@ package uk.gov.hmcts.opal.controllers; +import jakarta.servlet.http.HttpServletRequest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -7,14 +8,19 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import uk.gov.hmcts.opal.authentication.service.AccessTokenService; +import uk.gov.hmcts.opal.authorisation.model.UserState; import uk.gov.hmcts.opal.dto.NoteDto; import uk.gov.hmcts.opal.dto.search.NoteSearchDto; import uk.gov.hmcts.opal.service.opal.NoteService; +import uk.gov.hmcts.opal.service.opal.UserService; +import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -25,19 +31,29 @@ class NoteControllerTest { @Mock private NoteService noteService; + @Mock + private AccessTokenService accessTokenService; // Injected to avoid NPE + + @Mock + private UserService userService; + @InjectMocks private NoteController noteController; @Test void testCreateNote_Success() { // Arrange + HttpServletRequest request = mock(HttpServletRequest.class); NoteDto noteDtoRequest = NoteDto.builder().build(); NoteDto noteDtoResponse = NoteDto.builder().noteId(1L).build(); + UserState userState = UserState.builder() + .userId("JS001").userName("John Smith").roles(Collections.emptySet()).build(); when(noteService.saveNote(any(NoteDto.class))).thenReturn(noteDtoResponse); + when(userService.getUserStateByUsername(any())).thenReturn(userState); // Act - ResponseEntity response = noteController.createNote(noteDtoRequest); + ResponseEntity response = noteController.createNote(noteDtoRequest, request); // Assert assertEquals(HttpStatus.CREATED, response.getStatusCode()); diff --git a/src/test/java/uk/gov/hmcts/opal/controllers/TestingSupportControllerTest.java b/src/test/java/uk/gov/hmcts/opal/controllers/TestingSupportControllerTest.java index 4794d85c9..4461f15d7 100644 --- a/src/test/java/uk/gov/hmcts/opal/controllers/TestingSupportControllerTest.java +++ b/src/test/java/uk/gov/hmcts/opal/controllers/TestingSupportControllerTest.java @@ -147,7 +147,7 @@ public void getTokenForUser_shouldHandleExceptions() { @Test void parseToken_shouldReturnEmail() { String bearerToken = "Bearer token"; - when(accessTokenService.extractUserEmail(bearerToken)).thenReturn("my@email.com"); + when(accessTokenService.extractPreferredUsername(bearerToken)).thenReturn("my@email.com"); ResponseEntity response = controller.parseToken(bearerToken); diff --git a/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserServiceTest.java index 65e916b90..0c2297b76 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitUserServiceTest.java @@ -11,11 +11,15 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.repository.query.FluentQuery; +import uk.gov.hmcts.opal.authorisation.model.Role; import uk.gov.hmcts.opal.dto.search.BusinessUnitUserSearchDto; +import uk.gov.hmcts.opal.entity.BusinessUnitEntity; import uk.gov.hmcts.opal.entity.BusinessUnitUserEntity; import uk.gov.hmcts.opal.repository.BusinessUnitUserRepository; +import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.function.Function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,6 +33,9 @@ class BusinessUnitUserServiceTest { @Mock private BusinessUnitUserRepository businessUnitUserRepository; + @Mock + private UserEntitlementService userEntitlementService; + @InjectMocks private BusinessUnitUserService businessUnitUserService; @@ -70,5 +77,23 @@ void testSearchBusinessUnitUsers() { } + @Test + void testGetAuthorisationRolesByUserId() { + // Arrange + BusinessUnitEntity bue = BusinessUnitEntity.builder().businessUnitId((short)100).build(); + BusinessUnitUserEntity businessUnitUserEntity = BusinessUnitUserEntity.builder() + .businessUnitUserId("BUUserABCD").businessUnit(bue).build(); + List list = List.of(businessUnitUserEntity); + when(businessUnitUserRepository.findAllByUser_UserId(any())).thenReturn(list); + when(userEntitlementService.getPermissionsByBusinessUnitUserId(any())).thenReturn(Collections.emptySet()); + + // Act + Set result = businessUnitUserService.getAuthorisationRolesByUserId(""); + + // Assert + assertNotNull(result); + assertEquals(1, result.size()); + + } } diff --git a/src/test/java/uk/gov/hmcts/opal/service/opal/UserEntitlementServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/opal/UserEntitlementServiceTest.java index 2175c7804..58a4387b2 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/opal/UserEntitlementServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/opal/UserEntitlementServiceTest.java @@ -11,11 +11,14 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.repository.query.FluentQuery; +import uk.gov.hmcts.opal.authorisation.model.Permission; import uk.gov.hmcts.opal.dto.search.UserEntitlementSearchDto; +import uk.gov.hmcts.opal.entity.ApplicationFunctionEntity; import uk.gov.hmcts.opal.entity.UserEntitlementEntity; import uk.gov.hmcts.opal.repository.UserEntitlementRepository; import java.util.List; +import java.util.Set; import java.util.function.Function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -70,5 +73,23 @@ void testSearchUserEntitlements() { } + @Test + void testGetPermissionsByBusinessUnitUserId() { + // Arrange + ApplicationFunctionEntity afe = ApplicationFunctionEntity.builder() + .applicationFunctionId(100L).functionName("Add_Note").build(); + UserEntitlementEntity userEntitlementEntity = UserEntitlementEntity.builder() + .userEntitlementId(1L).applicationFunction(afe).build(); + List list = List.of(userEntitlementEntity); + when(userEntitlementRepository.findAllByBusinessUnitUser_BusinessUnitUserId(any())).thenReturn(list); + + // Act + Set result = userEntitlementService.getPermissionsByBusinessUnitUserId(""); + + // Assert + assertNotNull(result); + assertEquals(1, result.size()); + + } } diff --git a/src/test/java/uk/gov/hmcts/opal/service/opal/UserServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/opal/UserServiceTest.java index b5fbd6090..9b531f25b 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/opal/UserServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/opal/UserServiceTest.java @@ -11,10 +11,12 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.repository.query.FluentQuery; +import uk.gov.hmcts.opal.authorisation.model.UserState; import uk.gov.hmcts.opal.dto.search.UserSearchDto; import uk.gov.hmcts.opal.entity.UserEntity; import uk.gov.hmcts.opal.repository.UserRepository; +import java.util.Collections; import java.util.List; import java.util.function.Function; @@ -29,6 +31,9 @@ class UserServiceTest { @Mock private UserRepository userRepository; + @Mock + private BusinessUnitUserService businessUnitUserService; + @InjectMocks private UserService userService; @@ -68,5 +73,20 @@ void testSearchUsers() { } + @Test + void testGetUserStateByUsername() { + // Arrange + UserEntity userEntity = UserEntity.builder().userId("UID_001").username("John Smith").build(); + when(userRepository.findByUsername(any())).thenReturn(userEntity); + when(businessUnitUserService.getAuthorisationRolesByUserId(any())).thenReturn(Collections.emptySet()); + + // Act + UserState result = userService.getUserStateByUsername(""); + // Assert + assertNotNull(result); + assertEquals("UID_001", result.getUserId()); + assertEquals("John Smith", result.getUserName()); + + } } diff --git a/src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java b/src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java index 912e8f36c..e156e9096 100644 --- a/src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java +++ b/src/test/java/uk/gov/hmcts/opal/util/ResponseUtilTest.java @@ -18,7 +18,7 @@ void buildResponse_withNonNullList_returnsOkResponse() { List responseList = Arrays.asList("item1", "item2", "item3"); // Act - ResponseEntity> responseEntity = ResponseUtil.buildResponse(responseList); + ResponseEntity> responseEntity = HttpUtil.buildResponse(responseList); // Assert assertEquals(200, responseEntity.getStatusCode().value()); @@ -31,7 +31,7 @@ void buildResponse_withEmptyList_returnsNoContentResponse() { List responseList = Collections.emptyList(); // Act - ResponseEntity> responseEntity = ResponseUtil.buildResponse(responseList); + ResponseEntity> responseEntity = HttpUtil.buildResponse(responseList); // Assert assertEquals(204, responseEntity.getStatusCode().value()); @@ -44,7 +44,7 @@ void buildResponse_withNullList_returnsNoContentResponse() { List responseList = null; // Act - ResponseEntity> responseEntity = ResponseUtil.buildResponse(responseList); + ResponseEntity> responseEntity = HttpUtil.buildResponse(responseList); // Assert assertEquals(204, responseEntity.getStatusCode().value()); @@ -57,7 +57,7 @@ void buildResponse_withNonNullString_returnsOkResponse() { String response = "item1"; // Act - ResponseEntity responseEntity = ResponseUtil.buildResponse(response); + ResponseEntity responseEntity = HttpUtil.buildResponse(response); // Assert assertEquals(200, responseEntity.getStatusCode().value()); @@ -71,7 +71,7 @@ void buildResponse_withNullString_returnsNoContentResponse() { String response = null; // Act - ResponseEntity responseEntity = ResponseUtil.buildResponse(response); + ResponseEntity responseEntity = HttpUtil.buildResponse(response); // Assert assertEquals(204, responseEntity.getStatusCode().value());