Search code examples
neo4jcypheraclintersectionpredicates

Neo4J : intersection and predicates


I'm working on a project involving Neo4J Db and I'm facing a problem I can't solve by myself.

We are dealing with Acl in a graph. Each Acl is linked to a set of metadata. Items are also linked to those metadata. A metadata grants access to one item when ALL the metadata linked to the item are also linked to the metadata.

Here is a picture of the graph model: ACL graph picture

In this example, ACL1 grants access to item1 (but not item2), and acl2 grants access to item2(but not item1).

I think the trick is to use the ALL predicate on the nodes linke to one item, but my tries always return all the items from the graph (because they are sharing one meta)

Can someone helps me to create the cypher query to returns for one user all the items he can access ?

Thanks for your help, Gregory

PS : here is a Neo4j console example dataset : http://console.neo4j.org/r/urjh64


Solution

  • Using the ALL predicate is the correct way here. How about this query:

    MATCH (:User { Login:'User1' })-[:IS_GRANTED]->(acl)-[:IS_APPLICABLE]->(meta)-[:IS_APPLICABLE]->(item)
    WITH item, collect(meta) AS userMetas
    MATCH (item)<-[:IS_APPLICABLE]-(meta2)
    WITH item, userMetas, collect(meta2) AS itemMetas
    WHERE ALL (x IN itemMetas   WHERE x IN userMetas)
    RETURN item
    

    First we navigate from the user to all items he potentially has access to. For the candidates we go back and collect all metas. In the WHERE we make sure that all of the metas of the item are also available for the user.