Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: when checking stamps for structure and component, contributors … #821

Merged
merged 3 commits into from
Dec 3, 2024
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
Expand Up @@ -4,6 +4,7 @@
import fr.insee.rmes.config.auth.roles.Roles;
import fr.insee.rmes.config.auth.user.Stamp;
import fr.insee.rmes.exceptions.RmesRuntimeBadRequestException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -173,6 +174,9 @@ private boolean checkStampIsContributor(String body) {
private static @Nullable String extractContributorStampFromBody(String body) {
return (new JSONObject(body)).optString("contributor");
}
private static @Nullable JSONArray extractContributorStampsFromBody(String body) {
return (new JSONObject(body)).optJSONArray("contributor");
}

//for PUT and DELETE structure
public boolean isStructureContributor(String structureId){
Expand All @@ -183,7 +187,13 @@ public boolean isStructureContributor(String structureId){
// for POST structure or component
public boolean isStructureAndComponentContributor(String body) {
logger.trace("Check if {} can create the structure or component", methodSecurityExpressionRoot.getPrincipal());
return hasRole(Roles.STRUCTURES_CONTRIBUTOR)&& checkStampIsContributor(body);
Optional<Stamp> stamp = getStamp();
JSONArray contributors = extractContributorStampsFromBody(body);

if(contributors == null){
return false;
}
return hasRole(Roles.STRUCTURES_CONTRIBUTOR) && contributors.toList().stream().anyMatch(s -> ((String) s).equalsIgnoreCase(stamp.get().stamp()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import fr.insee.rmes.config.auth.security.DefaultSecurityContext;
import fr.insee.rmes.config.auth.security.OpenIDConnectSecurityContext;
import fr.insee.rmes.config.auth.user.Stamp;
import fr.insee.rmes.model.ValidationStatus;
import fr.insee.rmes.webservice.StructureResources;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -28,7 +27,6 @@

import static fr.insee.rmes.integration.authorizations.TokenForTestsConfiguration.*;
import static fr.insee.rmes.integration.authorizations.TokenForTestsConfiguration.KEY_FOR_ROLES_IN_ROLE_CLAIM;
import static fr.insee.rmes.model.ValidationStatus.UNPUBLISHED;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -68,7 +66,6 @@ class TestStructuresResourcesEnvProd {

int structureId=10;
int componentId=12;
ValidationStatus status= UNPUBLISHED;

@Test
void putStructureAdmin_ok() throws Exception {
Expand Down Expand Up @@ -139,7 +136,7 @@ void postStructureAsStructureContributor_ok() throws Exception {
mvc.perform(post("/structures/structure").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \""+timbre+"\"}"))
.content("{\"id\": \"1\",\"contributor\": [\""+timbre+"\"]}"))
.andExpect(status().isOk());
}

Expand Down Expand Up @@ -168,7 +165,7 @@ void postStructureAsStructureContributorWrongStamp_ko() throws Exception {
mvc.perform(post("/structures/structure").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \"wrong\"}"))
.content("{\"id\": \"1\",\"contributor\": [\"wrong\"]}"))
.andExpect(status().isForbidden());
}

Expand Down Expand Up @@ -228,7 +225,7 @@ void postComponentAsStructureContributor_ok() throws Exception {
mvc.perform(post("/structures/components").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \""+timbre+"\"}"))
.content("{\"id\": \"1\",\"contributor\": [\""+timbre+"\"]}"))
.andExpect(status().isCreated());
}

Expand Down Expand Up @@ -257,7 +254,7 @@ void postComponentAsStructureContributorWrongStamp_ko() throws Exception {
mvc.perform(post("/structures/components").header("Authorization", "Bearer toto")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{\"id\": \"1\",\"contributor\": \"wrong\"}"))
.content("{\"id\": \"1\",\"contributor\": [\"wrong\"]}"))
.andExpect(status().isForbidden());
}

Expand Down