如何向Devise添加策略? [英] How do I add a strategy to Devise

查看:91
本文介绍了如何向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

代码应该检查参数的auto_login参数,如果存在,找到它可以并登录的第一个用户。我已经完全跳过了安全措施,只是得到一个基本的测试用例。当我尝试登录到具有 before_filter authenticate_user!的控制器(即 localhost:3000 / test?auto_login = true ),它无法登录并重定向到登录页面。我做错了什么?

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. When I try to log into a controller that has a before_filter authenticate_user! (i.e. localhost:3000/test?auto_login=true), it can't log me in and redirects me to the login page. What am I doing wrong?

推荐答案

你可能想尝试直接添加到Warden :: Strategies:

You may want to try adding it directly to 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.

这篇关于如何向Devise添加策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆