Sha256: 3b3900b5f529a953547640a68c47c4807e77950be8c0b1594b6827b7710830f7

Contents?: true

Size: 1.56 KB

Versions: 90

Compression:

Stored size: 1.56 KB

Contents

module Avo
  module Services
    module AuthorizationClients
      class PunditClient
        def authorize(user, record, action, policy_class: nil)
          Pundit.authorize(user, record, action, policy_class: policy_class)
        rescue Pundit::NotDefinedError => error
          raise NoPolicyError.new error.message
        rescue Pundit::NotAuthorizedError => error
          raise NotAuthorizedError.new error.message
        end

        def policy(user, record)
          Pundit.policy(user, record)
        end

        def policy!(user, record)
          Pundit.policy!(user, record)
        rescue Pundit::NotDefinedError => error
          raise NoPolicyError.new error.message
        end

        def apply_policy(user, model, policy_class: nil)
          # Try and figure out the scope from a given policy or auto-detected one
          scope_from_policy_class = scope_for_policy_class(policy_class)

          # If we discover one use it.
          # Else fallback to pundit.
          if scope_from_policy_class.present?
            scope_from_policy_class.new(user, model).resolve
          else
            Pundit.policy_scope!(user, model)
          end
        rescue Pundit::NotDefinedError => error
          raise NoPolicyError.new error.message
        end

        private

        # Fetches the scope for a given policy
        def scope_for_policy_class(policy_class = nil)
          return if policy_class.blank?

          if policy_class.present? && defined?(policy_class::Scope)
            policy_class::Scope
          end
        end
      end
    end
  end
end

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
avo-2.53.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.52.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.49.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.48.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.47.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.46.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.45.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.44.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.43.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.42.2 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.42.1 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.42.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.41.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.40.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.39.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.38.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.37.2 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.37.1 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.37.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.36.3 lib/avo/services/authorization_clients/pundit_client.rb