Sha256: 0f55e8976030710ac3800e6a9cb91de0959c52fd00e47a12dc36dde7bea1e539

Contents?: true

Size: 1.11 KB

Versions: 51

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Ibrain
  module Policies
    class BasePolicy
      class << self
        def query_rules
          {
            '*': {
              guard: ->(_obj, _args, _ctx) { false }
            }
          }
        end

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

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

        def roles
          Ibrain.user_class.roles.keys
        end

        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

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

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

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
ibrain-core-0.5.15 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.13 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.12 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.10 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.9 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.8 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.7 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.6 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.5 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.4 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.3 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.2 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.1 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.5.0 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.4.9 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.4.8 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.4.7 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.4.5 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.4.4 app/graphql/ibrain/policies/base_policy.rb
ibrain-core-0.4.3 app/graphql/ibrain/policies/base_policy.rb