Search in sources :

Example 51 with SecurityContext

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

the class CreateRelationshipCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> properties = webSocketData.getRelData();
    final String sourceId = (String) properties.get("sourceId");
    final String targetId = (String) properties.get("targetId");
    final String relType = (String) properties.get("relType");
    final AbstractNode sourceNode = getNode(sourceId);
    final AbstractNode targetNode = getNode(targetId);
    if ((sourceNode != null) && (targetNode != null)) {
        final SecurityContext securityContext = getWebSocket().getSecurityContext();
        final App app = StructrApp.getInstance(securityContext);
        try {
            final Class relationClass = StructrApp.getConfiguration().getRelationClassForCombinedType(sourceNode.getType(), relType, targetNode.getType());
            final RelationshipInterface rel = app.create(sourceNode, targetNode, relationClass);
            TransactionCommand.registerRelCallback(rel, callback);
            if (rel != null) {
                webSocketData.setResult(Arrays.asList(rel));
                getWebSocket().send(webSocketData, true);
            }
        } catch (FrameworkException t) {
            getWebSocket().send(MessageBuilder.status().code(400).message(t.getMessage()).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(400).message("The CREATE_RELATIONSHIP command needs source and target!").build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) RelationshipInterface(org.structr.core.graph.RelationshipInterface)

Example 52 with SecurityContext

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

the class CreateSimplePage method processMessage.

// ~--- methods --------------------------------------------------------
@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String pageName = (String) webSocketData.getNodeData().get(Page.name.dbName());
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    try {
        final Page page = Page.createSimplePage(securityContext, pageName);
        TransactionCommand.registerNodeCallback(page, callback);
    } catch (FrameworkException fex) {
        logger.warn("Could not create node.", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.toString()).build(), true);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) Page(org.structr.web.entity.dom.Page)

Example 53 with SecurityContext

use of org.structr.common.SecurityContext 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 54 with SecurityContext

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

the class DeleteRelationshipCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final AbstractRelationship obj = getRelationship(webSocketData.getId());
    if (obj != null) {
        StructrApp.getInstance(securityContext).delete(obj);
    }
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) SecurityContext(org.structr.common.SecurityContext)

Example 55 with SecurityContext

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

the class FindDuplicateFilesCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final PropertyKey<String> path = StructrApp.key(AbstractFile.class, "path");
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(AbstractFile.class).sort(path);
    try {
        AbstractFile lastFile = null;
        String lastFilepath = null;
        boolean lastWasDupe = false;
        final ArrayList<GraphObject> filesWithSamePath = new ArrayList<>();
        final Iterator it = query.getAsList().iterator();
        while (it.hasNext()) {
            final AbstractNode node = (AbstractNode) it.next();
            try {
                final AbstractFile file = (AbstractFile) node;
                final String currentFilepath = file.getProperty(path);
                // skip the first file as we can not compare it to the previous one
                if (lastFile != null) {
                    if (currentFilepath.equals(lastFilepath)) {
                        if (!lastWasDupe) {
                            // if this is the first duplicate found we need to add both files
                            filesWithSamePath.add(lastFile);
                        }
                        filesWithSamePath.add(file);
                        lastWasDupe = true;
                    } else {
                        lastWasDupe = false;
                    }
                }
                lastFilepath = currentFilepath;
                lastFile = file;
            } catch (ClassCastException cce) {
                logger.warn("Tried casting node '{}' of type '{}' to AbstractFile. Most likely a node type inheriting from File was deleted and an instance remains. Please delete this node or change its type.", node.getUuid(), node.getType());
            }
        }
        // set full result list
        webSocketData.setResult(filesWithSamePath);
        webSocketData.setRawResultCount(filesWithSamePath.size());
        // send only over local connection
        getWebSocket().send(webSocketData, true);
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) ArrayList(java.util.ArrayList) GraphObject(org.structr.core.GraphObject) SecurityContext(org.structr.common.SecurityContext) Iterator(java.util.Iterator)

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