Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Virtual attribute for the unencrypted password



7
8
9
# File 'app/models/user.rb', line 7

def password
  @password
end

Class Method Details

.authenticate(email, password) ⇒ Object

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



44
45
46
47
# File 'app/models/user.rb', line 44

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

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.



50
51
52
# File 'app/models/user.rb', line 50

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

Instance Method Details

#administrator?Boolean



105
106
107
# File 'app/models/user.rb', line 105

def administrator?
  !!admin
end

#authenticated?(password) ⇒ Boolean



59
60
61
# File 'app/models/user.rb', line 59

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

#count_test_plansObject



35
36
37
# File 'app/models/user.rb', line 35

def count_test_plans
  test_plans.count
end

#display_nameObject

method called by usernamestamp plugin



101
102
103
# File 'app/models/user.rb', line 101

def display_name
  return self.first_name + " " + self.last_name
end

#encrypt(password) ⇒ Object

Encrypts the password with the user salt



55
56
57
# File 'app/models/user.rb', line 55

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

#forget_meObject



74
75
76
77
78
# File 'app/models/user.rb', line 74

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

#forgot_passwordObject



80
81
82
83
# File 'app/models/user.rb', line 80

def forgot_password
  @forgotten_password = true
  self.make_password_reset_code
end

#grant_adminObject



109
110
111
# File 'app/models/user.rb', line 109

def grant_admin
  update_attributes(:admin => true)
end

#recently_forgot_password?Boolean



96
97
98
# File 'app/models/user.rb', line 96

def recently_forgot_password?
  @forgotten_password
end

#recently_reset_password?Boolean



92
93
94
# File 'app/models/user.rb', line 92

def recently_reset_password?
  @reset_password
end

#remember_meObject

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



68
69
70
71
72
# File 'app/models/user.rb', line 68

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



63
64
65
# File 'app/models/user.rb', line 63

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

#reset_passwordObject



85
86
87
88
89
90
# File 'app/models/user.rb', line 85

def reset_password
  # First update the password_reset_code before setting the 
  # reset_password flag to avoid duplicate email notifications.
  update_attributes(:password_reset_code => nil)
  @reset_password = true
end

#revoke_adminObject



113
114
115
# File 'app/models/user.rb', line 113

def revoke_admin
  update_attributes(:admin => false)
end

#to_sObject



39
40
41
# File 'app/models/user.rb', line 39

def to_s
  display_name
end