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

- (Object) password

Virtual attribute for the unencrypted password



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

def password
  @password
end

Class Method Details

+ (Object) authenticate(email, password)

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

+ (Object) encrypt(password, salt)

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

- (Boolean) administrator?

Returns:

  • (Boolean)


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

def administrator?
  !!admin
end

- (Boolean) authenticated?(password)

Returns:

  • (Boolean)


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

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

- (Object) count_test_plans



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

def count_test_plans
  test_plans.count
end

- (Object) display_name

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

- (Object) encrypt(password)

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

- (Object) forget_me



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

- (Object) forgot_password



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

def forgot_password
  @forgotten_password = true
  self.make_password_reset_code
end

- (Object) grant_admin



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

def grant_admin
  update_attributes(:admin => true)
end

- (Boolean) recently_forgot_password?

Returns:

  • (Boolean)


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

def recently_forgot_password?
  @forgotten_password
end

- (Boolean) recently_reset_password?

Returns:

  • (Boolean)


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

def recently_reset_password?
  @reset_password
end

- (Object) remember_me

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

- (Boolean) remember_token?

Returns:

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

- (Object) reset_password



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

- (Object) revoke_admin



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

def revoke_admin
  update_attributes(:admin => false)
end

- (Object) to_s



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

def to_s
  display_name
end