Skip to content

Commit

Permalink
Added new role assignments query call/models and implemented retrievi…
Browse files Browse the repository at this point in the history
…ng role assignments, including labels, for a user in RoleAssignmentService
  • Loading branch information
Gareth40342 committed Nov 6, 2024
1 parent 5af37bb commit 2ab3c6d
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import uk.gov.hmcts.reform.civil.ras.model.QueryRequest;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentRequest;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentServiceResponse;
import uk.gov.hmcts.reform.civil.ras.model.UpdateRoleAssignmentResponse;
Expand All @@ -30,6 +32,23 @@ RoleAssignmentServiceResponse getRoleAssignments(
@RequestHeader(SERVICE_AUTHORIZATION) String serviceAuthorization,
@PathVariable(ACTOR_ID) String actorId);

@PostMapping(
value = "/am/role-assignments/query",
consumes = APPLICATION_JSON_VALUE,
headers = CONTENT_TYPE + "=" + APPLICATION_JSON_VALUE
)
RoleAssignmentServiceResponse getRoleAssignments(
@RequestHeader(AUTHORIZATION) String authorization,
@RequestHeader(SERVICE_AUTHORIZATION) String serviceAuthorization,
@RequestHeader(value = "x-correlation-id",
required = false) String correlationId,
@RequestHeader(value = "pageNumber", required = false) Integer pageNumber,
@RequestHeader(value = "size", required = false) Integer size,
@RequestHeader(value = "sort", required = false) String sort,
@RequestHeader(value = "direction", required = false) String direction,
@RequestBody(required = true) QueryRequest queryRequest,
@RequestParam(value = "includeLabels", defaultValue = "false") Boolean includeLabels);

@PostMapping(
value = "/am/role-assignments",
consumes = APPLICATION_JSON_VALUE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package uk.gov.hmcts.reform.civil.ras.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Singular;
import lombok.Value;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Value
@Builder
@AllArgsConstructor
@Getter
public class QueryRequest {

@Singular("actorId")
private final List<String> actorId;
@Singular("roleType")
private final List<String> roleType;
@Singular("roleName")
private final List<String> roleName;
@Singular("classification")
private final List<String> classification;
@Singular("grantType")
private final List<String> grantType;

private LocalDateTime validAt;
@Singular("roleCategory")
private final List<String> roleCategory;

private Map<String, List<String>> attributes;
@Singular("authorisations")
private final List<String> authorisations;

@Singular("hasAttributes")
private final List<String> hasAttributes;

private Boolean readOnly;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class RoleAssignmentResponse {
private String actorIdType;
private String roleType;
private String roleName;
private String roleLabel;
private String classification;
private String grantType;
private String roleCategory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.stereotype.Service;
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;
import uk.gov.hmcts.reform.civil.ras.client.RoleAssignmentsApi;
import uk.gov.hmcts.reform.civil.ras.model.QueryRequest;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentRequest;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentServiceResponse;

Expand All @@ -30,6 +31,27 @@ public RoleAssignmentServiceResponse getRoleAssignments(String actorId,
);
}

public RoleAssignmentServiceResponse getRoleAssignmentsWithLabels(String actorId, String authorization) {

if (log.isDebugEnabled()) {
log.debug(actorId, "Getting Role assignments for actorId {0}");
}

return roleAssignmentApi.getRoleAssignments(
authorization,
authTokenGenerator.generate(),
null,
null,
null,
null,
null,
QueryRequest.builder()
.actorId(actorId)
.build(),
true
);
}

public void assignUserRoles(String actorId, String authorization, RoleAssignmentRequest roleAssignmentRequest) {
if (log.isDebugEnabled()) {
log.debug(actorId, "Assigning roles to actorId {0}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;
import uk.gov.hmcts.reform.civil.ras.client.RoleAssignmentsApi;
import uk.gov.hmcts.reform.civil.ras.model.GrantType;
import uk.gov.hmcts.reform.civil.ras.model.QueryRequest;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignment;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentRequest;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentResponse;
import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentServiceResponse;
import uk.gov.hmcts.reform.civil.ras.model.RoleCategory;
import uk.gov.hmcts.reform.civil.ras.model.RoleRequest;
import uk.gov.hmcts.reform.civil.ras.model.RoleType;

import java.util.List;
import java.util.Map;
Expand All @@ -28,6 +28,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.hmcts.reform.civil.ras.model.RoleType.ORGANISATION;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {
Expand Down Expand Up @@ -65,6 +66,35 @@ void init() {
when(roleAssignmentApi.getRoleAssignments(anyString(), anyString(), anyString())).thenReturn(RAS_RESPONSE);
}

@Test
void getRoleAssignmentsWithLabels_shouldReturnExpectAssignments() {
RoleAssignmentServiceResponse expected = RoleAssignmentServiceResponse.builder()
.roleAssignmentResponse(
List.of(RoleAssignmentResponse
.builder()
.actorId(ACTORID)
.roleLabel("Role Label")
.build()
)
)
.build();
when(roleAssignmentApi.getRoleAssignments(
eq(USER_AUTH_TOKEN),
eq(SERVICE_TOKEN),
eq(null),
eq(null),
eq(null),
eq(null),
eq(null),
eq(QueryRequest.builder().actorId(ACTORID).build()),
eq(true))
).thenReturn(expected);

var actual = roleAssignmentsService.getRoleAssignmentsWithLabels(ACTORID, USER_AUTH_TOKEN);

assertEquals(expected, actual);
}

@Test
void shouldReturn() {
var roleAssignmentsExpected = roleAssignmentsService.getRoleAssignments(ACTORID, USER_AUTH_TOKEN);
Expand All @@ -74,17 +104,18 @@ void shouldReturn() {
@Test
void shouldPostExpectedPayload() {
RoleAssignmentRequest request = RoleAssignmentRequest.builder()
.roleRequest(RoleRequest.builder()
.assignerId(ACTORID)
.reference("civil-hearings-system-user")
.process("civil-system-user")
.replaceExisting(true)
.build())
.roleRequest(
RoleRequest.builder()
.assignerId(ACTORID)
.reference("civil-hearings-system-user")
.process("civil-system-user")
.replaceExisting(true)
.build())
.requestedRoles(List.of(
RoleAssignment.builder()
.actorId(ACTORID)
.actorIdType("IDAM")
.roleType(RoleType.ORGANISATION)
.roleType(ORGANISATION)
.classification("PUBLIC")
.grantType(GrantType.STANDARD)
.roleCategory(RoleCategory.SYSTEM)
Expand Down

0 comments on commit 2ab3c6d

Please sign in to comment.