@Test public void testDiff_field1() throws Exception { EntityMetadata md = EvalTestContext.getMd("./testMetadata-restricted.json"); JsonDoc oldDoc = EvalTestContext.getDoc("./doc-restricted.json"); JsonDoc newDoc = EvalTestContext.getDoc("./doc-restricted.json"); newDoc.modify(new Path("field1"), JSON_NODE_FACTORY.textNode("test"), true); Set<String> roles = new HashSet<>(); roles.add("somerole"); FieldAccessRoleEvaluator eval = new FieldAccessRoleEvaluator(md, roles); Set<Path> list = eval.getInaccessibleFields_Update(newDoc, oldDoc); Assert.assertEquals(1, list.size()); Assert.assertTrue(list.contains(new Path("field1"))); }
public Set<Path> getInaccessibleFields_Update(JsonDoc newDoc, JsonDoc oldDoc) { if (comparator == null) { comparator = md.getEntitySchema().getDocComparator(); } Set<Path> inaccessibleFields = getInaccessibleFields(Operation.update); Set<Path> ret = new HashSet<>(); if (!inaccessibleFields.isEmpty()) { try { diff = comparator.compareNodes(oldDoc.getRoot(), newDoc.getRoot()); } catch (Exception e) { throw new RuntimeException(e); } for (DocComparator.Delta<JsonNode> d : diff.getDelta()) { if ((d instanceof DocComparator.Addition && ((DocComparator.Addition<JsonNode>) d).getAddedNode().isValueNode()) || (d instanceof DocComparator.Removal && ((DocComparator.Removal<JsonNode>) d).getRemovedNode().isValueNode()) || (d instanceof DocComparator.Modification && ((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode().isValueNode())) { FieldTreeNode fieldMd = md.resolve(d.getField()); boolean modified = true; if (d instanceof JsonCompare.Modification) { Object o1 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode()); Object o2 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getModifiedNode()); if (o1.equals(o2)) { modified = false; } } if (modified && inaccessibleFields.contains(fieldMd.getFullPath())) { ret.add(d.getField()); } } if (d instanceof DocComparator.Addition || d instanceof DocComparator.Removal || d instanceof DocComparator.Move) { Path field = d.getField(); if (field.numSegments() > 2) { Path parent = md.resolve(field.prefix(-1)).getFullPath(); if (inaccessibleFields.contains(parent)) { ret.add(parent); } } } } } return ret; }
FieldAccessRoleEvaluator { public Set<Path> getInaccessibleFields_Update(JsonDoc newDoc, JsonDoc oldDoc) { if (comparator == null) { comparator = md.getEntitySchema().getDocComparator(); } Set<Path> inaccessibleFields = getInaccessibleFields(Operation.update); Set<Path> ret = new HashSet<>(); if (!inaccessibleFields.isEmpty()) { try { diff = comparator.compareNodes(oldDoc.getRoot(), newDoc.getRoot()); } catch (Exception e) { throw new RuntimeException(e); } for (DocComparator.Delta<JsonNode> d : diff.getDelta()) { if ((d instanceof DocComparator.Addition && ((DocComparator.Addition<JsonNode>) d).getAddedNode().isValueNode()) || (d instanceof DocComparator.Removal && ((DocComparator.Removal<JsonNode>) d).getRemovedNode().isValueNode()) || (d instanceof DocComparator.Modification && ((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode().isValueNode())) { FieldTreeNode fieldMd = md.resolve(d.getField()); boolean modified = true; if (d instanceof JsonCompare.Modification) { Object o1 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode()); Object o2 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getModifiedNode()); if (o1.equals(o2)) { modified = false; } } if (modified && inaccessibleFields.contains(fieldMd.getFullPath())) { ret.add(d.getField()); } } if (d instanceof DocComparator.Addition || d instanceof DocComparator.Removal || d instanceof DocComparator.Move) { Path field = d.getField(); if (field.numSegments() > 2) { Path parent = md.resolve(field.prefix(-1)).getFullPath(); if (inaccessibleFields.contains(parent)) { ret.add(parent); } } } } } return ret; } }
FieldAccessRoleEvaluator { public Set<Path> getInaccessibleFields_Update(JsonDoc newDoc, JsonDoc oldDoc) { if (comparator == null) { comparator = md.getEntitySchema().getDocComparator(); } Set<Path> inaccessibleFields = getInaccessibleFields(Operation.update); Set<Path> ret = new HashSet<>(); if (!inaccessibleFields.isEmpty()) { try { diff = comparator.compareNodes(oldDoc.getRoot(), newDoc.getRoot()); } catch (Exception e) { throw new RuntimeException(e); } for (DocComparator.Delta<JsonNode> d : diff.getDelta()) { if ((d instanceof DocComparator.Addition && ((DocComparator.Addition<JsonNode>) d).getAddedNode().isValueNode()) || (d instanceof DocComparator.Removal && ((DocComparator.Removal<JsonNode>) d).getRemovedNode().isValueNode()) || (d instanceof DocComparator.Modification && ((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode().isValueNode())) { FieldTreeNode fieldMd = md.resolve(d.getField()); boolean modified = true; if (d instanceof JsonCompare.Modification) { Object o1 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode()); Object o2 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getModifiedNode()); if (o1.equals(o2)) { modified = false; } } if (modified && inaccessibleFields.contains(fieldMd.getFullPath())) { ret.add(d.getField()); } } if (d instanceof DocComparator.Addition || d instanceof DocComparator.Removal || d instanceof DocComparator.Move) { Path field = d.getField(); if (field.numSegments() > 2) { Path parent = md.resolve(field.prefix(-1)).getFullPath(); if (inaccessibleFields.contains(parent)) { ret.add(parent); } } } } } return ret; } FieldAccessRoleEvaluator(EntityMetadata md, Set<String> callerRoles); }
FieldAccessRoleEvaluator { public Set<Path> getInaccessibleFields_Update(JsonDoc newDoc, JsonDoc oldDoc) { if (comparator == null) { comparator = md.getEntitySchema().getDocComparator(); } Set<Path> inaccessibleFields = getInaccessibleFields(Operation.update); Set<Path> ret = new HashSet<>(); if (!inaccessibleFields.isEmpty()) { try { diff = comparator.compareNodes(oldDoc.getRoot(), newDoc.getRoot()); } catch (Exception e) { throw new RuntimeException(e); } for (DocComparator.Delta<JsonNode> d : diff.getDelta()) { if ((d instanceof DocComparator.Addition && ((DocComparator.Addition<JsonNode>) d).getAddedNode().isValueNode()) || (d instanceof DocComparator.Removal && ((DocComparator.Removal<JsonNode>) d).getRemovedNode().isValueNode()) || (d instanceof DocComparator.Modification && ((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode().isValueNode())) { FieldTreeNode fieldMd = md.resolve(d.getField()); boolean modified = true; if (d instanceof JsonCompare.Modification) { Object o1 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode()); Object o2 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getModifiedNode()); if (o1.equals(o2)) { modified = false; } } if (modified && inaccessibleFields.contains(fieldMd.getFullPath())) { ret.add(d.getField()); } } if (d instanceof DocComparator.Addition || d instanceof DocComparator.Removal || d instanceof DocComparator.Move) { Path field = d.getField(); if (field.numSegments() > 2) { Path parent = md.resolve(field.prefix(-1)).getFullPath(); if (inaccessibleFields.contains(parent)) { ret.add(parent); } } } } } return ret; } FieldAccessRoleEvaluator(EntityMetadata md, Set<String> callerRoles); boolean hasAccess(Set<Path> fields, Operation op); boolean hasAccess(Path field, Operation op); Set<Path> getInaccessibleFields(Operation op); Set<Path> getInaccessibleFields_Insert(JsonDoc doc); DocComparator.Difference<JsonNode> getLastDiff(); Set<Path> getInaccessibleFields_Update(JsonDoc newDoc, JsonDoc oldDoc); Projection getExcludedFields(Operation op); }
FieldAccessRoleEvaluator { public Set<Path> getInaccessibleFields_Update(JsonDoc newDoc, JsonDoc oldDoc) { if (comparator == null) { comparator = md.getEntitySchema().getDocComparator(); } Set<Path> inaccessibleFields = getInaccessibleFields(Operation.update); Set<Path> ret = new HashSet<>(); if (!inaccessibleFields.isEmpty()) { try { diff = comparator.compareNodes(oldDoc.getRoot(), newDoc.getRoot()); } catch (Exception e) { throw new RuntimeException(e); } for (DocComparator.Delta<JsonNode> d : diff.getDelta()) { if ((d instanceof DocComparator.Addition && ((DocComparator.Addition<JsonNode>) d).getAddedNode().isValueNode()) || (d instanceof DocComparator.Removal && ((DocComparator.Removal<JsonNode>) d).getRemovedNode().isValueNode()) || (d instanceof DocComparator.Modification && ((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode().isValueNode())) { FieldTreeNode fieldMd = md.resolve(d.getField()); boolean modified = true; if (d instanceof JsonCompare.Modification) { Object o1 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getUnmodifiedNode()); Object o2 = fieldMd.getType().fromJson(((DocComparator.Modification<JsonNode>) d).getModifiedNode()); if (o1.equals(o2)) { modified = false; } } if (modified && inaccessibleFields.contains(fieldMd.getFullPath())) { ret.add(d.getField()); } } if (d instanceof DocComparator.Addition || d instanceof DocComparator.Removal || d instanceof DocComparator.Move) { Path field = d.getField(); if (field.numSegments() > 2) { Path parent = md.resolve(field.prefix(-1)).getFullPath(); if (inaccessibleFields.contains(parent)) { ret.add(parent); } } } } } return ret; } FieldAccessRoleEvaluator(EntityMetadata md, Set<String> callerRoles); boolean hasAccess(Set<Path> fields, Operation op); boolean hasAccess(Path field, Operation op); Set<Path> getInaccessibleFields(Operation op); Set<Path> getInaccessibleFields_Insert(JsonDoc doc); DocComparator.Difference<JsonNode> getLastDiff(); Set<Path> getInaccessibleFields_Update(JsonDoc newDoc, JsonDoc oldDoc); Projection getExcludedFields(Operation op);  }