Module: Authlogic::Session::Validation

Included in:
Base
Defined in:
lib/authlogic/session/validation.rb

Overview

Responsible for session validation

Defined Under Namespace

Classes: Errors

Instance Method Summary collapse

Instance Method Details

#attempted_recordObject

You should use this as a place holder for any records that you find during validation. The main reason for this is to allow other modules to use it if needed. Take the failed_login_count feature, it needs this in order to increase the failed login count.


28
29
30
# File 'lib/authlogic/session/validation.rb', line 28

def attempted_record
  @attempted_record
end

#attempted_record=(value) ⇒ Object

See attempted_record


33
34
35
# File 'lib/authlogic/session/validation.rb', line 33

def attempted_record=(value)
  @attempted_record = value
end

#errorsObject

The errors in Authlogic work JUST LIKE ActiveRecord. In fact, it uses the exact same ActiveRecord errors class. Use it the same way:

Example

class UserSession
  before_validation :check_if_awesome

  private
    def check_if_awesome
      errors.add(:login, "must contain awesome") if  && !.include?("awesome")
      errors.add(:base, "You must be awesome to log in") unless attempted_record.awesome?
    end
end

51
52
53
# File 'lib/authlogic/session/validation.rb', line 51

def errors
  @errors ||= Errors.new(self)
end

#valid?Boolean

Determines if the information you provided for authentication is valid or not. If there is a problem with the information provided errors will be added to the errors object and this method will return false.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/authlogic/session/validation.rb', line 58

def valid?
  errors.clear
  self.attempted_record = nil
  
  before_validation
  new_session? ? before_validation_on_create : before_validation_on_update
  validate
  ensure_authentication_attempted
          
  if errors.size == 0
    new_session? ? after_validation_on_create : after_validation_on_update
    after_validation
  end
  
  save_record(attempted_record)
  errors.size == 0
end