Sha256: 786db284b85b7ec6bd4b2497c6d2ecf7967b9757ca00c99a02ea692e00cb5f24

Contents?: true

Size: 973 Bytes

Versions: 7

Compression:

Stored size: 973 Bytes

Contents

# frozen_string_literal: true

module Plutonium
  module Core
    module Controllers
      module Authorizable
        extend ActiveSupport::Concern
        include ActionPolicy::Controller

        included do
          authorize :user, through: :current_user
          authorize :scope, through: :entity_scope_for_authorize

          helper_method :policy_for, :authorized_resource_scope
        end

        private

        def authorized_resource_scope(resource, **options)
          raise ArgumentError("Expected resource to be a class inheriting ActiveRecord::Base") unless resource.instance_of?(Class) && resource < ActiveRecord::Base

          options[:with] ||= ActionPolicy.lookup(resource, namespace: authorization_namespace)
          resource = resource.all

          authorized_scope(resource, **options)
        end

        def entity_scope_for_authorize
          scoped_to_entity? ? current_scoped_entity : nil
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
plutonium-0.15.3 lib/plutonium/core/controllers/authorizable.rb
plutonium-0.15.2 lib/plutonium/core/controllers/authorizable.rb
plutonium-0.15.1 lib/plutonium/core/controllers/authorizable.rb
plutonium-0.15.0 lib/plutonium/core/controllers/authorizable.rb
plutonium-0.15.0.pre.rc3 lib/plutonium/core/controllers/authorizable.rb
plutonium-0.15.0.pre.rc2 lib/plutonium/core/controllers/authorizable.rb
plutonium-0.15.0.pre.rc1 lib/plutonium/core/controllers/authorizable.rb