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

Closing the claim and invalidating the pending attestations when entity is updated #310

Merged
merged 3 commits into from
Apr 17, 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 @@ -77,8 +77,7 @@
import static dev.sunbirdrc.registry.Constants.*;
import static dev.sunbirdrc.registry.exception.ErrorMessages.*;
import static dev.sunbirdrc.registry.middleware.util.Constants.*;
import static dev.sunbirdrc.registry.middleware.util.OSSystemFields._osState;
import static dev.sunbirdrc.registry.middleware.util.OSSystemFields.osOwner;
import static dev.sunbirdrc.registry.middleware.util.OSSystemFields.*;

/**
* This is helper class, user-service calls this class in-order to access registry functionality
Expand Down Expand Up @@ -841,6 +840,14 @@ private JsonNode getUserInfoFromRegistry(HttpServletRequest request, String enti
throw new Exception("Forbidden");
}

private JsonNode getEntityByUserId(String entityName, String userId) throws Exception {
ObjectNode payload = getSearchByOwnerQuery(entityName, userId);
watch.start("RegistryController.searchEntity");
JsonNode result = searchEntity(payload, userId);
watch.stop("RegistryController.searchEntity");
return result;
}

@NotNull
private ObjectNode getSearchByOwnerQuery(String entityName, String userId) {
ObjectNode payload = JsonNodeFactory.instance.objectNode();
Expand Down Expand Up @@ -1015,7 +1022,7 @@ public void invalidateAttestation(String entityName, String entityId, String use
}
if (entity.has(policyName) && entity.get(policyName).isArray()) {
ArrayNode attestations = (ArrayNode) entity.get(policyName);
updateAttestation(attestations, propertyToUpdate, attestationPolicy);
updateAttestation(attestationPolicy.getAttestorEntity(), userId, entity,attestations, propertyToUpdate, attestationPolicy);
}
}
if (entity != null) {
Expand All @@ -1029,7 +1036,8 @@ public String getPropertyToUpdate(HttpServletRequest request, String entityId){
String propertyURI = getPropertyURI(entityId, request);
return propertyURI.split("/")[0];
}
private void updateAttestation(ArrayNode attestations,String propertyToUpdate, AttestationPolicy attestationPolicy) {

private void updateAttestation(String attestorEntity, String userId, JsonNode entity, ArrayNode attestations,String propertyToUpdate, AttestationPolicy attestationPolicy) throws Exception {
for (JsonNode attestation : attestations) {
if (attestation.get(_osState.name()).asText().equals(States.PUBLISHED.name())
&& !attestation.get("name").asText().equals(propertyToUpdate)
Expand All @@ -1046,10 +1054,48 @@ private void updateAttestation(ArrayNode attestations,String propertyToUpdate, A
);
}
((ObjectNode) attestation).set(_osState.name(), JsonNodeFactory.instance.textNode(States.INVALID.name()));
} else if (attestation.get(_osState.name()).asText().equals(States.ATTESTATION_REQUESTED.name())) {
JsonNode propertyData = JSONUtil.extractPropertyDataFromEntity(entity, attestationPolicy.getAttestationProperties(), null);
if (attestation.has("propertiesOSID")) {
ObjectNode propertiesOSID = attestations.get("propertiesOSID").deepCopy();
Map<String, List<String>> propertiesOSIDMapper = new HashMap<>();
ObjectReader reader = objectMapper.readerFor(new TypeReference<List<String>>() {
});
for (Iterator<Map.Entry<String, JsonNode>> it = propertiesOSID.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> itr = it.next();
if(itr.getValue().isArray() && !itr.getValue().isEmpty() && itr.getValue().get(0).isTextual()) {
List<String> list = reader.readValue(itr.getValue());
propertiesOSIDMapper.put(itr.getKey(), list);
}
}
propertyData = JSONUtil.extractPropertyDataFromEntity(entity, attestationPolicy.getAttestationProperties(), propertiesOSIDMapper);
}

if(!propertyData.equals(JSONUtil.convertStringJsonNode(attestation.get("propertyData").asText()))) {
((ObjectNode) attestation).set(_osState.name(), JsonNodeFactory.instance.textNode(States.DRAFT.name()));
invalidateClaim(attestorEntity, userId, attestation.get(_osClaimId.name()).asText());
}
}
}
}

public void invalidateClaim(String attestorEntityName, String userId, String claimId) throws Exception {
final String attestorPlugin = "did:internal:ClaimPluginActor";
Action action = Action.SET_TO_DRAFT;
ObjectNode additionalInputs = JsonNodeFactory.instance.objectNode();
JsonNode attestorInfo = getEntityByUserId(attestorEntityName, userId).get(attestorEntityName).get(0);
additionalInputs.put("claimId", claimId);
additionalInputs.put("action", action.name());
additionalInputs.put("notes", "Closed due to entity update");
additionalInputs.set("attestorInfo", attestorInfo);
PluginRequestMessage pluginRequestMessage = PluginRequestMessage.builder().build();
pluginRequestMessage.setAttestorPlugin(attestorPlugin);
pluginRequestMessage.setAdditionalInputs(additionalInputs);
pluginRequestMessage.setStatus(action.name());
pluginRequestMessage.setUserId(userId);
PluginRouter.route(pluginRequestMessage);
}

public Object getSignedDoc(String title, JsonNode result, Object credentialTemplate) throws
SignatureException.CreationException, SignatureException.UnreachableException {
Map<String, Object> requestBodyMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected void onReceive(MessageProtos.Message request) throws Throwable {
case RAISE_CLAIM:
riseClaim(pluginRequestMessage);
break;
case SET_TO_DRAFT:
case GRANT_CLAIM:
case REJECT_CLAIM:
attestClaim(pluginRequestMessage);
Expand Down
Loading