Search in sources :

Example 1 with LinkedTreeNode

use of org.structr.core.entity.LinkedTreeNode in project structr by structr.

the class DeleteNodeCommand method deleteNode.

protected static void deleteNode(final StructrWebSocket ws, final NodeInterface obj, final Boolean recursive) {
    final SecurityContext securityContext = ws.getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        if (!(obj.isGranted(Permission.delete, securityContext))) {
            logger.warn("No delete permission for {} on {}", new Object[] { ws.getCurrentUser().toString(), obj.toString() });
            ws.send(MessageBuilder.status().message("No delete permission").code(400).build(), true);
            tx.success();
            return;
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
    }
    if (Boolean.TRUE.equals(recursive)) {
        // Remove all child nodes first
        try {
            final List<NodeInterface> filteredResults = new LinkedList<>();
            if (obj instanceof DOMNode) {
                DOMNode node = (DOMNode) obj;
                filteredResults.addAll(DOMNode.getAllChildNodes(node));
            } else if (obj instanceof LinkedTreeNode) {
                LinkedTreeNode node = (LinkedTreeNode) obj;
                filteredResults.addAll(node.getAllChildNodes());
            }
            for (NodeInterface node : filteredResults) {
                app.delete(node);
            }
        } catch (FrameworkException fex) {
            logger.warn("Exception occured", fex);
            ws.send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
        } catch (DOMException dex) {
            logger.warn("DOMException occured.", dex);
            ws.send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
        }
    }
    try {
        app.delete(obj);
    } catch (FrameworkException fex) {
        logger.warn("Unable to delete node(s)", fex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) DOMException(org.w3c.dom.DOMException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedTreeNode(org.structr.core.entity.LinkedTreeNode) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 2 with LinkedTreeNode

use of org.structr.core.entity.LinkedTreeNode in project structr by structr.

the class SetPermissionCommand method setPermission.

private void setPermission(final Value<Tx> transaction, final App app, final AbstractNode obj, final Principal principal, final String action, final Permission permission, final boolean rec) throws FrameworkException {
    // create new transaction if not already present
    Tx tx = transaction.get(null);
    if (tx == null) {
        tx = app.tx();
        transaction.set(null, tx);
    }
    switch(action) {
        case "grant":
            obj.grant(permission, principal);
            break;
        case "revoke":
            obj.revoke(permission, principal);
            break;
    }
    sum++;
    // commit transaction after 100 nodes
    if (++count == 100) {
        logger.info("Committing transaction after {} objects..", sum);
        count = 0;
        // commit and close old transaction
        tx.success();
        tx.close();
        // create new transaction, do not notify Ui
        tx = app.tx(true, true, false);
    }
    if (rec && obj instanceof LinkedTreeNode) {
        for (final Object t : ((LinkedTreeNode) obj).treeGetChildren()) {
            setPermission(transaction, app, (AbstractNode) t, principal, action, permission, rec);
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) LinkedTreeNode(org.structr.core.entity.LinkedTreeNode)

Aggregations

LinkedTreeNode (org.structr.core.entity.LinkedTreeNode)2 Tx (org.structr.core.graph.Tx)2 LinkedList (java.util.LinkedList)1 SecurityContext (org.structr.common.SecurityContext)1 FrameworkException (org.structr.common.error.FrameworkException)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 NodeInterface (org.structr.core.graph.NodeInterface)1 DOMNode (org.structr.web.entity.dom.DOMNode)1 DOMException (org.w3c.dom.DOMException)1