lib/authlogic/session/password.rb in authlogic-2.0.5 vs lib/authlogic/session/password.rb in authlogic-2.0.6
- old
+ new
@@ -27,35 +27,38 @@
# def self.find_by_username_or_email(login)
# find_by_username(login) || find_by_email(login)
# end
# end
#
- # * <tt>Default:</tt> "find_by_#{login_field}"
+ # Now just specifcy the name of this method for this configuration option and you are all set. You can do anything you want here. Maybe you allow users to have multiple logins
+ # and you want to search a has_many relationship, etc. The sky is the limit.
+ #
+ # * <tt>Default:</tt> "find_by_case_insensitive_#{login_field}"
# * <tt>Accepts:</tt> Symbol or String
def find_by_login_method(value = nil)
- config(:find_by_login_method, value, "find_by_#{login_field}")
+ config(:find_by_login_method, value, "find_with_#{login_field}")
end
alias_method :find_by_login_method=, :find_by_login_method
# The name of the method you want Authlogic to create for storing the login / username. Keep in mind this is just for your
# Authlogic::Session, if you want it can be something completely different than the field in your model. So if you wanted people to
# login with a field called "login" and then find users by email this is compeltely doable. See the find_by_login_method configuration
# option for more details.
#
- # * <tt>Default:</tt> Uses the configuration option in your model: User.login_field
+ # * <tt>Default:</tt> klass.login_field || klass.email_field
# * <tt>Accepts:</tt> Symbol or String
def login_field(value = nil)
config(:login_field, value, klass.login_field || klass.email_field)
end
alias_method :login_field=, :login_field
- # Works exactly like login_field, but for the password instead.
+ # Works exactly like login_field, but for the password instead. Returns :password if a login_field exists.
#
# * <tt>Default:</tt> :password
# * <tt>Accepts:</tt> Symbol or String
def password_field(value = nil)
- config(:password_field, value, :password)
+ config(:password_field, value, login_field && :password)
end
alias_method :password_field=, :password_field
# The name of the method in your model used to verify the password. This should be an instance method. It should also be prepared to accept a raw password and a crytped password.
#
@@ -69,22 +72,27 @@
# Password related instance methods
module InstanceMethods
def initialize(*args)
if !self.class.configured_password_methods
- self.class.send(:attr_writer, login_field) if !respond_to?("#{login_field}=")
- self.class.send(:attr_reader, login_field) if !respond_to?(login_field)
- self.class.send(:attr_writer, password_field) if !respond_to?("#{password_field}=")
- self.class.send(:define_method, password_field) {} if !respond_to?(password_field)
+ if login_field
+ self.class.send(:attr_writer, login_field) if !respond_to?("#{login_field}=")
+ self.class.send(:attr_reader, login_field) if !respond_to?(login_field)
+ end
+
+ if password_field
+ self.class.send(:attr_writer, password_field) if !respond_to?("#{password_field}=")
+ self.class.send(:define_method, password_field) {} if !respond_to?(password_field)
- self.class.class_eval <<-"end_eval", __FILE__, __LINE__
- private
- # The password should not be accessible publicly. This way forms using form_for don't fill the password with the attempted password. The prevent this we just create this method that is private.
- def protected_#{password_field}
- @#{password_field}
- end
- end_eval
+ self.class.class_eval <<-"end_eval", __FILE__, __LINE__
+ private
+ # The password should not be accessible publicly. This way forms using form_for don't fill the password with the attempted password. The prevent this we just create this method that is private.
+ def protected_#{password_field}
+ @#{password_field}
+ end
+ end_eval
+ end
self.class.configured_password_methods = true
end
super
@@ -112,10 +120,10 @@
end
end
private
def authenticating_with_password?
- !send(login_field).nil? || !send("protected_#{password_field}").nil?
+ login_field && (!send(login_field).nil? || !send("protected_#{password_field}").nil?)
end
def validate_by_password
errors.add(login_field, I18n.t('error_messages.login_blank', :default => "can not be blank")) if send(login_field).blank?
errors.add(password_field, I18n.t('error_messages.password_blank', :default => "can not be blank")) if send("protected_#{password_field}").blank?
\ No newline at end of file