【问题标题】:Force validation of blank passwords in Authlogic强制验证 Authlogic 中的空白密码
【发布时间】:2011-01-11 13:23:46
【问题描述】:

我正在向使用 Authlogic 的 Rails 应用程序添加密码重置功能。我按照这里的指南进行操作:http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/,一切都按我的意愿进行,除了一件事:密码重置表单接受空白密码并且根本不会更改它们。

我一直在四处寻找,并了解到这是预期的默认行为,因为它允许您使用户编辑表单,只有在输入新密码时才更改用户密码,否则忽略它。但在这种情况下,我特别想在用户最初注册时强制验证密码。对于这个问题,我找到了两种可能的解决方案,但还没有弄清楚如何实现它们。

1) 有人在 Google 网上论坛上问过同样的问题:

User model saves with blank password

Ben 的回应是使用@user.validate_password = true 来强制验证密码。我试过这个,但我得到一个未定义的方法错误:undefined method 'validate_password_field=' for #<User>

2) 似乎有一个名为 ignore_blank_passwords 的 Authlogic 配置选项。它记录在这里:

Module: Authlogic::ActsAsAuthentic::Password::Config#ignore_blank_passwords

这看起来可行,但我的理解是,这是您在 User 模型中的初始 acts_as_authentic 调用中使用的全局配置选项,我不想在应用程序范围内更改它,因为我确实有一个常规的编辑表单供用户使用,我希望默认忽略空白密码。

有人找到解决办法了吗?我在 Authlogic 1.4.1 的更改日志中看到了validate_password=,从那时起就没有任何关于它的内容被删除。我只是不正确地使用它吗?有没有办法在每个请求的基础上使用ignore_blank_passwords

【问题讨论】:

    标签: ruby-on-rails authlogic reset-password


    【解决方案1】:

    这是一个旧线程,但由于没有答案,我会发布这个。

    我已经设法比其他解决方案更干净一些,用我自己的“帮助”验证逻辑验证。

    我将此添加给用户:

    class User < ActiveRecord::Base
    
      ...
    
      attr_writer :password_required
    
      validates_presence_of :password, :if => :password_required?
    
      def password_required?
        @password_required
      end
    
      ...
    end
    

    您可以通过创建attr_accessor 和使用:if =&gt; :password_required(无询问)将其减少到两行,但我更喜欢带有询问标志的其他语法。

    那么你的控制器动作可以这样完成:

    def update
      @user.password = params[:user][:password]
      @user.password_confirmation = params[:user][: password_confirmation]
      @user.password_required = true
    
      if @user.save
        flash[:notice] = "Password successfully updated"
        redirect_to account_url
      else
        render :action => :edit
      end
    end
    

    这将产生局部影响;应用程序的其余部分不会受到影响(除非password_required 在其他地方设置为true,即)。

    希望对你有帮助。

    【讨论】:

    • 我可以使用ignore_blank_paswords 属性。详情看我的回答。
    【解决方案2】:

    这是我做的。

    class User < ActiveRecord::Base
      attr_accessor :ignore_blank_passwords
    
      # object level attribute overrides the config level
      # attribute
      def ignore_blank_passwords?
        ignore_blank_passwords.nil? ? super : (ignore_blank_passwords == true)
      end
    end
    

    现在在您的控制器中,将 ignore_blank_passwords 属性设置为 false。

    user.ignore_blank_passwords = false
    

    在这里,您在 AuthLogic 的范围内工作。您不必更改验证逻辑。

    【讨论】:

      【解决方案3】:
      User.ignore_blank_passwords = false
      

      使用模型,而不是对象来设置这个属性。

      def update_passwords
        User.ignore_blank_passwords = false
        if @user.update_attributes(params[:user])
          ...
        end
        User.ignore_blank_passwords = true
      end
      

      【讨论】:

      • 这不会改变整个模型吗?我只想在一个操作中忽略空白密码。
      【解决方案4】:

      也许测试控制器中的参数值? (航空代码):

      def update
        @user.password = params[:user][:password]
        @user.password_confirmation = params[:user][: password_confirmation]
        if @user.password.blank?
          flash[:error] = "Password cannot be blank"
          render :action => :edit
          return
        end
        if @user.save
          flash[:notice] = "Password successfully updated"
          redirect_to account_url
        else
          render :action => :edit
        end
      end
      

      【讨论】:

        【解决方案5】:

        除了zetetic的解决方案之外,您还可以这样做:

        def update
          @user.password = params[:user][:password]
          @user.password_confirmation = params[:user][: password_confirmation]
        
          if @user.changed? && @user.save
            flash[:notice] = "Password successfully updated"
            redirect_to account_url
          else
            render :action => :edit
          end
        end
        

        您基本上是在检查 authlogic 是否更改了用户记录(如果密码为空,则不会更改)。在 else 块中,您可以检查密码是否为空,并在用户记录中添加适当的错误消息或显示闪烁消息。

        【讨论】:

          猜你喜欢
          • 2011-01-12
          • 2019-08-13
          • 1970-01-01
          • 2015-02-19
          • 1970-01-01
          • 1970-01-01
          • 2020-07-25
          • 1970-01-01
          • 2014-04-15
          相关资源
          最近更新 更多