Class: UserAccount

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/user_account.rb

Overview

Every User may have an UserAccount that enables the user to log in to the website.

user = User.create(...)       # This user may not log in.
account = user.build_account
account.password = "foo"
account.save                  # Now, the user may log in.
account.destroy               # Now, the user may not log in anymore.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loginObject

Virtual attribute for authenticating by either username, alias or email



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

def 
  @login
end

Class Method Details

.find_first_by_auth_conditions(warden_conditions) ⇒ Object

Used by devise to identify the correct user account by the given strings.



83
84
85
86
87
# File 'app/models/user_account.rb', line 83

def self.find_first_by_auth_conditions(warden_conditions)
   = warden_conditions[:login] || warden_conditions[:email]
  return UserAccount.identify() if 
  return UserAccount.where(warden_conditions).first # use devise identification system for auth tokens and the like.
end

.identify(login_string) ⇒ Object

Tries to identify a user based on the given ‘login_string`. This can be one of those defined in `User.attributes_used_for_identification`, currently, `[:alias, :last_name, :name, :email]`.

Bug fix: The alias is prioritized, such that a user having the alias doe can be identified by this alias even if there are other users with surname Doe.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/user_account.rb', line 96

def self.identify()
  
  # Priorization: Check alias first. (Bug fix)
  user_identified_by_alias = User.find_by_alias()
   = [ User.find_by_alias() ] if user_identified_by_alias
  
  # What can go wrong?
  # 1. No user could match the login string.
   ||= User.find_all_by_identification_string(  )
  #raise 'no_user_found' unless users_that_match_the_login_string.count > 0
  return nil unless .count > 0

  # 2. The user may not have an active user account.
   = .select do |user|
    user.has_account? 
  end
  raise 'user_has_no_account' unless .count > 0
  
  # 3. The identification string may refer to several users with an active user account.
  raise 'identification_not_unique' if .count > 1
  identified_user = .first

  return identified_user.
end

Instance Method Details

#auth_tokenObject



141
142
143
# File 'app/models/user_account.rb', line 141

def auth_token
  super || generate_auth_token!
end

#email=(value) ⇒ Object

HACK: This method seems to be required by the PasswordController and is missing, since we have a virtual email field. TODO: If we ever change the Password authentication



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

def email= value
  #dummy required by devise to create an 'error' user account
end

#email_changed?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/user_account.rb', line 67

def email_changed?
  false
end

#generate_auth_token!Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/user_account.rb', line 145

def generate_auth_token!
  # see also: https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
  #
  raise 'auth_token already set' if self.read_attribute(:auth_token)
  token = ''
  loop do
    token = Devise.friendly_token + Devise.friendly_token
    break token unless UserAccount.where(auth_token: token).first
  end
  self.update_attribute :auth_token, token
  token
end

#generate_passwordObject



127
128
129
# File 'app/models/user_account.rb', line 127

def generate_password
  self.password = Password.generate
end

#generate_password_if_unsetObject

This generates a password if (1) no password is stored in the database and (2) no new password is set to be saved (in the ‘password` attribute).



133
134
135
136
137
138
139
# File 'app/models/user_account.rb', line 133

def generate_password_if_unset
  if self.encrypted_password.blank?
    unless self.password
      self.generate_password
    end
  end
end

#readonly?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/user_account.rb', line 56

def readonly?
  false # Otherwise, the user is not able to login.
end

#remember_meObject

Configure each account to not automatically log out when the browser is closed. After a system reboot, the user is still logged in, which is the expected behaviour for this application.

This useses devise’s rememberable module.



77
78
79
# File 'app/models/user_account.rb', line 77

def remember_me
  true
end

#send_new_passwordObject



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

def send_new_password
  generate_password
  self.save
  send_welcome_email
end

#send_welcome_emailObject



158
159
160
161
# File 'app/models/user_account.rb', line 158

def send_welcome_email
  raise 'attempt to send welcome email with empty password' unless self.password
  UserAccountMailer.welcome_email( self.user, self.password ).deliver
end