module Hobo::Model::UserBase::ClassMethods

Additional classmethods for authentication

Public Instance Methods

authenticate(login, password) click to toggle source

Authenticates a user by their login name and unencrypted password. Returns the user or nil.

# File lib/hobo/model/user_base.rb, line 77
def authenticate(login, password)
  # Downcase emails before logging in
  login = login.downcase if attr_type(@login_attribute) == HoboFields::Types::EmailAddress

  u = where("#{@login_attribute} = ?", login).first # need to get the salt

  if u && u.authenticated?(password)
    if u.respond_to?(:last_login_at) || u.respond_to?(:login_count)
      u.last_login_at = Time.now if u.respond_to?(:last_login_at)
      u.login_count = (u.login_count.to_i + 1) if u.respond_to?(:login_count)
      u.save
    end
    u
  else
    nil
  end
end
encrypt(password, salt) click to toggle source

Encrypts some data with the salt.

# File lib/hobo/model/user_base.rb, line 96
def encrypt(password, salt)
  Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end
login_attribute=(attr, validate=true) click to toggle source
# File lib/hobo/model/user_base.rb, line 60
def login_attribute=(attr, validate=true)
  @login_attribute = attr = attr.to_sym
  unless attr == :login
    alias_attribute(:login, attr)
    declare_attr_type(:login, attr_type(attr)) if table_exists? # this breaks if the table doesn't exist
  end

  if validate
    validates_length_of     attr, :within => 3..100
    validates_uniqueness_of attr, :case_sensitive => false
  end
end