Class: GraphStarter::QueryAuthorizer

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

Instance Method Summary collapse

Constructor Details

#initialize(query_object) ⇒ QueryAuthorizer

Can take:

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


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

def initialize(query_object)
  validate_query_object!(query_object)

  @query_object = query_object
end

Instance Method Details

#authorized_pluck(variable, user) ⇒ Object



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

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

#authorized_query(variables, user) ⇒ Object



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

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