Module: Subroutine::Auth::ClassMethods

Defined in:
lib/subroutine/auth.rb

Instance Method Summary collapse

Instance Method Details

#authorize(validation_name) ⇒ Object



25
26
27
# File 'lib/subroutine/auth.rb', line 25

def authorize(validation_name)
  validate validation_name, unless: :skip_auth_checks?
end

#no_user_requirements!Object



29
30
31
# File 'lib/subroutine/auth.rb', line 29

def no_user_requirements!
  self.authorization_declared = true
end

#policy(*meths) ⇒ Object

policy :can_update_user policy :can_update_user, unless: :dont_do_it policy :can_update_user, if: :do_it policy :can_do_whatever, policy: :foo_policy



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/subroutine/auth.rb', line 53

def policy(*meths)
  opts = meths.extract_options!
  policy_name = opts[:policy] || :policy

  if_conditionals = Array(opts[:if])
  unless_conditionals = Array(opts[:unless])

  validate unless: :skip_auth_checks? do
    run_it = true
    # http://guides.rubyonrails.org/active_record_validations.html#combining-validation-conditions

    # The validation only runs when all the :if conditions
    if if_conditionals.present?
      run_it &&= if_conditionals.all? { |i| send(i) }
    end

    # and none of the :unless conditions are evaluated to true.
    if unless_conditionals.present?
      run_it &&= unless_conditionals.none? { |u| send(u) }
    end

    next unless run_it

    p = send(policy_name)
    if !p || meths.any? { |m| !(p.respond_to?("#{m}?") ? p.send("#{m}?") : p.send(m)) }
      unauthorized! opts[:error]
    end
  end
end

#require_no_user!Object



41
42
43
44
45
46
47
# File 'lib/subroutine/auth.rb', line 41

def require_no_user!
  self.authorization_declared = true

  validate unless: :skip_auth_checks? do
    unauthorized! :empty_unauthorized if current_user.present?
  end
end

#require_user!Object



33
34
35
36
37
38
39
# File 'lib/subroutine/auth.rb', line 33

def require_user!
  self.authorization_declared = true

  validate unless: :skip_auth_checks? do
    unauthorized! unless current_user.present?
  end
end

#supported_user_class_namesObject



21
22
23
# File 'lib/subroutine/auth.rb', line 21

def supported_user_class_names
  [user_class_name, "Integer", "NilClass"].compact
end