简体   繁体   English

如何为Devise添加策略

[英]How do I add a strategy to Devise

I'm trying to add a really simple strategy to devise, and it doesn't seem to be working. 我正在尝试添加一个非常简单的策略来设计,但是它似乎没有用。 Here is the code that I am trying to use 这是我尝试使用的代码

#config/initializers/devise.rb
Devise.setup do |config|
  config.orm = :mongo_mapper

  config.warden do |manager|
    manager.strategies.add(:auto_login_strategy) do 
      def valid?
        params[:auto_login]
      end

      def authenticate!
        u = User.find(:first)
        u.nil? ? fail!("No created users") : success!(u)
      end
    end
    manager.default_strategies(:scope=>:user).unshift :auto_login_strategy
  end  
end

The code is supposed to check the params for an 'auto_login' parameter, and if present, find the first user it can and log them in. I have skipped security measures entirely to just get a basic test case working. 该代码应该检查“ auto_login”参数的参数,如果存在,请找到它可以登录的第一个用户,然后登录。我完全跳过了安全措施,只是使一个基本的测试用例能够正常工作。 When I try to log into a controller that has a before_filter authenticate_user! 当我尝试登录具有before_filter authenticate_user!的控制器时before_filter authenticate_user! (ie localhost:3000/test?auto_login=true ), it can't log me in and redirects me to the login page. (即localhost:3000/test?auto_login=true ),它无法登录并重定向到登录页面。 What am I doing wrong? 我究竟做错了什么?

You may want to try adding it directly to Warden::Strategies: 您可能想尝试将其直接添加到Warden :: Strategies:

class MyStrategy
  def valid?...
  def authenticate!...
end

Warden::Strategies.add(:database_authenticatable, MyStrategy)

I did this a while ago, but then ended up not needing it. 我前一阵子做了,但是最终不需要了。 Let me know if I've remembered it correctly. 让我知道我是否正确记得了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM