Sha256: 17a7a955b3ee56d436670f2f1ccf4d2e6c11f363e704c02f0e4f4d51428450a7

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module AccessPolicyRails
  module ControllerExtensions
    extend ActiveSupport::Concern

     included do
       include AccessPolicy

       AccessPolicy.instance_methods.each do |method|
         hide_action method
       end

       hide_action :policy_check_user
       hide_action :authorize
       hide_action :policy_for

       helper_method :policy_for

     end

    def policy_check_user
      current_user
    end

    def authorize(*args, &block)
      _guard.authorize(*args, &block)
    end

    def policy_for(object_to_guard=self)
      _guard.send(:switched_user_or_role, policy_check_user) do
        PolicyWrapper.new(_guard.policy_for(object_to_guard))
      end
    end

    module ClassMethods
      def guarded_action(action_name, authorize_action: true, &block)
        if authorize_action
          authorized_action(action_name, &block)
        else
          authorized_service(action_name, &block)
        end
      end

      def authorized_action(action_name, &block)
        action_name_guarded = "#{action_name}_with_guard".to_sym
        policy_guarded_method action_name_guarded, action_name ,&block

        define_method action_name do
          with_user_or_role(policy_check_user) do
            self.send(action_name_guarded)
          end
        end

        hide_action action_name_guarded
        hide_action unsafe_action_name(action_name_guarded)
      end

      def authorized_service(action_name, &block)

        define_method action_name do
          with_user_or_role(policy_check_user) do
            instance_exec &block
          end
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
access_policy_rails-0.0.1 lib/access_policy_rails/controller_extensions.rb