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.21.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.21.1.pre.pr1476 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.21.1.pre.issue1450 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.21.1.pre.issue1444 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.20.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.19.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.18.1 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.18.1.pre.1.eagerloaddirs lib/avo/services/authorization_clients/pundit_client.rb
avo-2.18.0 lib/avo/services/authorization_clients/pundit_client.rb
avo-2.17.1.pre.5.stackedlayout lib/avo/services/authorization_clients/pundit_client.rb