Class: CodeToQuery::Policies::PunditAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/code_to_query/policies/pundit_adapter.rb

Instance Method Summary collapse

Instance Method Details

#call(current_user, table:, intent: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/code_to_query/policies/pundit_adapter.rb', line 11

def call(current_user, table:, intent: nil) # rubocop:disable Lint/UnusedMethodArgument
  return {} unless defined?(Pundit)

  info = {
    enforced_predicates: inferred_tenant_predicates(current_user, table),
    allowed_tables: [],
    allowed_columns: {}
  }

  model = infer_model_for_table(table)
  if model
    begin
      Pundit.policy_scope!(current_user, model)
      info[:allowed_tables] << table
    rescue StandardError
    end

    policy = Pundit.policy(current_user, model)
    if policy.respond_to?(:permitted_columns)
      cols = Array(policy.permitted_columns).map(&:to_s)
      info[:allowed_columns][table] = cols if cols.any?
    end
  end

  info
end