Module: Walruz::Subject::ClassMethods

Defined in:
lib/walruz/subject.rb

Instance Method Summary collapse

Instance Method Details

#check_authorizations(policy_map) ⇒ Object

Stablishes the actions that can be made with a subject. You may specify as many actions as you like, and also you may have a default policy, that will get executed if a specified flag doesn’t exist. You just have to pass the action :default, or the policy class only.

Once you stablish the authorizations policies on a subject, you can check if an actor is able to interact with it via the Walruz::Actor methods

Examples:

# Without :default key
class UserProfile
  check_authorizations :read  => Policies::FriendPolicy,
                       :write => Policies::OwnerPolicy
end

# With :default key
class UserProfile
  check_authorizations :read    => Policies::FriendPolicy,
                       :write   => Policies::OwnerPolicy,
                       :default => Policies::AdminPolicy
end

# Without any key at all
class UserProfile
  # this policy is the default one
  check_authorizations Policies::OwnerPolicy
end

Invoking the actions from the actor

current_user.can?(:read, profile)

Parameters:

  • Set (Hash)

    of actions with associated policies

Returns:

  • self



79
80
81
82
83
84
85
86
# File 'lib/walruz/subject.rb', line 79

def check_authorizations(policy_map)
  case policy_map
  when Hash
    self._walruz_policies = policy_map
  else
    self._walruz_policies = { :default => policy_map }
  end
end