Commit 5b7e0c2a by liyijie

Support array auth_action symbol

parent 1255ae0c
...@@ -7,21 +7,27 @@ module RailsApiAuthentication ...@@ -7,21 +7,27 @@ module RailsApiAuthentication
end end
def perform def perform
klass = self.class.action_class klasses = self.class.action_classes
@current_auth = klass.send(:auth!, request) klasses.each do |klass|
rescue UserError => e @current_auth = klass.auth!(request) rescue next
render json: e.message, status: e.status break
end
render(json: "Unauthorized", status: 401) if @current_auth.nil?
end end
module ClassMethods module ClassMethods
def auth_action klass_sym, options={} def auth_action klass_sym, options={}
prepend_before_action :perform, options prepend_before_action :perform, options
@klass = klass_sym.to_s.camelize.constantize define_method("current_auth") { @current_auth }
define_method("current_#{klass_sym}") { @current_auth || nil }
@klasses = Array(klass_sym).map do |sym|
define_method("current_#{sym}") { @current_auth }
sym.to_s.camelize.constantize
end
end end
def action_class def action_classes
@klass || superclass.action_class @klasses || superclass.action_classes
end end
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment