Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.gov.moj.cpp.accesscontrol.sjp.providers;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class ProsecutingAuthorityAccess {
Expand All @@ -11,10 +13,17 @@ public class ProsecutingAuthorityAccess {

private String prosecutingAuthority;

private List<String> agentProsecutorAuthorityAccess;

private ProsecutingAuthorityAccess(final String prosecutingAuthority) {
this.prosecutingAuthority = prosecutingAuthority;
}

private ProsecutingAuthorityAccess(final String prosecutingAuthority, final List<String> agentProsecutorAuthorityAccess) {
this.prosecutingAuthority = prosecutingAuthority;
this.agentProsecutorAuthorityAccess = agentProsecutorAuthorityAccess;
}

public static ProsecutingAuthorityAccess of(final String prosecutingAuthority) {

if (StringUtils.isEmpty(prosecutingAuthority)) {
Expand All @@ -26,13 +35,31 @@ public static ProsecutingAuthorityAccess of(final String prosecutingAuthority) {
return new ProsecutingAuthorityAccess(prosecutingAuthority);
}

public static ProsecutingAuthorityAccess of(final String prosecutingAuthority, final List<String> agentProsecutorAuthorityAccess) {

if (StringUtils.isEmpty(prosecutingAuthority)) {
NONE.agentProsecutorAuthorityAccess = agentProsecutorAuthorityAccess;
return NONE;
} else if (prosecutingAuthority.equals(ALL.getProsecutingAuthority())) {
ALL.agentProsecutorAuthorityAccess = agentProsecutorAuthorityAccess;
return ALL;
}

return new ProsecutingAuthorityAccess(prosecutingAuthority, agentProsecutorAuthorityAccess);
}

public String getProsecutingAuthority() {
return prosecutingAuthority;
}

public List<String> getAgentProsecutorAuthorityAccess() {
return agentProsecutorAuthorityAccess;
}

public boolean hasAccess(final String prosecutingAuthority) {
return ALL.getProsecutingAuthority().equals(this.getProsecutingAuthority()) ||
(this.getProsecutingAuthority() != null &&
this.getProsecutingAuthority().equals(prosecutingAuthority));
this.getProsecutingAuthority().equals(prosecutingAuthority)) ||
(agentProsecutorAuthorityAccess != null && agentProsecutorAuthorityAccess.contains(prosecutingAuthority));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonValue;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.lang.Boolean.valueOf;
import static uk.gov.moj.cpp.accesscontrol.drools.constants.AccessControlFrameworkComponent.ACCESS_CONTROL;
import static uk.gov.moj.cpp.accesscontrol.sjp.providers.SjpProvider.jsonBuilderFactory;
Expand Down Expand Up @@ -62,7 +67,15 @@ public ProsecutingAuthorityAccess getCurrentUsersProsecutingAuthorityAccess(fina

private ProsecutingAuthorityAccess buildFromResponseJson(final JsonObject responsePayload) {

return ProsecutingAuthorityAccess.of(responsePayload.getString("prosecutingAuthorityAccess", null));
final List<String> agentProsecutors = Optional
.ofNullable(responsePayload.getJsonArray("agentProsecutorAuthorityAccess"))
.map(agentAccess -> agentAccess.getValuesAs(JsonObject.class)
.stream()
.map(agent -> agent.getString("prosecutingAuthority"))
.collect(Collectors.toList()))
.orElse(Collections.emptyList());

return ProsecutingAuthorityAccess.of(responsePayload.getString("prosecutingAuthorityAccess", null), agentProsecutors);
}

private JsonValue buildRequestPayload(final JsonEnvelope envelope) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import java.util.List;

import org.junit.jupiter.api.Test;

public class ProsecutingAuthorityAccessTest {
Expand Down Expand Up @@ -43,6 +45,20 @@ public void shouldReturnFalseForInvalidProsecutingAuthorityAccess() {
assertThat(prosecutingAuthorityAccess.hasAccess(""), is(false));
}

@Test
public void shouldGrantAccessViaAgentProsecutorAuthorityAccess() {

final ProsecutingAuthorityAccess prosecutingAuthorityAccess =
ProsecutingAuthorityAccess.of(PROSECUTOR, List.of("AGENT1", "AGENT2"));

assertThat(prosecutingAuthorityAccess.getProsecutingAuthority(), is(PROSECUTOR));
assertThat(prosecutingAuthorityAccess.getAgentProsecutorAuthorityAccess(), is(List.of("AGENT1", "AGENT2")));
assertThat(prosecutingAuthorityAccess.hasAccess(PROSECUTOR), is(true));
assertThat(prosecutingAuthorityAccess.hasAccess("AGENT1"), is(true));
assertThat(prosecutingAuthorityAccess.hasAccess("AGENT2"), is(true));
assertThat(prosecutingAuthorityAccess.hasAccess("OTHER"), is(false));
}

@Test
public void shouldBuildAllProsecutingAuthorityAccess() {

Expand Down
Loading