Ruby-on-rails “authenticate_user!”的设计实现在哪里?方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9272272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
where is devise implementation of "authenticate_user!" method?
提问by Greg
Where is devise implementation of authenticate_user!method?
authenticate_user!方法的设计实现在哪里?
I have been looking for it and have not found it so far.
我一直在寻找它,到目前为止还没有找到。
回答by jupp0r
It's in lib/devise/controllers/helpers.rb1and is generated dynamically (user being only one of the possible suffixes):
它在lib/devise/controllers/helpers.rb1 中并且是动态生成的(用户只是可能的后缀之一):
def self.define_helpers(mapping) #:nodoc:
mapping = mapping.name
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{mapping}!(opts={})
opts[:scope] = :#{mapping}
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
def #{mapping}_signed_in?
!!current_#{mapping}
end
def current_#{mapping}
@current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
end
def #{mapping}_session
current_#{mapping} && warden.session(:#{mapping})
end
METHODS
ActiveSupport.on_load(:action_controller) do
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
end
end
回答by Ultrasaurus
When you add devise to rails, you'll typically add in config/routes.rb:
当您将设计添加到 rails 时,您通常会添加config/routes.rb:
devise_for :user
This is defined in the Devise Mapper class.
这是在设计映射器类中定义的。
which calls Devise.add_mappingfor every resource passes to devise_for
这要求Devise.add_mapping每个资源传递给devise_for
the Devise module's add_mapping method is defined here, which subsequently calls define_helpers, which defines authenticateas discussed in other answers.
Devise 模块的 add_mapping 方法在此处定义,随后调用define_helpers,其定义authenticate如其他答案中所述。
回答by Dogbert
It's declared using some metaprogramming here - https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L46-49
它在这里使用一些元编程声明 - https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L46-49
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{mapping}!(opts={})
opts[:scope] = :#{mapping}
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
end
...
end

