Module: Hobo::Model::UserBase::ClassMethods

Defined in:
lib/hobo/model/user_base.rb

Overview

Additional classmethods for authentication

Instance Method Summary collapse

Instance Method Details

#authenticate(login, password) ⇒ Object

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hobo/model/user_base.rb', line 77

def authenticate(, password)
  # Downcase emails before logging in
   = .downcase if attr_type(@login_attribute) == HoboFields::Types::EmailAddress

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

  if u && u.authenticated?(password)
    if u.respond_to?(:last_login_at) || u.respond_to?(:login_count)
      u. = Time.now if u.respond_to?(:last_login_at)
      u. = (u..to_i + 1) if u.respond_to?(:login_count)
      u.save
    end
    u
  else
    nil
  end
end

#encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.



96
97
98
# File 'lib/hobo/model/user_base.rb', line 96

def encrypt(password, salt)
  Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end

#login_attribute=(attr, validate = true) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hobo/model/user_base.rb', line 60

def (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