From b3fc52cc89a4a723244d90a972d2f5270a18198d Mon Sep 17 00:00:00 2001 From: Holash Chand Date: Wed, 4 Oct 2023 16:07:05 +0530 Subject: [PATCH 1/2] resolve issue with attestation on entity update when attestation state is requested --- .../registry/helper/RegistryHelper.java | 54 ++++++++++++++++--- .../sunbirdrc/actors/ClaimPluginActor.java | 1 + 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java index d95f59584..bf742f07a 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java @@ -800,16 +800,19 @@ private JsonNode getUserInfoFromKeyCloak(HttpServletRequest request, String enti private JsonNode getUserInfoFromRegistry(HttpServletRequest request, String entityName) throws Exception { String userId = getUserId(request,entityName); if (userId != null) { - ObjectNode payload = getSearchByOwnerQuery(entityName, userId); - - watch.start("RegistryController.searchEntity"); - JsonNode result = searchEntity(payload); - watch.stop("RegistryController.searchEntity"); - return result; + return getEntityByUserId(entityName, userId); } 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); + watch.stop("RegistryController.searchEntity"); + return result; + } + @NotNull private ObjectNode getSearchByOwnerQuery(String entityName, String userId) { ObjectNode payload = JsonNodeFactory.instance.objectNode(); @@ -985,7 +988,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); + updateAttestation(attestationPolicy.getAttestorEntity(), userId, entity, attestations, propertyToUpdate); } } if (entity != null) { @@ -999,7 +1002,7 @@ public String getPropertyToUpdate(HttpServletRequest request, String entityId){ String propertyURI = getPropertyURI(entityId, request); return propertyURI.split("/")[0]; } - private void updateAttestation(ArrayNode attestations,String propertyToUpdate) { + private void updateAttestation(String attestorEntity, String userId, JsonNode entity, ArrayNode attestations,String propertyToUpdate) throws Exception { for (JsonNode attestation : attestations) { if (attestation.get(_osState.name()).asText().equals(States.PUBLISHED.name()) && !attestation.get("name").asText().equals(propertyToUpdate) @@ -1007,10 +1010,45 @@ private void updateAttestation(ArrayNode attestations,String propertyToUpdate) { ObjectNode propertiesOSID = attestation.get("propertiesOSID").deepCopy(); JSONUtil.removeNode(propertiesOSID, uuidPropertyName); ((ObjectNode) attestation).set(_osState.name(), JsonNodeFactory.instance.textNode(States.INVALID.name())); + } else if (attestation.get(_osState.name()).asText().equals(States.ATTESTATION_REQUESTED.name())) { + AttestationPolicy attestationPolicy = getAttestationPolicy(attestation.get("entityName").asText(), attestation.get("name").asText()); + ObjectNode propertiesOSID = attestation.get("propertiesOSID").deepCopy(); + Map> propertiesOSIDMapper = new HashMap<>(); + ObjectReader reader = objectMapper.readerFor(new TypeReference>() { + }); + for (Iterator> it = propertiesOSID.fields(); it.hasNext(); ) { + Map.Entry itr = it.next(); + if(itr.getValue().isArray() && !itr.getValue().isEmpty() && itr.getValue().get(0).isTextual()) { + List list = reader.readValue(itr.getValue()); + propertiesOSIDMapper.put(itr.getKey(), list); + } + } + JsonNode 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(JsonNode result, Object credentialTemplate) throws SignatureException.CreationException, SignatureException.UnreachableException { Map requestBodyMap = new HashMap<>(); diff --git a/java/sunbirdrc-actors/src/main/java/dev/sunbirdrc/actors/ClaimPluginActor.java b/java/sunbirdrc-actors/src/main/java/dev/sunbirdrc/actors/ClaimPluginActor.java index 92fd01746..bdb6106c1 100644 --- a/java/sunbirdrc-actors/src/main/java/dev/sunbirdrc/actors/ClaimPluginActor.java +++ b/java/sunbirdrc-actors/src/main/java/dev/sunbirdrc/actors/ClaimPluginActor.java @@ -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); From 8b2ba74a3d803cfb1038b3d476741a84e18b44d1 Mon Sep 17 00:00:00 2001 From: Sreejit-K Date: Wed, 17 Apr 2024 17:13:01 +0530 Subject: [PATCH 2/2] Added conditions of the propertyOSID feilds and added Actor flow --- .../registry/helper/RegistryHelper.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java index 6e7d1ae1e..9a29096da 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java @@ -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 @@ -844,7 +843,7 @@ private JsonNode getUserInfoFromRegistry(HttpServletRequest request, String enti private JsonNode getEntityByUserId(String entityName, String userId) throws Exception { ObjectNode payload = getSearchByOwnerQuery(entityName, userId); watch.start("RegistryController.searchEntity"); - JsonNode result = searchEntity(payload); + JsonNode result = searchEntity(payload, userId); watch.stop("RegistryController.searchEntity"); return result; } @@ -1038,7 +1037,7 @@ public String getPropertyToUpdate(HttpServletRequest request, String entityId){ return propertyURI.split("/")[0]; } - private void updateAttestation(String attestorEntity, String userId, JsonNode entity, 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) @@ -1056,19 +1055,22 @@ private void updateAttestation(String attestorEntity, String userId, JsonNode en } ((ObjectNode) attestation).set(_osState.name(), JsonNodeFactory.instance.textNode(States.INVALID.name())); } else if (attestation.get(_osState.name()).asText().equals(States.ATTESTATION_REQUESTED.name())) { - AttestationPolicy attestationPolicy = getAttestationPolicy(attestation.get("entityName").asText(), attestation.get("name").asText()); - ObjectNode propertiesOSID = attestation.get("propertiesOSID").deepCopy(); - Map> propertiesOSIDMapper = new HashMap<>(); - ObjectReader reader = objectMapper.readerFor(new TypeReference>() { - }); - for (Iterator> it = propertiesOSID.fields(); it.hasNext(); ) { - Map.Entry itr = it.next(); - if(itr.getValue().isArray() && !itr.getValue().isEmpty() && itr.getValue().get(0).isTextual()) { - List list = reader.readValue(itr.getValue()); - propertiesOSIDMapper.put(itr.getKey(), list); + JsonNode propertyData = JSONUtil.extractPropertyDataFromEntity(entity, attestationPolicy.getAttestationProperties(), null); + if (attestation.has("propertiesOSID")) { + ObjectNode propertiesOSID = attestations.get("propertiesOSID").deepCopy(); + Map> propertiesOSIDMapper = new HashMap<>(); + ObjectReader reader = objectMapper.readerFor(new TypeReference>() { + }); + for (Iterator> it = propertiesOSID.fields(); it.hasNext(); ) { + Map.Entry itr = it.next(); + if(itr.getValue().isArray() && !itr.getValue().isEmpty() && itr.getValue().get(0).isTextual()) { + List list = reader.readValue(itr.getValue()); + propertiesOSIDMapper.put(itr.getKey(), list); + } } + propertyData = JSONUtil.extractPropertyDataFromEntity(entity, attestationPolicy.getAttestationProperties(), propertiesOSIDMapper); } - JsonNode 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());