[英]Change minimum password length with restful_authentication
有没有办法通过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。
转到供应商/插件/restful-authentication/lib/authentication/by_password.rb并编辑此字符串
validates_length_of :password, :within => 6..40, :if => :password_required?
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没有这些配置选项。 正确的解决方案是派生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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.