Skip to content

Commit

Permalink
Merge pull request #462 from dcrissman/issue458
Browse files Browse the repository at this point in the history
fixes #458 - change all access levels to anyone
  • Loading branch information
paterczm committed Sep 15, 2015
2 parents 5350322 + fbcfae2 commit cc3fff2
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

import org.junit.AfterClass;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.redhat.lightblue.Request;
import com.redhat.lightblue.config.DataSourcesConfiguration;
import com.redhat.lightblue.config.JsonTranslator;
Expand Down Expand Up @@ -104,12 +106,54 @@ public AbstractCRUDTestController(boolean loadStatically) throws Exception {
JsonNode[] metadataNodes = getMetadataJsonNodes();
if (metadataNodes != null) {
for (JsonNode metadataJson : metadataNodes) {
if (isGrantAnyoneAccess()) {
grantAnyoneAccess(metadataJson);
}
metadata.createNewMetadata(tx.parse(EntityMetadata.class, metadataJson));
}
}
}
}

/**
* Deep dives to set all access levels to 'anyone'.
* @param node - root {@link JsonNode}
*/
public static void grantAnyoneAccess(JsonNode node) {
doGrantAnyoneAccess(node.get("schema"));
}

/**
* <p>Recursive Method!!</p>
* <p>Iterates over the {@link JsonNode} to determine if it, or any of it's
* sub-documents, has an access node. If so, all access settings will be changed to
* 'anyone'</p>
* @param node - {@link JsonNode} to set the access to anyone on.
*/
private static void doGrantAnyoneAccess(JsonNode node) {
if (node.has("fields")) {
JsonNode fieldsNode = node.get("fields");
Iterator<JsonNode> fieldNodes = fieldsNode.iterator();
while (fieldNodes.hasNext()) {
doGrantAnyoneAccess(fieldNodes.next());
}
}

if (node.has("items")) {
doGrantAnyoneAccess(node.get("items"));
}

if (node.has("access")) {
JsonNode accessNode = node.get("access");
Iterator<JsonNode> accessNodes = accessNode.iterator();
while (accessNodes.hasNext()) {
ArrayNode child = (ArrayNode) accessNodes.next();
child.removeAll();
child.add("anyone");
}
}
}

/**
* Creates and returns an instance of {@link JsonNode} that represents the
* relevant lightblue-crud.json. If not set by a subclass, then the default settings will be used.
Expand Down Expand Up @@ -149,6 +193,14 @@ protected JsonNode getLightblueMetadataJson() throws Exception {
*/
protected abstract JsonNode[] getMetadataJsonNodes() throws Exception;

/**
* @return <code>true</code> if access settings on metadata should be altered to
* 'anyone', otherwise <code>false</code>. Defaults to <code>true</code>.
*/
public boolean isGrantAnyoneAccess() {
return true;
}

/**
* Creates and returns a {@link Request} based on the passed in
* <code>jsonFile</code>.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.redhat.lightblue.test;

import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadJsonNode;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;

public class AbstractCRUDTestControllerTest {

@Test
public void testGrantAnyoneAccess() throws Exception {
JsonNode node = loadJsonNode("./metadata/access.json");
AbstractCRUDTestController.grantAnyoneAccess(node);

assertEquals("anyone",
node.get("schema").get("access").get("insert").get(0).textValue());
assertEquals("anyone",
node.get("schema").get("fields").get("anArray").get("access").get("insert").get(0).textValue());
assertEquals("anyone",
node.get("schema").get("fields").get("anArray").get("items").get("fields").get("name").get("access").get("insert").get(0).textValue());
assertEquals("anyone",
node.get("schema").get("fields").get("someField").get("access").get("insert").get(0).textValue());
}

}
76 changes: 76 additions & 0 deletions test/src/test/resources/metadata/access.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"schema": {
"access": {
"insert": [
"insert-perm"
],
"update": [
"update-perm"
],
"find": [
"find-perm"
],
"delete": [
"delete-perm"
]
},
"fields": {
"anArray": {
"type": "array",
"items": {
"type": "object",
"fields": {
"name": {
"type": "string",
"access": {
"insert": [
"insert-perm"
],
"update": [
"update-perm"
],
"find": [
"find-perm"
],
"delete": [
"delete-perm"
]
}
}
}
},
"access": {
"insert": [
"insert-perm"
],
"update": [
"update-perm"
],
"find": [
"find-perm"
],
"delete": [
"delete-perm"
]
}
},
"someField": {
"type": "string",
"access": {
"insert": [
"insert-perm"
],
"update": [
"update-perm"
],
"find": [
"find-perm"
],
"delete": [
"delete-perm"
]
}
}
}
}
}

0 comments on commit cc3fff2

Please sign in to comment.