Class: Rddd::Authorization::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/rddd/authorization/rule.rb

Overview

Representation of single authorization rule. It applies to a specific action and evaluates the given block to decide the outcome.

Constant Summary collapse

InvalidEvaluationBlock =
Class.new(RuntimeError)

Instance Method Summary collapse

Constructor Details

#initialize(action, &block) ⇒ Rule

Initialize.

Parameters:

  • Action (Symbol)

    to apply rule to.

  • Block (Block)

    to be evaluated during authorization.

Raises:



17
18
19
20
21
22
23
# File 'lib/rddd/authorization/rule.rb', line 17

def initialize(action, &block)
  # Block should take two arguments - user and params.
  raise InvalidEvaluationBlock unless block.arity == 2

  @action = action
  @block = block
end

Instance Method Details

#can?(user, params) ⇒ Boolean

Evalute the rule for a given user and params.

Parameters:

  • User (Object)

    we are gonna authorize.

  • Auxiliary (Hash)

    attributes to decide authorization.

Returns:

  • (Boolean)


41
42
43
# File 'lib/rddd/authorization/rule.rb', line 41

def can?(user, params)
  @block.call(user, params)
end

#is_for?(action) ⇒ Boolean

Is the Rule for given action?

Parameters:

  • Action (Symbol)

    to be matched.

Returns:

  • (Boolean)


31
32
33
# File 'lib/rddd/authorization/rule.rb', line 31

def is_for?(action)
  @action == action
end