Search in sources :

Example 1 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class DeploymentTest method test21ExportGrants.

@Test
public void test21ExportGrants() {
    Principal user1 = null;
    Principal user2 = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
        user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception.");
    }
    Assert.assertNotNull("User was not created, test cannot continue", user1);
    Assert.assertNotNull("User was not created, test cannot continue", user2);
    // setup
    final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
    final App app1 = StructrApp.getInstance(context1);
    try (final Tx tx = app1.tx()) {
        final Page page = Page.createNewPage(context1, "test21");
        final Html html = createElement(page, page, "html");
        final Head head = createElement(page, html, "head");
        createElement(page, head, "title", "test21");
        final Body body = createElement(page, html, "body");
        final Div div1 = createElement(page, body, "div");
        final Content content = createContent(page, div1, "<b>Test</b>");
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
        // create grants
        page.grant(Permission.read, user2);
        div1.grant(Permission.read, user2);
        content.grant(Permission.read, user2);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), true, false);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) NodeAttribute(org.structr.core.graph.NodeAttribute) Head(org.structr.web.entity.html.Head) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Page(org.structr.web.entity.dom.Page) Div(org.structr.web.entity.html.Div) Content(org.structr.web.entity.dom.Content) SecurityContext(org.structr.common.SecurityContext) Body(org.structr.web.entity.html.Body) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class DeploymentTest method test20ExportOwnership.

@Test
public void test20ExportOwnership() {
    Principal user1 = null;
    Principal user2 = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
        user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception.");
    }
    Assert.assertNotNull("User was not created, test cannot continue", user1);
    Assert.assertNotNull("User was not created, test cannot continue", user2);
    // setup
    final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
    final App app1 = StructrApp.getInstance(context1);
    try (final Tx tx = app1.tx()) {
        final Page page = Page.createNewPage(context1, "test20");
        final Html html = createElement(page, page, "html");
        final Head head = createElement(page, html, "head");
        createElement(page, head, "title", "test20");
        final Body body = createElement(page, html, "body");
        final Div div1 = createElement(page, body, "div");
        final Content content = createContent(page, div1, "<b>Test</b>");
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
        // set owner to different user
        div1.setProperty(AbstractNode.owner, user2);
        content.setProperty(AbstractNode.owner, user2);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), true, false);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) NodeAttribute(org.structr.core.graph.NodeAttribute) Head(org.structr.web.entity.html.Head) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Page(org.structr.web.entity.dom.Page) Div(org.structr.web.entity.html.Div) Content(org.structr.web.entity.dom.Content) SecurityContext(org.structr.common.SecurityContext) Body(org.structr.web.entity.html.Body) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class Factory method page.

protected Result page(final QueryResult<S> input, final int offset, final int pageSize) throws FrameworkException {
    final SecurityContext securityContext = factoryProfile.getSecurityContext();
    final boolean dontCheckCount = securityContext.ignoreResultCount();
    final List<T> nodes = new ArrayList<>();
    int overallCount = 0;
    int position = 0;
    int count = 0;
    try (final QueryResult<S> tmp = input) {
        for (final S item : tmp) {
            final T n = instantiate(item);
            if (n != null) {
                overallCount++;
                position++;
                if (disablePaging || (position > offset && position <= offset + pageSize)) {
                    nodes.add(n);
                    // stop if we got enough nodes
                    if (++count == pageSize && dontCheckCount && !disablePaging) {
                        break;
                    }
                }
            }
        }
    } catch (NetworkException nex) {
        throw new FrameworkException(503, nex.getMessage());
    }
    // The overall count may be inaccurate
    return new Result(nodes, overallCount, true, false);
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) ArrayList(java.util.ArrayList) NetworkException(org.structr.api.NetworkException) QueryResult(org.structr.api.QueryResult) Result(org.structr.core.Result)

Example 4 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class BulkChangeNodePropertyKeyCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final NodeFactory nodeFactory = new NodeFactory(superUserContext);
    String type = null;
    final String oldKey = (String) properties.get("oldKey");
    final String newKey = (String) properties.get("newKey");
    if (graphDb != null && StringUtils.isNotBlank(oldKey) && StringUtils.isNotBlank(newKey)) {
        Iterator<AbstractNode> nodeIterator = null;
        if (properties.containsKey(AbstractNode.type.dbName())) {
            type = (String) properties.get(AbstractNode.type.dbName());
            nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByLabel(type)).iterator();
            properties.remove(AbstractNode.type.dbName());
        } else {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
        }
        final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "ChangeNodePropertyKey", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
                for (Entry entry : properties.entrySet()) {
                    String key = (String) entry.getKey();
                    PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(node.getClass(), key);
                    if (propertyKey != null) {
                        Node dbNode = node.getNode();
                        if (dbNode.hasProperty(newKey)) {
                            logger.error("Node {} has already a property with key {}", new Object[] { node, newKey });
                            throw new IllegalStateException("Node has already a property of the new key");
                        }
                        if (dbNode.hasProperty(oldKey)) {
                            dbNode.setProperty(newKey, dbNode.getProperty(oldKey));
                            dbNode.removeProperty(oldKey);
                        }
                        node.updateInIndex();
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
                logger.warn("Unable to set properties of node {}: {}", new Object[] { node.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set node properties: {}", t.getMessage());
            }
        });
        logger.info("Fixed {} nodes ...", count);
    } else {
        logger.info("No values for oldKey and/or newKey found, aborting.");
    }
    logger.info("Done");
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) DatabaseService(org.structr.api.DatabaseService) Entry(java.util.Map.Entry) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 5 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class BulkCopyRelationshipPropertyCommand method execute.

@Override
public void execute(final Map<String, Object> map) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final RelationshipFactory relFactory = new RelationshipFactory(securityContext);
    final String sourceKey = (String) map.get("sourceKey");
    final String destKey = (String) map.get("destKey");
    if (sourceKey == null || destKey == null) {
        throw new IllegalArgumentException("This command requires one argument of type Map. Map must contain values for 'sourceKey' and 'destKey'.");
    }
    if (graphDb != null) {
        final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "CopyRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                // Treat only "our" rels
                if (rel.getProperty(GraphObject.id) != null) {
                    Class type = rel.getClass();
                    PropertyKey destPropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, destKey);
                    PropertyKey sourcePropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, sourceKey);
                    try {
                        // copy properties
                        rel.setProperty(destPropertyKey, rel.getProperty(sourcePropertyKey));
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to copy relationship property {} of relationship {} to {}: {}", new Object[] { sourcePropertyKey, rel.getUuid(), destPropertyKey, fex.getMessage() });
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
                logger.warn("Unable to copy relationship properties of relationship {}: {}", new Object[] { rel.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to copy relationship properties: {}", t.getMessage());
            }
        });
        logger.info("Finished setting properties on {} nodes", count);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

SecurityContext (org.structr.common.SecurityContext)131 FrameworkException (org.structr.common.error.FrameworkException)76 App (org.structr.core.app.App)56 StructrApp (org.structr.core.app.StructrApp)56 Tx (org.structr.core.graph.Tx)36 GraphObject (org.structr.core.GraphObject)35 PropertyKey (org.structr.core.property.PropertyKey)26 PropertyMap (org.structr.core.property.PropertyMap)26 AbstractNode (org.structr.core.entity.AbstractNode)19 IOException (java.io.IOException)18 Map (java.util.Map)17 File (org.structr.web.entity.File)14 LinkedList (java.util.LinkedList)13 DatabaseService (org.structr.api.DatabaseService)12 DOMNode (org.structr.web.entity.dom.DOMNode)12 Result (org.structr.core.Result)11 PropertyConverter (org.structr.core.converter.PropertyConverter)11 GraphObjectMap (org.structr.core.GraphObjectMap)10 Query (org.structr.core.app.Query)10 Principal (org.structr.core.entity.Principal)10