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

allow_anything!() click to toggle source

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
authorize!(action, subject, *args) click to toggle source

Checks whether this resource has authorization to perform an action on a particular subject. Raises {Can4::AccessDenied} if it doesn't.

@param action [Symbol] The intended action. @param subject [Object] The subject of the action. @raise [AccessDenied] if the object does not have permission.

# File lib/can4/ability.rb, line 81
def authorize!(action, subject, *args)
  raise AccessDenied if cannot?(action, subject, *args)
end
can(action, subject, &block) click to toggle source

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
can?(action, subject, *args) click to toggle source

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
cannot?(*args) click to toggle source

Inverse of can?.

@see can?

# File lib/can4/ability.rb, line 48
def cannot?(*args)
  !can?(*args)
end

Protected Instance Methods

lookup_rule(subject) click to toggle source

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
rule_for(subject) click to toggle source

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() click to toggle source

Subjects hash.

# File lib/can4/ability.rb, line 88
def subjects
  @subjects ||= {}
end