什么是设计authenticate_user的代码! 生成后:用户
我意识到authenticate_user!
没有在gem文件中明确定义 ,但我想知道典型的应用程序(名为User
模型上的身份validation),该方法是什么样的。 我需要知道,以便我可以稍微修改它。
我相信你链接到你自己的答案,它定义的方法是
def authenticate_#{mapping}!(opts={}) opts[:scope] = :#{mapping} warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) end
如果我们替换真正的类,在您的情况下User
,它看起来像:
def authenticate_user!(opts={}) opts[:scope] = :user warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) end
因此,它确实需要提供警告,而这正是大部分身份validation逻辑所在。
对于典型的Rails应用程序, authenticate_user!
方法将被定义为ApplicationController
上的instance_method
。
Devise使用Warden进行授权。 并且为了使用它,Devise提供自己的授权策略,包括授权! 方法(这是你需要的)。 您已经拥有的代码的第一部分(来自您问题中的链接)。 它是:
def authenticate_user!(opts={}) opts[:scope] = :user warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) end
在这里需要了解的是warden.authenticate!
方法。 这种方法由Devise to Warden给出,它取决于选择的Devise策略。
例如,实现Devise的DatabaseAuthenticatable策略的方法在这里: https : //github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb ,实现Devise的可记忆策略的代码在这里: https ://github.com/plataformatec/devise/blob/master/lib/devise/strategies/rememberable.rb
但仍有许多事情需要思考。 祝好运 !