Module: Walruz::CoreExt::Array

Defined in:
lib/walruz/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#only_authorized_for(actor, options = {}) ⇒ Array<Walruz::Subject> #only_authorized_for(actor, action) ⇒ Array<Walruz::Subject>

Overloads:

  • #only_authorized_for(actor, options = {}) ⇒ Array<Walruz::Subject>

    Filters the Walruz::Subject elements inside an array either by a policy or by an action on the subject. This will execute the authorization policies using specified actor and action/policy.

    Notes:

    • If a policy is given, you have to use the policy label.

    • If an action is given, you have to use the name of the action declared on the subject.

    Examples:

    Filtering a list of Posts by the read action specified on the Post class

    Post.all.only_authorized_for(current_user, :action => :read)
    # this will execute current_user.can?(:read, post) for each element of the array

    Filtering a list of Posts by a policy

    Post.all.only_authorized_for(nil, :policy => :public_policy)
    # This will execute the Policy with the label ":public_policy" on every post using the given actor

    Parameters:

    • actor (Walruz::Actor)

      The actor that is going to be used on the authorization process

    • opts (Hash)

      for the filtering process.

    Returns:

    Raises:

  • #only_authorized_for(actor, action) ⇒ Array<Walruz::Subject>

    Filters the Walruz::Subject elements inside an array by the action specified. This will execute the authorization policies using specified actor and action on each subject of the Array.

    Examples:

    Filtering a list of Posts by the read action specified on the Post class

    Post.all.only_authorized_for(current_user, :read)
    # this will execute current_user.can?(:read, post) for each element of the array

    Parameters:

    • actor (Walruz::Actor)

      The actor that is going to be used on the authorization process

    • action (Symbol)

      to be executed by the actor on each subject in the list.

    Returns:

    Raises:



47
48
49
50
51
52
53
# File 'lib/walruz/core_ext/array.rb', line 47

def only_authorized_for(actor, opts = {})
  unless opts.kind_of?(Symbol)
    only_authorized_with_options(actor, opts)
  else # use the opts 
    only_authorized_on_action(actor, opts)
  end
end