class Can4::Ability
Ability
class for resources.
To define an ability model for your resource, define an ability class in a location of your choosing, and define the actions available to the resource on construction.
@example
class Ability < Can4::Ability def initialize(user) # Handle unauthenticated users. user ||= User.new if user.admin? # Allow admins to perform any action. allow_anything! else # Will always return true for can?(:read, @comment). can :read, Comment # Will only return true for can?(:read, @private_message) # if the user is allowed to read the private message. can :read, PrivateMessage do |msg| msg.user_id == user.id end end end end
Public Instance Methods
Allows the object to perform any action on any subject. This overrides all cannot rules.
# File lib/can4/ability.rb, line 63 def allow_anything! instance_eval do def can?(*) true end def cannot?(*) false end end end
Adds an access-granting rule.
@param action [Symbol] The action, represented as a symbol. @param subject [Object] The subject. @param block [Proc] An optional Proc to install for matching.
# File lib/can4/ability.rb, line 57 def can(action, subject, &block) rule_for(subject).add_grant(action, block) end
Checks whether the object can perform an action on a subject.
@overload can?(action, subject)
@param action [Symbol] The action, represented as a symbol. @param subject [Object] The subject.
@overload can?(action, subject, *args)
@param action [Symbol] The action, represented as a symbol. @param subject [Object] The subject. @param args [Object] Splat parameters to an installed block.
@return [Boolean] True or false.
# File lib/can4/ability.rb, line 41 def can?(action, subject, *args) lookup_rule(subject).authorized?(action, subject, args) end
Protected Instance Methods
Lookup a rule for a particular subject.
@param subject [Object] The subject.
# File lib/can4/ability.rb, line 102 def lookup_rule(subject) case subject when Symbol, Module subjects[subject] || NullRule else subjects[subject.class] || NullRule end end
Find or create a new rule for the specified subject.
@param subject [Object] The subject.
# File lib/can4/ability.rb, line 95 def rule_for(subject) subjects[subject] ||= SubjectRule.new end
Subjects hash.
# File lib/can4/ability.rb, line 88 def subjects @subjects ||= {} end