Class: GraphStarter::QueryAuthorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/graph_starter/query_authorizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_object, filter = nil) ⇒ QueryAuthorizer

Can take:

* a Query
* a Proxy object
* Anything that responds to #query where a `Query` is returned


7
8
9
10
11
12
# File 'lib/graph_starter/query_authorizer.rb', line 7

def initialize(query_object, filter = nil)
  validate_query_object!(query_object)

  @query_object = query_object
  @filter = filter
end

Class Method Details

.queryish?(query_object) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/graph_starter/query_authorizer.rb', line 42

def self.queryish?(query_object)
  query_object.is_a?(::Neo4j::Core::Query) ||
    # Working around these two classes for new.  They should return `true`
    # for `respond_to(:query)`
    query_object.is_a?(::Neo4j::ActiveNode::HasN::AssociationProxy) ||
    query_object.is_a?(::Neo4j::ActiveNode::Query::QueryProxy) ||
    query_object.respond_to?(:query)
end

Instance Method Details

#authorized_pluck(variable, user) ⇒ Object



14
15
16
# File 'lib/graph_starter/query_authorizer.rb', line 14

def authorized_pluck(variable, user)
  authorized_query(variable, user).pluck(variable)
end

#authorized_query(variables, user) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graph_starter/query_authorizer.rb', line 18

def authorized_query(variables, user)
  variables = Array(variables)

  result_query = query.with(*variables)

  result_query = authorized_user_query(result_query, user, variables)

  # Collapse 2D array of all possible levels into one column of levels
  result_query
    .unwind(level_collection: :level_collections)
    .unwind(level: :level_collection).break
    .with(:level, *variables).where_not(level: nil)
    .with('collect(level) AS levels', *variables)
    .with("CASE WHEN 'write' IN levels THEN 'write' ELSE 'read' END AS level", *variables)
end