Sha256: 428d8051298f88377871deb1d7fc5a9f76100896e5af58674f175e57f27a4d3f

Contents?: true

Size: 1.95 KB

Versions: 50

Compression:

Stored size: 1.95 KB

Contents

ActiveAdmin::Dependency.pundit!

require 'pundit'

# Add a setting to the application to configure the pundit default policy
ActiveAdmin::Application.inheritable_setting :pundit_default_policy, nil

module ActiveAdmin

  class PunditAdapter < AuthorizationAdapter

    def authorized?(action, subject = nil)
      policy = retrieve_policy(subject)
      action = format_action(action, subject)

      policy.respond_to?(action) && policy.public_send(action)
    end

    def scope_collection(collection, action = Auth::READ)
      # scoping is appliable only to read/index action
      # which means there is no way how to scope other actions
      Pundit.policy_scope!(user, collection)
    rescue Pundit::NotDefinedError => e
      if default_policy_class && default_policy_class.const_defined?(:Scope)
        default_policy_class::Scope.new(user, collection).resolve
      else
        raise e
      end
    end

    def retrieve_policy(subject)
      case subject
      when nil   then Pundit.policy!(user, resource)
      when Class then Pundit.policy!(user, subject.new)
      else Pundit.policy!(user, subject)
      end
    rescue Pundit::NotDefinedError => e
      if default_policy_class
        default_policy(user, subject)
      else
        raise e
      end
    end

    def format_action(action, subject)
      # https://github.com/elabs/pundit/blob/master/lib/generators/pundit/install/templates/application_policy.rb
      case action
      when Auth::CREATE  then :create?
      when Auth::UPDATE  then :update?
      when Auth::READ    then subject.is_a?(Class) ? :index? : :show?
      when Auth::DESTROY then subject.is_a?(Class) ? :destroy_all? : :destroy?
      else "#{action}?"
      end
    end

    private

    def default_policy_class
      ActiveAdmin.application.pundit_default_policy && ActiveAdmin.application.pundit_default_policy.constantize
    end

    def default_policy(user, subject)
      default_policy_class.new(user, subject)
    end

  end

end

Version data entries

50 entries across 50 versions & 6 rubygems

Version Path
activeadmin-1.2.1 lib/active_admin/pundit_adapter.rb
activeadmin_addons-1.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/pundit_adapter.rb
activeadmin-1.2.0 lib/active_admin/pundit_adapter.rb
activeadmin_addons-1.1.1 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/pundit_adapter.rb
yousty-activeadmin-1.0.17.pre lib/active_admin/pundit_adapter.rb
yousty-activeadmin-1.0.16.pre lib/active_admin/pundit_adapter.rb
activeadmin_addons-1.1.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/pundit_adapter.rb
activeadmin_addons-1.0.1 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/pundit_adapter.rb
activeadmin_addons-1.0.0 vendor/bundle/ruby/2.2.0/bundler/gems/activeadmin-a5a53c3f2b8f/lib/active_admin/pundit_adapter.rb
activeadmin-1.1.0 lib/active_admin/pundit_adapter.rb
yousty-activeadmin-1.0.15.pre lib/active_admin/pundit_adapter.rb
activeadmin-1.0.0 lib/active_admin/pundit_adapter.rb
activeadmin-1.0.0.pre5 lib/active_admin/pundit_adapter.rb
activeadmin-orac-1.0.0.pre4 lib/active_admin/pundit_adapter.rb
activeadmin-orac-1.0.0 lib/active_admin/pundit_adapter.rb
activeadmin-orac-1.0.0.pre.orac lib/active_admin/pundit_adapter.rb
yousty-activeadmin-1.0.14.pre lib/active_admin/pundit_adapter.rb
yousty-activeadmin-1.0.13.pre lib/active_admin/pundit_adapter.rb
yousty-activeadmin-1.0.12.pre lib/active_admin/pundit_adapter.rb
yousty-activeadmin-1.0.11.pre lib/active_admin/pundit_adapter.rb