views:

122

answers:

2

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

and like this:

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

but the minimum password remained at 6.

A: 

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?
fl00r
And then the plug in is updated and the web app breaks for no apparent reason.
J. Pablo Fernández
what exactly did you write? 'cause I've just try it and it's work perfect
fl00r
What I've tried is on the question. Not sure what you mean beyond that. My point is that if you modify third party code without submitting upstream and/or creating a branch and using it, then you risk updating that code and losing your modifications, and that can be very hard to track and debug.
J. Pablo Fernández
it's not big and critic patch, although restful_authentication is quite old and I don't think it is supporting well and till updating plugin
fl00r
A: 

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
James A. Rosen