Sha256: 8c71c404d2efd6d40846fa4dd803d3db6c2e55cdbe4fe70e9fc11b4f232a3446

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# A role defines a group of users in the system who are able to access a
# collection of features in the application.
# 
# == Examples
# 
#   Role.bootstrap(
#     {:id => 1, :name => 'administrator'},
#     {:id => 2, :name => 'developer'},
#     {:id => 3, :name => 'guest'}
#   )
class Role < ActiveRecord::Base
  enumerate_by :name
  
  has_many :assigned_permissions, :class_name => 'RolePermission'
  has_many :permissions, :through => :assigned_permissions
  has_many :assignments, :class_name => 'RoleAssignment'
  
  # Is this role authorized for the given url?  The url can be any one of the
  # following formats:
  # * +string+ - A relative or absolute path in the application
  # * +hash+ - A hash include the controller/action attributes
  # 
  # Using this information, the controller and action can be determined.
  # Authorization is based on whether the role has a permission that either
  # directly matches the path or represents a parent path (i.e. using the
  # controller/class hierarchy)
  named_scope :authorized_for, lambda {|*args|
    options = args.first || ''
    controller, action = Permission.recognize_path(options)
    controllers = "#{controller.camelize}Controller".constantize.ancestors.select {|c| c < ActionController::Base}.map(&:controller_path)
    
    {:joins => :permissions, :conditions => ['permissions.controller IN (?) AND (permissions.action IS NULL OR permissions.action = ?)', controllers, action]}
  }
  
  bootstrap(
    {:id => 1, :name => 'admin'}
  )
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
has_roles-0.3.1 app/models/role.rb
has_roles-0.3.0 app/models/role.rb