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.



30
31
32
# File 'lib/authlogic/session/validation.rb', line 30

def attempted_record
  @attempted_record
end

#attempted_record=(value) ⇒ Object

See attempted_record



35
36
37
# File 'lib/authlogic/session/validation.rb', line 35

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


53
54
55
# File 'lib/authlogic/session/validation.rb', line 53

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.

Returns:

  • (Boolean)


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

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.empty?
    new_session? ? after_validation_on_create : after_validation_on_update
    after_validation
  end

  save_record(attempted_record)
  errors.empty?
end