views:

218

answers:

2

Hello, I'm using Authlogic to manage my user sessions. I'm using the LDAP add-on, so I have the following in my users model

  acts_as_authentic do |c|
     c.validate_password_field = false
  end

The problem is that recently I found out that there will be some users inside the application that won't be part of the LDAP (and can't be added!). So I would need to validate SOME passwords against the database and the others against the LDAP.

The users whose password will be validated against the database will have an specific attribute that will tell me that that password will be validated in my database.

How can I manage that? Is it possible that the validate_password_field receives a "variable"? That way I could create some method that will return true/false depending on where the password validation will be done?

Thanks!

Nicolás Hock Isaza

A: 

You should be able to do an unless in your validation.

acts_as_authentic do |c|
    c.validate_password_field = false if c.ldap
end

Or even (as your model field is a boolean) :

acts_as_authentic do |c|
    c.validate_password_field = c.ldap
end
Damien MATHIEU
That means that the "acts_as_authentic" configuration modules are not loaded on start-up?
Hock
+1  A: 

You should be able to do this:

  acts_as_authentic do |u|
    u.validate_password_field = true
    authentic_options = {:unless => Proc.new{|c| c.ldap}}
    u.merge_validates_confirmation_of_password_field_options(authentic_options)
    u.merge_validates_length_of_password_confirmation_field_options(authentic_options)
    u.merge_validates_length_of_password_field_options(authentic_options)
  end

If you were writing the validation yourself (not using authlogic) you would want to do something like this in the validation:

validates_presence_of :password, :unless => Proc.new{|u| u.ldap }

Since authlogic provides the 3 helper methods to add options to the end of the validates methods, you can use this to turn off validations when using LDAP.

awaage