Module: Authlogic::ORMAdapters::ActiveRecordAdapter::ActsAsAuthentic::Credentials
- Defined in:
- lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb
Overview
Credentials
Handles any credential specific code, such as validating the login, encrpyting the password, etc.
Class Methods
-
friendly_unique_token
- returns a random string of 20 alphanumeric characters. Used when resetting the password. This is a more user friendly token then a long Sha512 hash.
Instance Methods
-
{options[:password_field]}=(value)
- encrypts a raw password and sets it to your crypted_password_field. Also sets the password_salt to a random token. -
valid_{options[:password_field]}?(password_to_check)
- checks is the password is valid. The password passed must be the raw password, not encrypted. -
reset_{options[:password_field]}
- resets the password using the friendly_unique_token class method -
reset_{options[:password_field]}!
- calls reset_password and then saves the record
Instance Method Summary collapse
Instance Method Details
#acts_as_authentic_with_credentials(options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb', line 20 def acts_as_authentic_with_credentials( = {}) acts_as_authentic_without_credentials() if [:validate_fields] if [:validate_login_field] case [:login_field_type] when :email validates_length_of [:login_field], :within => 6..100 validates_format_of [:login_field], :with => [:login_field_regex], :message => [:login_field_regex_failed_message] else validates_length_of [:login_field], :within => 2..100, :allow_blank => true validates_format_of [:login_field], :with => [:login_field_regex], :message => [:login_field_regex_failed_message] end validates_uniqueness_of [:login_field], :scope => [:scope] end if [:validate_email_field] && [:email_field] validates_length_of [:email_field], :within => 6..100 validates_format_of [:email_field], :with => [:email_field_regex], :message => [:email_field_regex_failed_message] validates_uniqueness_of [:email_field], :scope => [:scope] end validate :validate_password if [:validate_password_field] end attr_writer "confirm_#{[:password_field]}" attr_accessor "tried_to_set_#{[:password_field]}" class_eval <<-"end_eval", __FILE__, __LINE__ def self.friendly_unique_token chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a newpass = "" 1.upto(20) { |i| newpass << chars[rand(chars.size-1)] } newpass end def #{[:password_field]}=(pass) return if pass.blank? self.tried_to_set_#{[:password_field]} = true @#{[:password_field]} = pass self.#{[:password_salt_field]} = self.class.unique_token self.#{[:crypted_password_field]} = #{[:crypto_provider]}.encrypt(@#{[:password_field]} + #{[:password_salt_field]}) end def valid_#{[:password_field]}?(attempted_password) return false if attempted_password.blank? || #{[:crypted_password_field]}.blank? || #{[:password_salt_field]}.blank? (#{[:crypto_provider]}.respond_to?(:decrypt) && #{[:crypto_provider]}.decrypt(#{[:crypted_password_field]}) == attempted_password + #{[:password_salt_field]}) || (!#{[:crypto_provider]}.respond_to?(:decrypt) && #{[:crypto_provider]}.encrypt(attempted_password + #{[:password_salt_field]}) == #{[:crypted_password_field]}) end def #{[:password_field]}; end def confirm_#{[:password_field]}; end def reset_#{[:password_field]} friendly_token = self.class.friendly_unique_token self.#{[:password_field]} = friendly_token self.confirm_#{[:password_field]} = friendly_token end alias_method :randomize_password, :reset_password def reset_#{[:password_field]}! reset_#{[:password_field]} save_without_session_maintenance(false) end alias_method :randomize_password!, :reset_password! protected def tried_to_set_password? tried_to_set_password == true end def validate_password if new_record? || tried_to_set_#{[:password_field]}? if @#{[:password_field]}.blank? errors.add(:#{[:password_field]}, #{[:].inspect}) else errors.add(:confirm_#{[:password_field]}, #{[:].inspect}) if @confirm_#{[:password_field]} != @#{[:password_field]} end end end end_eval end |