Search in sources :

Example 71 with SecurityContext

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

the class ClearDatabase method execute.

// ~--- methods --------------------------------------------------------
public void execute() throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final NodeFactory nodeFactory = new NodeFactory(securityContext);
    if (graphDb != null) {
        Iterator<NodeInterface> nodeIterator = null;
        final App app = StructrApp.getInstance();
        try (final Tx tx = app.tx()) {
            nodeIterator = app.nodeQuery(NodeInterface.class).getAsList().iterator();
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("Exception while creating all nodes iterator.", fex);
        }
        final long deletedNodes = bulkGraphOperation(securityContext, nodeIterator, 1000, "ClearDatabase", new BulkGraphOperation<NodeInterface>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, NodeInterface node) {
                // Delete only "our" nodes
                if (node.getProperty(GraphObject.id) != null) {
                    try {
                        app.delete(node);
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to delete node {}: {}", new Object[] { node.getUuid(), fex.getMessage() });
                    }
                }
            }

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

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to clear database: {}", t.getMessage());
            }
        });
        logger.info("Finished deleting {} nodes", deletedNodes);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject)

Example 72 with SecurityContext

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

the class SearchFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (sources == null) {
            throw new IllegalArgumentException();
        }
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final Query query = StructrApp.getInstance(securityContext).nodeQuery();
        applyRange(securityContext, query);
        Class type = null;
        if (sources.length >= 1 && sources[0] != null) {
            final String typeString = sources[0].toString();
            type = config.getNodeEntityClass(typeString);
            if (type != null) {
                query.andTypes(type);
            } else {
                logger.warn("Error in search(): type {} not found.", typeString);
                return "Error in search(): type " + typeString + " not found.";
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in search(): no type specified. Parameters: {}", getParametersAsString(sources));
            return "Error in search(): no type specified.";
        }
        // experimental: disable result count, prevents instantiation
        // of large collections just for counting all the objects..
        securityContext.ignoreResultCount(true);
        // extension for native javascript objects
        if (sources.length == 2 && sources[1] instanceof Map) {
            final PropertyMap map = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
            for (final Map.Entry<PropertyKey, Object> entry : map.entrySet()) {
                query.and(entry.getKey(), entry.getValue(), false);
            }
        } else {
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_SEARCH);
            }
            for (int c = 1; c < parameter_count; c += 2) {
                final PropertyKey key = StructrApp.key(type, sources[c].toString());
                if (key != null) {
                    final PropertyConverter inputConverter = key.inputConverter(securityContext);
                    Object value = sources[c + 1];
                    if (inputConverter != null) {
                        value = inputConverter.convert(value);
                    }
                    query.and(key, value, false);
                }
            }
        }
        // return search results
        return query.getAsList();
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    } finally {
        resetRange();
    }
}
Also used : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) PropertyMap(org.structr.core.property.PropertyMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) PropertyKey(org.structr.core.property.PropertyKey)

Example 73 with SecurityContext

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

the class SetPrivilegedFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    synchronized (ctx) {
        final SecurityContext previousSecurityContext = ctx.getSecurityContext();
        ctx.setSecurityContext(SecurityContext.getSuperUserInstance());
        try {
            final SetFunction set = new SetFunction();
            set.apply(ctx, caller, sources);
        } finally {
            ctx.setSecurityContext(previousSecurityContext);
        }
    }
    return "";
}
Also used : SecurityContext(org.structr.common.SecurityContext)

Example 74 with SecurityContext

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

the class NodeFactory method instantiateWithType.

@Override
public T instantiateWithType(final Node node, final Class<T> nodeClass, final Relationship pathSegment, boolean isCreation) {
    // cannot instantiate node without type
    if (nodeClass == null) {
        return null;
    }
    SecurityContext securityContext = factoryProfile.getSecurityContext();
    T newNode = null;
    try {
        newNode = nodeClass.newInstance();
    } catch (NoClassDefFoundError | InstantiationException | IllegalAccessException itex) {
        newNode = null;
    }
    if (newNode == null) {
        newNode = (T) factoryDefinition.createGenericNode();
    }
    newNode.init(factoryProfile.getSecurityContext(), node, nodeClass, isCreation);
    newNode.setRawPathSegment(pathSegment);
    newNode.onNodeInstantiation(isCreation);
    // check access
    if (isCreation || securityContext.isReadable(newNode, factoryProfile.includeDeletedAndHidden(), factoryProfile.publicOnly())) {
        return newNode;
    }
    return null;
}
Also used : SecurityContext(org.structr.common.SecurityContext)

Example 75 with SecurityContext

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

the class DataFeed method updateFeed.

static void updateFeed(final DataFeed thisFeed, final boolean cleanUp) {
    final String remoteUrl = thisFeed.getUrl();
    if (StringUtils.isNotBlank(remoteUrl)) {
        final SecurityContext securityContext = thisFeed.getSecurityContext();
        final App app = StructrApp.getInstance(securityContext);
        try {
            final PropertyKey<Date> dateKey = StructrApp.key(FeedItem.class, "pubDate");
            final PropertyKey<String> urlKey = StructrApp.key(FeedItem.class, "url");
            final URL remote = new URL(remoteUrl);
            final SyndFeedInput input = new SyndFeedInput();
            try (final Reader reader = new XmlReader(remote)) {
                final SyndFeed feed = input.build(reader);
                final List<SyndEntry> entries = feed.getEntries();
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "feedType"), feed.getFeedType());
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "description"), feed.getDescription());
                final List<FeedItem> newItems = Iterables.toList(thisFeed.getItems());
                for (final SyndEntry entry : entries) {
                    final PropertyMap props = new PropertyMap();
                    final String link = entry.getLink();
                    // Check if item with this link already exists
                    if (app.nodeQuery(FeedItem.class).and(urlKey, link).getFirst() == null) {
                        props.put(urlKey, entry.getLink());
                        props.put(StructrApp.key(FeedItem.class, "name"), entry.getTitle());
                        props.put(StructrApp.key(FeedItem.class, "author"), entry.getAuthor());
                        props.put(StructrApp.key(FeedItem.class, "comments"), entry.getComments());
                        props.put(StructrApp.key(FeedItem.class, "description"), entry.getDescription().getValue());
                        final FeedItem item = app.create(FeedItem.class, props);
                        item.setProperty(dateKey, entry.getPublishedDate());
                        final List<FeedItemContent> itemContents = new LinkedList<>();
                        final List<FeedItemEnclosure> itemEnclosures = new LinkedList<>();
                        // Get and add all contents
                        final List<SyndContent> contents = entry.getContents();
                        for (final SyndContent content : contents) {
                            final FeedItemContent itemContent = app.create(FeedItemContent.class);
                            itemContent.setValue(content.getValue());
                            itemContents.add(itemContent);
                        }
                        // Get and add all enclosures
                        final List<SyndEnclosure> enclosures = entry.getEnclosures();
                        for (final SyndEnclosure enclosure : enclosures) {
                            final FeedItemEnclosure itemEnclosure = app.create(FeedItemEnclosure.class);
                            itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "url"), enclosure.getUrl());
                            itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "enclosureLength"), enclosure.getLength());
                            itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "enclosureType"), enclosure.getType());
                            itemEnclosures.add(itemEnclosure);
                        }
                        item.setProperty(StructrApp.key(FeedItem.class, "contents"), itemContents);
                        item.setProperty(StructrApp.key(FeedItem.class, "enclosures"), itemEnclosures);
                        newItems.add(item);
                        logger.debug("Created new item: {} ({}) ", item.getProperty(FeedItem.name), item.getProperty(dateKey));
                    }
                }
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "items"), newItems);
                thisFeed.setProperty(StructrApp.key(DataFeed.class, "lastUpdated"), new Date());
            }
        } catch (IllegalArgumentException | IOException | FeedException | FrameworkException ex) {
            logger.error("Error while updating feed", ex);
        }
    }
    if (cleanUp) {
        thisFeed.cleanUp();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) XmlReader(com.rometools.rome.io.XmlReader) Reader(java.io.Reader) URL(java.net.URL) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEnclosure(com.rometools.rome.feed.synd.SyndEnclosure) FrameworkException(org.structr.common.error.FrameworkException) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) XmlReader(com.rometools.rome.io.XmlReader) IOException(java.io.IOException) Date(java.util.Date) LinkedList(java.util.LinkedList) PropertyMap(org.structr.core.property.PropertyMap) SyndContent(com.rometools.rome.feed.synd.SyndContent) SecurityContext(org.structr.common.SecurityContext)

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