module Subroutine::Auth::ClassMethods

Public Instance Methods

authorize(validation_name) click to toggle source
# File lib/subroutine/auth.rb, line 25
def authorize(validation_name)
  validate validation_name, unless: :skip_auth_checks?
end
no_user_requirements!() click to toggle source
# File lib/subroutine/auth.rb, line 29
def no_user_requirements!
  self.authorization_declared = true
end
policy(*meths) click to toggle source

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

# 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!() click to toggle source
# 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!() click to toggle source
# 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_names() click to toggle source
# File lib/subroutine/auth.rb, line 21
def supported_user_class_names
  [user_class_name, "Integer", "NilClass"].compact
end