Search in sources :

Example 1 with DynamicResourceAccess

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");
    }
}
Also used : ResourceAccess(org.structr.core.entity.ResourceAccess) DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) GenericNode(org.structr.core.entity.GenericNode) IOException(java.io.IOException) PropertyMap(org.structr.core.property.PropertyMap) NodeInterface(org.structr.core.graph.NodeInterface) Location(org.structr.core.entity.Location) NodeHasLocation(org.structr.core.entity.relationship.NodeHasLocation) Test(org.junit.Test)

Example 2 with DynamicResourceAccess

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;
}
Also used : DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) ResourceAccess(org.structr.core.entity.ResourceAccess) DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) NodeAttribute(org.structr.core.graph.NodeAttribute) LinkedList(java.util.LinkedList)

Example 3 with DynamicResourceAccess

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);
    }
}
Also used : DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) SchemaNode(org.structr.core.entity.SchemaNode) NotFoundException(org.structr.api.NotFoundException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with DynamicResourceAccess

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);
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess)

Aggregations

DynamicResourceAccess (org.structr.core.entity.DynamicResourceAccess)4 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 ResourceAccess (org.structr.core.entity.ResourceAccess)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 NotFoundException (org.structr.api.NotFoundException)1 FrameworkException (org.structr.common.error.FrameworkException)1 AbstractNode (org.structr.core.entity.AbstractNode)1 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)1 GenericNode (org.structr.core.entity.GenericNode)1 Location (org.structr.core.entity.Location)1 SchemaNode (org.structr.core.entity.SchemaNode)1 NodeHasLocation (org.structr.core.entity.relationship.NodeHasLocation)1 NodeAttribute (org.structr.core.graph.NodeAttribute)1 NodeInterface (org.structr.core.graph.NodeInterface)1 Tx (org.structr.core.graph.Tx)1