Sha256: e79aaa78d69dae12d9b8b55546cc602964fcf15768fd2a3ac74e102e5f248c3f

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

require 'strong_actions/decision'

module StrongActions
  module Controller
    extend ActiveSupport::Concern

    included do
      if ::ActionPack::VERSION::MAJOR < 4
        before_filter :authorize_roles!
      else
        before_action :authorize_roles!
      end

      helper_method :available?
    end

    private

    def authorize_roles!
      StrongActions.config.roles.each do |role|
        unless judge(role, controller_path, action_name, params)
          message = "#{controller_path.classify}Controller##{action_name} is not permitted for role #{role}"
          raise StrongActions::ForbiddenAction.new(message)
        end
      end
    end

    def available?(controller_path, action_name = nil, params = {})
      StrongActions.config.roles.each do |role|
        return false unless judge(role, controller_path, action_name, params)
      end

      true
    end

    def judge(role, controller_path, action_name = nil, params = {})
      controller_path = normalize_controller_path(controller_path)

      @decision ||= StrongActions::Decision.new(self)
      @decision.call(role, controller_path, action_name, params)
    end

    def normalize_controller_path(controller_path)
      controller_path.start_with?('/') ? controller_path[1..-1] : controller_path 
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
strong_actions-0.2.1 lib/strong_actions/controller.rb
strong_actions-0.2.0 lib/strong_actions/controller.rb
strong_actions-0.1.1 lib/strong_actions/controller.rb
strong_actions-0.1.0 lib/strong_actions/controller.rb
strong_actions-0.0.9 lib/strong_actions/controller.rb
strong_actions-0.0.8 lib/strong_actions/controller.rb
strong_actions-0.0.7 lib/strong_actions/controller.rb