Class: Ibrain::Policies::BasePolicy

Inherits:
Object
  • Object
show all
Defined in:
app/graphql/ibrain/policies/base_policy.rb

Direct Known Subclasses

GraphqlPolicy

Class Method Summary collapse

Class Method Details

.guard(type, field) ⇒ Object



41
42
43
# File 'app/graphql/ibrain/policies/base_policy.rb', line 41

def guard(type, field)
  rules.dig(type.name, field, :guard)
end

.has_permission?(current_user, resource) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'app/graphql/ibrain/policies/base_policy.rb', line 34

def has_permission?(current_user, resource)
  return false if current_user.blank?
  return true if current_user.super_admin?

  current_user.try(:scope).to_s.split(',').include?(resource)
end

.mutation_rulesObject



15
16
17
18
19
20
21
# File 'app/graphql/ibrain/policies/base_policy.rb', line 15

def mutation_rules
  {
    '*': {
      guard: ->(_obj, _args, _ctx) { false }
    }
  }
end

.not_authorized_handler(type, field) ⇒ Object



45
46
47
# File 'app/graphql/ibrain/policies/base_policy.rb', line 45

def not_authorized_handler(type, field)
  rules.dig(type, field, :not_authorized) || rules.dig(type, :*, :not_authorized)
end

.query_rulesObject



7
8
9
10
11
12
13
# File 'app/graphql/ibrain/policies/base_policy.rb', line 7

def query_rules
  {
    '*': {
      guard: ->(_obj, _args, _ctx) { false }
    }
  }
end

.rolesObject



30
31
32
# File 'app/graphql/ibrain/policies/base_policy.rb', line 30

def roles
  Ibrain.user_class.roles.keys
end

.rulesObject



23
24
25
26
27
28
# File 'app/graphql/ibrain/policies/base_policy.rb', line 23

def rules
  {
    'Types::QueryType' => query_rules,
    'Types::MutationType' => mutation_rules
  }.freeze
end