【问题标题】:Change minimum password length with restful_authentication使用 restful_authentication 更改最小密码长度
【发布时间】:2011-02-10 02:58:19
【问题描述】:

有没有办法使用 restful_authentication 更改密码的最小长度?目前是 6 个字符,我需要另一个值。

我试过像这样在 Authentication::ByPassword 之前和之后调用 validates_length_of

validates_length_of :password, :within => 4..40, :if => :password_required?  
include Authentication::ByPassword

像这样:

include Authentication::ByPassword
validates_length_of :password, :within => 4..40, :if => :password_required?  

但最小密码仍为 6。

【问题讨论】:

    标签: ruby-on-rails ruby restful-authentication


    【解决方案1】:

    转到 vendor/plugins/restful-authentication/lib/authentication/by_password.rb 并编辑此字符串

    validates_length_of :password, :within => 6..40, :if => :password_required?
    

    【讨论】:

    • 然后插件被更新,网络应用程序无故中断。
    • 你到底写了什么?因为我刚刚试了一下,效果很好
    • 我尝试过的就是这个问题。不知道你的意思除此之外。我的观点是,如果您修改第三方代码而不提交上游和/或创建分支并使用它,那么您可能会更新该代码并丢失您的修改,并且很难跟踪和调试。
    • 虽然restful_authentication已经很老了,但我认为它的支持不是很好,直到更新插件
    【解决方案2】:

    ActsAsAuthentic 具有如下配置选项:

    acts_as_authentic do |config|
      config.merge_validates_length_of_password_field_options       :within => 4..40
      config.merge_validates_confirmation_of_password_field_options :within => 4..40
    end
    

    不幸的是,RestfulAuthentication 没有这些配置选项。 正确的解决方案是 fork RestfulAuthentication 项目并添加它们。

    同时,你可以给Authentication::ByPassword.included打猴子补丁:

    # in app/models/user.rb:
    Authentication::ByPassword.class_eval do
      def self.included(base)
        recipient.extend(ModelClassMethods)
        recipient.class_eval do
          include ModelInstanceMethods
    
          # Virtual attribute for the unencrypted password
          attr_accessor :password
          validates_presence_of :password, :if => :password_required?
          validates_presence_of :password_confirmation, :if => :password_required?
          validates_confirmation_of :password, :if => :password_required?
          validates_length_of :password, :within => 4..40, :if => :password_required?
          before_save :encrypt_password
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 2013-11-12
      相关资源
      最近更新 更多