Sha256: 4ecd5d13997ea3d68b7d1cb640cedd91d8bfb12f223a76e3e1a561194d5c1f6a

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# Canard provides a CanCan Ability class for you. The Canard Ability class
# looks for and applies abilities for the object passed when a new Ability
# instance is initialized.
#
# If the passed object has a reference to user the user is set to that.
# Otherwise the passed object is assumed to be the user. This gives the
# flexibility to have a seperate model for authorization from the model used
# to authenticate the user.
#
# Abilities are applied in the order they are set with the acts_as_user method
# for example for the User model
#
#     class User < ActiveRecord::Base
#
#       acts_as_user :roles =>  :manager, :admin
#
#     end
#
# the abilities would be applied in the order: users, managers, admins
# with each subsequent set of abilities building on or overriding the existing
# abilities.
#
# If there is no object passed to the Ability.new method a guest ability is
# created and Canard will look for a guests.rb amongst the ability definitions
# and give the guest those abilities.
class Ability

  include CanCan::Ability

  attr_reader :user

  def initialize(object=nil)
    
    # If object has a user attribute set the user from it otherwise assume
    # this is the user.
    @user = object.respond_to?(:user) ? object.user : object
    
    if @user
      # Add the base user abilities.
      append_abilities @user.class.name.underscore.to_sym
    else
      # If user not set then lets create a guest
      @user = Object.new
      append_abilities :guest
    end
    
    # If user has roles get those abilities
    if @user.respond_to?(:roles)
      # Add roles on top of the base user abilities
      @user.roles.each { |role| append_abilities(role) }
    end
  
  end
  
  private
  
  def append_abilities(role)
    ability_definitions = Canard.ability_definitions
    instance_eval(&ability_definitions[role]) if ability_definitions.has_key?(role)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
canard-0.3.6 lib/ability.rb
canard-0.3.5 lib/ability.rb