Sha256: 0c7c8ff0633fb3a3770d08521c43293c2aaf776a2ae394a6d32c4ff9b80f3d67

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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.capitalize}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

1 entries across 1 versions & 1 rubygems

Version Path
strong_actions-0.0.6 lib/strong_actions/controller.rb