Skip to content

Commit c5ce259

Browse files
authored
Merge pull request #310 from Sreejit-K/attestation-entity-update
Closing the claim and invalidating the pending attestations when entity is updated
2 parents 3ad4c3e + 8b2ba74 commit c5ce259

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@
7777
import static dev.sunbirdrc.registry.Constants.*;
7878
import static dev.sunbirdrc.registry.exception.ErrorMessages.*;
7979
import static dev.sunbirdrc.registry.middleware.util.Constants.*;
80-
import static dev.sunbirdrc.registry.middleware.util.OSSystemFields._osState;
81-
import static dev.sunbirdrc.registry.middleware.util.OSSystemFields.osOwner;
80+
import static dev.sunbirdrc.registry.middleware.util.OSSystemFields.*;
8281

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

843+
private JsonNode getEntityByUserId(String entityName, String userId) throws Exception {
844+
ObjectNode payload = getSearchByOwnerQuery(entityName, userId);
845+
watch.start("RegistryController.searchEntity");
846+
JsonNode result = searchEntity(payload, userId);
847+
watch.stop("RegistryController.searchEntity");
848+
return result;
849+
}
850+
844851
@NotNull
845852
private ObjectNode getSearchByOwnerQuery(String entityName, String userId) {
846853
ObjectNode payload = JsonNodeFactory.instance.objectNode();
@@ -1015,7 +1022,7 @@ public void invalidateAttestation(String entityName, String entityId, String use
10151022
}
10161023
if (entity.has(policyName) && entity.get(policyName).isArray()) {
10171024
ArrayNode attestations = (ArrayNode) entity.get(policyName);
1018-
updateAttestation(attestations, propertyToUpdate, attestationPolicy);
1025+
updateAttestation(attestationPolicy.getAttestorEntity(), userId, entity,attestations, propertyToUpdate, attestationPolicy);
10191026
}
10201027
}
10211028
if (entity != null) {
@@ -1029,7 +1036,8 @@ public String getPropertyToUpdate(HttpServletRequest request, String entityId){
10291036
String propertyURI = getPropertyURI(entityId, request);
10301037
return propertyURI.split("/")[0];
10311038
}
1032-
private void updateAttestation(ArrayNode attestations,String propertyToUpdate, AttestationPolicy attestationPolicy) {
1039+
1040+
private void updateAttestation(String attestorEntity, String userId, JsonNode entity, ArrayNode attestations,String propertyToUpdate, AttestationPolicy attestationPolicy) throws Exception {
10331041
for (JsonNode attestation : attestations) {
10341042
if (attestation.get(_osState.name()).asText().equals(States.PUBLISHED.name())
10351043
&& !attestation.get("name").asText().equals(propertyToUpdate)
@@ -1046,10 +1054,48 @@ private void updateAttestation(ArrayNode attestations,String propertyToUpdate, A
10461054
);
10471055
}
10481056
((ObjectNode) attestation).set(_osState.name(), JsonNodeFactory.instance.textNode(States.INVALID.name()));
1057+
} else if (attestation.get(_osState.name()).asText().equals(States.ATTESTATION_REQUESTED.name())) {
1058+
JsonNode propertyData = JSONUtil.extractPropertyDataFromEntity(entity, attestationPolicy.getAttestationProperties(), null);
1059+
if (attestation.has("propertiesOSID")) {
1060+
ObjectNode propertiesOSID = attestations.get("propertiesOSID").deepCopy();
1061+
Map<String, List<String>> propertiesOSIDMapper = new HashMap<>();
1062+
ObjectReader reader = objectMapper.readerFor(new TypeReference<List<String>>() {
1063+
});
1064+
for (Iterator<Map.Entry<String, JsonNode>> it = propertiesOSID.fields(); it.hasNext(); ) {
1065+
Map.Entry<String, JsonNode> itr = it.next();
1066+
if(itr.getValue().isArray() && !itr.getValue().isEmpty() && itr.getValue().get(0).isTextual()) {
1067+
List<String> list = reader.readValue(itr.getValue());
1068+
propertiesOSIDMapper.put(itr.getKey(), list);
1069+
}
1070+
}
1071+
propertyData = JSONUtil.extractPropertyDataFromEntity(entity, attestationPolicy.getAttestationProperties(), propertiesOSIDMapper);
1072+
}
1073+
1074+
if(!propertyData.equals(JSONUtil.convertStringJsonNode(attestation.get("propertyData").asText()))) {
1075+
((ObjectNode) attestation).set(_osState.name(), JsonNodeFactory.instance.textNode(States.DRAFT.name()));
1076+
invalidateClaim(attestorEntity, userId, attestation.get(_osClaimId.name()).asText());
1077+
}
10491078
}
10501079
}
10511080
}
10521081

1082+
public void invalidateClaim(String attestorEntityName, String userId, String claimId) throws Exception {
1083+
final String attestorPlugin = "did:internal:ClaimPluginActor";
1084+
Action action = Action.SET_TO_DRAFT;
1085+
ObjectNode additionalInputs = JsonNodeFactory.instance.objectNode();
1086+
JsonNode attestorInfo = getEntityByUserId(attestorEntityName, userId).get(attestorEntityName).get(0);
1087+
additionalInputs.put("claimId", claimId);
1088+
additionalInputs.put("action", action.name());
1089+
additionalInputs.put("notes", "Closed due to entity update");
1090+
additionalInputs.set("attestorInfo", attestorInfo);
1091+
PluginRequestMessage pluginRequestMessage = PluginRequestMessage.builder().build();
1092+
pluginRequestMessage.setAttestorPlugin(attestorPlugin);
1093+
pluginRequestMessage.setAdditionalInputs(additionalInputs);
1094+
pluginRequestMessage.setStatus(action.name());
1095+
pluginRequestMessage.setUserId(userId);
1096+
PluginRouter.route(pluginRequestMessage);
1097+
}
1098+
10531099
public Object getSignedDoc(String title, JsonNode result, Object credentialTemplate) throws
10541100
SignatureException.CreationException, SignatureException.UnreachableException {
10551101
Map<String, Object> requestBodyMap = new HashMap<>();

java/sunbirdrc-actors/src/main/java/dev/sunbirdrc/actors/ClaimPluginActor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected void onReceive(MessageProtos.Message request) throws Throwable {
4444
case RAISE_CLAIM:
4545
riseClaim(pluginRequestMessage);
4646
break;
47+
case SET_TO_DRAFT:
4748
case GRANT_CLAIM:
4849
case REJECT_CLAIM:
4950
attestClaim(pluginRequestMessage);

0 commit comments

Comments
 (0)