Sha256: be42c32b284ccb39c7bc8da5ee19d9472ed835538de1fc592680f3366ede184f

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require 'has_roles/authorization_helper'
require 'has_roles/url_helper'

# Adds a generic implementation for dealing with role management
module HasRoles
  module MacroMethods
    # Indicates that the model has roles. This will create the following
    # associations:
    # * +role_assignments+ - The join association for roles that have been
    #   assigned to a record in this model
    # * +roles+ - The actual roles through the join association
    def has_roles
      has_many :role_assignments, :class_name => 'RoleAssignment', :as => :assignee, :dependent => :destroy
      has_many :roles, :through => :role_assignments
      
      include HasRoles::InstanceMethods
    end
  end
  
  module InstanceMethods
    # Checks whether this user is authorized to access the given url.
    # 
    # == Examples
    # 
    #   user = User.find(1)
    #   user.authorized_for?(:controller => 'admin/messages')
    #   user.authorized_for?(:controller => 'admin/messages', :action => 'destroy')
    #   user.authorized_for?('admin/messages')
    #   user.authorized_for?('http://localhost:3000/admin/messages')
    def authorized_for?(options = '')
      !Permission.restricts?(options) || roles.authorized_for(options).exists?
    end
  end
end

ActiveRecord::Base.class_eval do
  extend HasRoles::MacroMethods
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
has_roles-0.3.1 lib/has_roles.rb
has_roles-0.3.0 lib/has_roles.rb