Sha256: 70ece93679552479aebd2111f9c4ddd91eb59ee592677901a015e743ffe00008

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

module RoleOn
  module RoleOnControllerMethods
    def role_on(role, options = {})
      before_filter do |c|
        action = c.params[:action].intern
        user_roles = c.current_user.roles.map(&:name).map(&:intern)
        restricted_actions = if options.include?(:on)
                               [options[:on]].flatten
                             elsif options.include?(:except)
                               c.class.action_methods.to_a.map(&:intern) - [options[:except]].flatten
                             else
                               c.class.action_methods.to_a.map(&:intern)
                             end
        if restricted_actions.include?(action) && !user_roles.include?(role)
          c.send(:access_denied)
          false
        end
        true
      end
    end
  end

  module RoleOnUserMethods
    def has_role?(role)
      return false if roles.nil?
      roles.include?(Role[role])
    end
  end

  def self.included(klass)
    if User == klass
      klass.send(:extend, RoleOnUserMethods)
      klass.send(:has_and_belongs_to_many, :roles, :join_table => 'user_roles')
    elsif ApplicationController == klass
      klass.send(:extend, RoleOnControllerMethods)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
darkhelmet-role_on-0.1.1 lib/role_on.rb