代码之家  ›  专栏  ›  技术社区  ›  Jimmy Nitzan Tomer

在AuthLogic中强制验证空密码

  •  5
  • Jimmy Nitzan Tomer  · 技术社区  · 15 年前

    我正在向使用AuthLogic的Rails应用程序添加密码重置功能。我跟着导游走: http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/ 除了一件事:密码重置表单接受空白密码,并且不更改它们,所有事情都按我的意愿工作。

    我一直在四处搜索,并了解到这是预期的默认行为,因为它允许您创建用户编辑表单,这些表单只在用户输入新密码时更改用户密码,否则将忽略它。但在本例中,我特别想强制验证密码,就像用户最初注册时一样。对于这个问题,我已经找到了两个可能的解决方案,但还没有找到如何实现它们中的任何一个。

    1)有人在谷歌集团上问了同样的问题:

    User model saves with blank password

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

    2)似乎有一个AuthLogic配置选项调用 ignore_blank_passwords .记录如下:

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

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

    有人找到解决办法吗?我懂了 validate_password= 在AuthLogic1.4.1的更改日志中,此后未删除任何相关内容。我只是不正确地使用它吗?有使用方法吗 忽略\空白\密码 基于每个请求?

    5 回复  |  直到 14 年前
        1
  •  5
  •   Harish Shetty    14 年前

    这就是我所做的。

    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的范围内工作。您不必更改验证逻辑。

        2
  •  8
  •   kikito    14 年前

    这是一种老掉牙的说法,但既然没有人回答,我会把它贴出来。

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

    我将此添加到用户:

    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 => :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 在其他地方设置为“真”,即)。

    希望能有所帮助。

        3
  •  2
  •   Raimonds    14 年前
    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
  •  1
  •   zetetic    15 年前

    可以测试控制器中参数的值吗?(航空码):

    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
  •  1
  •   leviathan    14 年前

    除了齐塔人的解决方案,你可以这样做:

    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块中,您可以检查密码是否为空,并向用户记录中添加适当的错误消息或显示闪存消息。