Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/authentication/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Virtual attribute for the unencrypted password



4
5
6
# File 'lib/authentication/user.rb', line 4

def password
  @password
end

Class Method Details

.authenticate(login, password) ⇒ Object

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



20
21
22
23
# File 'lib/authentication/user.rb', line 20

def self.authenticate(, password)
  u = () # need to get the salt
  u && u.authenticated?(password) ? u : nil
end

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.



26
27
28
# File 'lib/authentication/user.rb', line 26

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

Instance Method Details

#authenticated?(password) ⇒ Boolean



35
36
37
# File 'lib/authentication/user.rb', line 35

def authenticated?(password)
  crypted_password == encrypt(password)
end

#encrypt(password) ⇒ Object

Encrypts the password with the user salt



31
32
33
# File 'lib/authentication/user.rb', line 31

def encrypt(password)
  self.class.encrypt(password, salt)
end

#forget_meObject



50
51
52
53
54
# File 'lib/authentication/user.rb', line 50

def forget_me
  self.remember_token_expires_at = nil
  self.remember_token            = nil
  save(false)
end

#remember_meObject

These create and unset the fields required for remembering users between browser closes



44
45
46
47
48
# File 'lib/authentication/user.rb', line 44

def remember_me
  self.remember_token_expires_at = 2.weeks.from_now.utc
  self.remember_token            = encrypt("#{email}--#{remember_token_expires_at}")
  save(false)
end

#remember_token?Boolean



39
40
41
# File 'lib/authentication/user.rb', line 39

def remember_token?
  remember_token_expires_at && Time.now.utc < remember_token_expires_at 
end