Is there a way to change the minimum length for passwords with restful_authentication? Currently it's 6 characters and I need another value.
I've tried calling validates_length_of before and after Authentication::ByPassword like this
validates_length_of :password, :within => 4..40, :if => :password_required?
include Authentication::ByPassword开发者_如何学JAVA
and like this:
include Authentication::ByPassword
validates_length_of :password, :within => 4..40, :if => :password_required?
but the minimum password remained at 6.
go to vendor/plugins/restful-authentication/lib/authentication/by_password.rb and edit this string
validates_length_of :password, :within => 6..40, :if => :password_required?
ActsAsAuthentic has configuration options like so:
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
Unfortunately, RestfulAuthentication doesn't have these configuration options. The right solution would be to fork the RestfulAuthentication project and add them.
In the mean time, you could monkey-patch 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
精彩评论