use of org.structr.core.entity.DynamicResourceAccess in project structr by structr.
the class BasicTest method test04CheckNodeEntities.
/**
* Create a node for each configured entity class and check the type
*/
@Test
public void test04CheckNodeEntities() {
AccessControlTest.clearResourceAccess();
final PropertyMap props = new PropertyMap();
try (final Tx tx = app.tx()) {
List<Class> entityList = Collections.EMPTY_LIST;
try {
entityList = getClasses("org.structr.core.entity");
} catch (IOException | ClassNotFoundException ex) {
logger.error("", ex);
}
assertTrue(entityList.contains(AbstractNode.class));
assertTrue(entityList.contains(GenericNode.class));
assertTrue(entityList.contains(Location.class));
assertTrue(entityList.contains(ResourceAccess.class));
// Don't test these, it would fail due to violated constraints
entityList.remove(TestTwo.class);
entityList.remove(TestNine.class);
entityList.remove(SchemaNode.class);
entityList.remove(SchemaRelationshipNode.class);
for (Class type : entityList) {
// Class entityClass = entity.getValue();
if (AbstractNode.class.isAssignableFrom(type)) {
props.clear();
// For Group, fill mandatory fields
if (type.equals(Group.class)) {
props.put(Group.name, "Group-0");
}
// For TestSeven, fill mandatory fields
if (type.equals(TestSeven.class)) {
props.put(TestSeven.name, "TestSeven-0");
}
// For ResourceAccess, fill mandatory fields
if (type.equals(ResourceAccess.class)) {
props.put(ResourceAccess.signature, "/X");
props.put(ResourceAccess.flags, 6L);
}
// For DynamicResourceAccess, fill mandatory fields
if (type.equals(DynamicResourceAccess.class)) {
props.put(DynamicResourceAccess.signature, "/Y");
props.put(DynamicResourceAccess.flags, 6L);
}
// For Localization, fill mandatory fields
if (type.equals(Localization.class)) {
/*
props.put(Localization.name, "localizationKey");
props.put(Localization.locale, "de_DE");
*/
}
// For Location, set coordinates
if (type.equals(Location.class)) {
props.put(StructrApp.key(Location.class, "latitude"), 12.34);
props.put(StructrApp.key(Location.class, "longitude"), 56.78);
}
logger.info("Creating node of type {}", type);
NodeInterface node = app.create(type, props);
assertTrue(type.getSimpleName().equals(node.getProperty(AbstractNode.type)));
// Remove mandatory fields for ResourceAccess from props map
if (type.equals(ResourceAccess.class)) {
props.remove(ResourceAccess.signature);
props.remove(ResourceAccess.flags);
}
}
}
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.DynamicResourceAccess in project structr by structr.
the class SchemaHelper method createDynamicGrants.
public static List<DynamicResourceAccess> createDynamicGrants(final String signature) {
final List<DynamicResourceAccess> grants = new LinkedList<>();
final long initialFlagsValue = 0;
final App app = StructrApp.getInstance();
try {
ResourceAccess grant = app.nodeQuery(ResourceAccess.class).and(ResourceAccess.signature, signature).getFirst();
if (grant == null) {
// create new grant
grants.add(app.create(DynamicResourceAccess.class, new NodeAttribute(DynamicResourceAccess.signature, signature), new NodeAttribute(DynamicResourceAccess.flags, initialFlagsValue)));
logger.debug("New signature created: {}", new Object[] { (signature) });
}
final String schemaSig = schemaResourceSignature(signature);
ResourceAccess schemaGrant = app.nodeQuery(ResourceAccess.class).and(ResourceAccess.signature, schemaSig).getFirst();
if (schemaGrant == null) {
// create additional grant for the _schema resource
grants.add(app.create(DynamicResourceAccess.class, new NodeAttribute(DynamicResourceAccess.signature, schemaSig), new NodeAttribute(DynamicResourceAccess.flags, initialFlagsValue)));
logger.debug("New signature created: {}", new Object[] { schemaSig });
}
final String uiSig = uiViewResourceSignature(signature);
ResourceAccess uiViewGrant = app.nodeQuery(ResourceAccess.class).and(ResourceAccess.signature, uiSig).getFirst();
if (uiViewGrant == null) {
// create additional grant for the Ui view
grants.add(app.create(DynamicResourceAccess.class, new NodeAttribute(DynamicResourceAccess.signature, uiSig), new NodeAttribute(DynamicResourceAccess.flags, initialFlagsValue)));
logger.debug("New signature created: {}", new Object[] { uiSig });
}
} catch (Throwable t) {
logger.warn("", t);
}
return grants;
}
use of org.structr.core.entity.DynamicResourceAccess in project structr by structr.
the class SchemaHelper method cleanUnusedDynamicGrants.
public static void cleanUnusedDynamicGrants(final List<SchemaNode> existingSchemaNodes) {
try {
final List<DynamicResourceAccess> existingDynamicGrants = StructrApp.getInstance().nodeQuery(DynamicResourceAccess.class).getAsList();
final Set<String> existingSchemaNodeNames = new HashSet<>();
for (final SchemaNode schemaNode : existingSchemaNodes) {
existingSchemaNodeNames.add(schemaNode.getResourceSignature());
}
for (final DynamicResourceAccess grant : existingDynamicGrants) {
boolean foundAllParts = true;
final String sig;
try {
sig = grant.getResourceSignature();
} catch (NotFoundException nfe) {
logger.debug("Unable to get signature from grant");
continue;
}
// Try to find schema nodes for all parts of the grant signature
final String[] parts = StringUtils.split(sig, "/");
if (parts != null) {
for (final String sigPart : parts) {
if ("/".equals(sigPart) || sigPart.startsWith("_")) {
continue;
}
// If one of the signature parts doesn't have an equivalent existing schema node, remove it
foundAllParts &= existingSchemaNodeNames.contains(sigPart);
}
}
if (!foundAllParts) {
logger.info("Did not find all parts of signature, will be removed: {}, ", new Object[] { sig });
removeDynamicGrants(sig);
}
}
} catch (Throwable t) {
logger.warn("", t);
}
}
use of org.structr.core.entity.DynamicResourceAccess in project structr by structr.
the class SchemaHelper method removeDynamicGrants.
public static void removeDynamicGrants(final String signature) {
final App app = StructrApp.getInstance();
try {
// delete grant
DynamicResourceAccess grant = app.nodeQuery(DynamicResourceAccess.class).and(DynamicResourceAccess.signature, signature).getFirst();
if (grant != null) {
app.delete(grant);
}
// delete grant
DynamicResourceAccess schemaGrant = app.nodeQuery(DynamicResourceAccess.class).and(DynamicResourceAccess.signature, "_schema/" + signature).getFirst();
if (schemaGrant != null) {
app.delete(schemaGrant);
}
// delete grant
DynamicResourceAccess viewGrant = app.nodeQuery(DynamicResourceAccess.class).and(DynamicResourceAccess.signature, signature + "/_Ui").getFirst();
if (viewGrant != null) {
app.delete(viewGrant);
}
} catch (Throwable t) {
logger.warn("", t);
}
}
Aggregations