Module: Walruz::Actor

Includes:
Manager::AuthorizationQuery, Memoization
Defined in:
lib/walruz/actor.rb

Overview

This module provides the methods that enable an actor to check if it is able to do an action on a subject. It also provides methods to check on specific Policies given the policy label. The methods mostly used by classes that include this module are:

can?(action, subject)

Returns a Boolean indicating if the actor is authorized to do an action on the subject.

authorize(action, subject)

Returns Either nil if the actor is not authorized or a Hash with the parameters returned from the Policy if the actor is authorized.

authorize!(action, subject)

Returns a Hash returned by the Policy if the actor is authorized or raises a Walruz::NotAuthorized exception otherwise.

satisfies?(policy_label, subject)

Returns true or false if the Policy is satisfied with the actor and subject given.

satisfies(policy_label, subject)

Returns Either nil if the actor and subject don’t satisfy the policy or a Hash with the parameters returned from the Policy.

Instance Method Summary collapse

Methods included from Memoization

included

Instance Method Details

#authorize(action, subject) ⇒ Hash #authorize(action, subject, reload) ⇒ Hash

Overloads:

  • #authorize(action, subject) ⇒ Hash

    Allows an actor to check if he can do some action on a given subject. The main difference between this method and the can? method is that this will return a Hash of values returned by the policies, in case the actor is not authorized, it will return nil.

    Note:

    This method will check the authorization the first time, following invocations will return a cached result unless the third parameter is specified.

    Parameters:

    • The (Symbol)

      action as it is declared on the check_authorizations method on the subject class.

    • The (Walruz::Subject)

      subject on which the actor wants to execute the action.

    Returns:

    • (Hash)

      Parameters returned from the policy.

  • #authorize(action, subject, reload) ⇒ Hash

    Allows an actor to check if he can do some action on a given subject. The main difference between this method and the can? method is that this will return a Hash of values returned by the policies, in case the actor is not authorized, it will return nil.

    Parameters:

    • The (Symbol)

      action as it is declared on the check_authorizations method on the subject class.

    • The (Walruz::Subject)

      subject on which the actor wants to execute the action.

    • A (Symbol)

      symbol with the value “:reload” indicating that you want to reset the cached result.

    Returns:

    • (Hash)

      Parameters returned from the policy.



65
66
67
# File 'lib/walruz/actor.rb', line 65

def authorize(action, subject)
  super(self, action, subject)
end

#authorize!(label, subject) ⇒ Hash

Allows an actor to check if he can do some action on a given subject. This method will behave similarly to the authorize method, the only difference is that instead of returning nil when the actor is not authorized, it will raise a Walruz::NotAuthorized exception.

Parameters:

  • The (Symbol)

    action as it is declared on the check_authorizations method on the subject class.

  • The (Walruz::Subject)

    subject on which the actor wants to execute the action.

Returns:

  • (Hash)

    Parameters returned from the policy.

Raises:

  • (Walruz::NotAuthorized)

    error if the actor is not authorized to perform the specified action on the subject.



81
82
83
# File 'lib/walruz/actor.rb', line 81

def authorize!(label, subject)
  super(self, label, subject)
end

#can?(action, subject) ⇒ Boolean #can?(action, subject, reload) ⇒ Boolean

Overloads:

  • #can?(action, subject) ⇒ Boolean

    Allows an actor to check if he can perform an action on a given subject.

    Note:

    This method will check the authorization the first time, following invocations will return a cached result unless the third parameter is specified.

    Parameters:

    • The (Symbol)

      action as it is declared on the check_authorizations method on the subject class.

    • The (Walruz::Subject)

      subject on which the actor wants to execute the action.

    • (optional) (Boolean)

      A boolean indicating if you want to reset the cached result.

    Returns:

    • (Boolean)

      A boolean indicating if the actor is authorized to perform the action (or not) on the subject.

  • #can?(action, subject, reload) ⇒ Boolean

    Allows an actor to check if he can perform an action on a given subject.

    Parameters:

    • The (Symbol)

      action as it is declared on the check_authorizations method on the subject class.

    • The (Walruz::Subject)

      subject on which the actor wants to execute the action.

    • A (Boolean)

      boolean indicating if you want to reset the cached result.

    Returns:

    • (Boolean)

      A boolean indicating if the actor is authorized to perform the action (or not) on the subject.



37
38
39
# File 'lib/walruz/actor.rb', line 37

def can?(action, subject)
  super(self, action, subject)
end

#satisfies(policy_label, subject) ⇒ Hash

Allows an actor to check if he satisfies the condition of a policy with a given subject.

Params:

Parameters:

Returns:

  • (Hash)

    Hash with the parameters returned from the policy if the actor and the subject satisfy the policy, nil otherwise.



105
106
107
# File 'lib/walruz/actor.rb', line 105

def satisfies(policy_label, subject)
  super(self, policy_label, subject)
end

#satisfies!(policy_label, subject) ⇒ Object



109
110
111
# File 'lib/walruz/actor.rb', line 109

def satisfies!(policy_label, subject)
  super(self, policy_label, subject)
end

#satisfies?(policy_label, subject) ⇒ Boolean

Allows an actor to check if he satisfies the condition of a policy with a given subject.

Params:

Parameters:

Returns:

  • (Boolean)

    saying if the actor and the subject satisify the policy.



93
94
95
# File 'lib/walruz/actor.rb', line 93

def satisfies?(policy_label, subject)
  super(self, policy_label, subject)
end