Class: User

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

Defined Under Namespace

Classes: ActivationCodeNotFound, AlreadyActivated, ArgumentError, PermissionsNotConfigured

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 'app/models/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.



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

def self.authenticate(, password)
  u = find :first, :conditions => ['login = ?', ]
  u && u.authenticated?(password) ? u : nil
end

.create_by_sql(attributes) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/user.rb', line 147

def self.create_by_sql(attributes)
  user = User.new(attributes)
  user.send('encrypt_password')
  user.send('make_activation_code')
  now = DateTime.now.to_formatted_s(:db)
  query = <<-SQL
  INSERT INTO users
      (activated_at, activation_code, created_at, crypted_password, email, enabled, firstName, lastName, login, password_reset_code, remember_token, remember_token_expires_at, salt, status, type, updated_at)
      VALUES
      ( '#{now}','#{user.activation_code}','#{now}', '#{user.crypted_password}', NULL, 1, '#{user.firstName}', '#{user.lastName}', '#{user.}', NULL, NULL, NULL, '#{user.salt}', NULL, NULL,'#{now}')
  SQL
  #can't use ActiveRecord#create here as it would trigger a notification email
  ActiveRecord::Base.connection.insert_sql(query)
end

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.



99
100
101
# File 'app/models/user.rb', line 99

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

.find_and_activate!(activation_code) ⇒ Object

Finds the user with the corresponding activation code, activates their account and returns the user.

Raises:

+User::ActivationCodeNotFound+ if there is no user with the corresponding activation code
+User::AlreadyActivated+ if the user with the corresponding activation code has already activated their account

Raises:



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

def self.find_and_activate!(activation_code)
  raise ArgumentError if activation_code.nil?
    user = find_by_activation_code(activation_code)
  raise ActivationCodeNotFound if !user
  raise AlreadyActivated.new(user) if user.active?
    user.send(:activate!)
    user
end

.find_with_activation_code(activation_code) ⇒ Object

Raises:



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

def self.find_with_activation_code(activation_code)
  raise ArgumentError if activation_code.nil?
    user = find_by_activation_code(activation_code)
  raise ActivationCodeNotFound if !user
  raise AlreadyActivated.new(user) if user.active?
  user
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


82
83
84
85
# File 'app/models/user.rb', line 82

def active?
  # the presence of an activation date means they have activated
  !activated_at.nil?
end

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#encrypt(password) ⇒ Object

Encrypts the password with the user salt



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

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

#first_last_nameObject



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

def first_last_name
  firstName+' '+lastName
end

#forget_meObject



137
138
139
140
141
# File 'app/models/user.rb', line 137

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

#forgot_passwordObject



116
117
118
119
# File 'app/models/user.rb', line 116

def forgot_password
  @forgotten_password = true
  self.make_password_reset_code
end

#has_role?(name) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'app/models/user.rb', line 143

def has_role?(name)
  self.roles.find_by_name(name) ? true : false
end

#pending?Boolean

Returns true if the user has just been activated.

Returns:

  • (Boolean)


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

def pending?
  @activated
end

#recently_forgot_password?Boolean

used in user_observer

Returns:

  • (Boolean)


129
130
131
# File 'app/models/user.rb', line 129

def recently_forgot_password?
  @forgotten_password
end

#recently_reset_password?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/models/user.rb', line 133

def recently_reset_password?
  @reset_password
end

#remember_token?Boolean

Returns:

  • (Boolean)


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

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

#reset_passwordObject



121
122
123
124
125
126
# File 'app/models/user.rb', line 121

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