Module: Umpire::AuthHelper

Included in:
Policy
Defined in:
lib/umpire/auth_helper.rb

Instance Method Summary collapse

Instance Method Details

#can?(*args) ⇒ Boolean

Usage:

with subject can? user, :drive, car, using: HighwayCode

without subject (assumes current_user if available) can? :drive, car, using: HighwayCode

multiple policies can? :park, car, using: [HighwayCode, ParkingRules]

without object can? :cook_spaghetti, using: [KitchenPolicy]

multiple actions can? [:order, :drink], beer, using: BarPolicy

Returns:

  • (Boolean)

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/umpire/auth_helper.rb', line 20

def can? *args
  options = args.last.is_a?(Hash) ? args.pop : {}
  policies = *options[:using]
  raise PolicyMissingError.new if policies.empty?

  first_argument = args.shift
  if first_argument.is_a?(Symbol) || first_argument.is_a?(Array)
    subject = defined?(current_user) ? current_user : {}
    actions = first_argument
  else
    subject = first_argument
    actions = args.shift
  end

  next_argument = args.shift
  object = next_argument.is_a?(Hash) ? nil : next_argument

  policies.each do |policy|
    return false unless policy.allows?(subject).to(actions, object)
  end

  true
end