Class: Lato::User

Inherits:
ApplicationRecord show all
Includes:
LatoUserApplication
Defined in:
app/models/lato/user.rb

Instance Method Summary collapse

Instance Method Details

#accept_invitation(params) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/models/lato/user.rb', line 254

def accept_invitation(params)
  invitation = Lato::Invitation.find_by(id: params[:id], accepted_code: params[:accepted_code])
  if !invitation || invitation.accepted? || invitation.email != email
    errors.add(:base, :invitation_invalid)
    return
  end

  ActiveRecord::Base.transaction do
    raise ActiveRecord::Rollback unless save && invitation.update(
      accepted_at: Time.now,
      lato_user_id: id
    )

    true
  end
end

#add_web3_connection(params) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'app/models/lato/user.rb', line 271

def add_web3_connection(params)
  signature_pubkey = Eth::Signature.personal_recover(params[:web3_nonce], params[:web3_signed_nonce])
  signature_address = Eth::Util.public_key_to_address signature_pubkey
  unless signature_address.to_s.downcase == params[:web3_address].downcase
    errors.add(:base, :web3_address_invalid)
    return
  end

  update(web3_address: params[:web3_address])
rescue StandardError => e
  errors.add(:base, :web3_connection_error)
  false
end

#c_email_verification_code(value = nil) ⇒ Object



301
302
303
304
305
306
307
# File 'app/models/lato/user.rb', line 301

def c_email_verification_code(value = nil)
  cache_key = "Lato::User/c_email_verification_code/#{id}"
  return Rails.cache.read(cache_key) if value.nil?

  Rails.cache.write(cache_key, value, expires_in: 30.minutes)
  value
end

#c_email_verification_semaphore(value = nil) ⇒ Object

Cache



293
294
295
296
297
298
299
# File 'app/models/lato/user.rb', line 293

def c_email_verification_semaphore(value = nil)
  cache_key = "Lato::User/c_email_verification_semaphore/#{id}"
  return Rails.cache.read(cache_key) if value.nil?

  Rails.cache.write(cache_key, value, expires_in: 2.minutes)
  value
end

#c_password_update_code(value = nil) ⇒ Object



309
310
311
312
313
314
315
# File 'app/models/lato/user.rb', line 309

def c_password_update_code(value = nil)
  cache_key = "Lato::User/c_password_update_code/#{id}"
  return Rails.cache.read(cache_key) if value.nil?

  Rails.cache.write(cache_key, value, expires_in: 30.minutes)
  value
end

#destroy_with_confirmation(params) ⇒ Object



245
246
247
248
249
250
251
252
# File 'app/models/lato/user.rb', line 245

def destroy_with_confirmation(params)
  unless params[:email_confirmation] == email
    errors.add(:email, :not_correct)
    return
  end

  destroy
end

#full_nameObject

Helpers



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

def full_name
  "#{last_name} #{first_name}"
end

#gravatar_image_url(size = 200) ⇒ Object



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

def gravatar_image_url(size = 200)
  @gravatar_image_url ||= "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}?s=#{size}"
end

#remove_web3_connectionObject



285
286
287
288
# File 'app/models/lato/user.rb', line 285

def remove_web3_connection
  update(web3_address: nil)
  true
end

#request_recover_password(params) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/lato/user.rb', line 187

def request_recover_password(params)
  user = Lato::User.find_by(email: params[:email])
  unless user
    errors.add(:email, :not_registered)
    return
  end

  code = SecureRandom.hex.upcase
  delivery = Lato::UserMailer.password_update_mail(user.id, code).deliver_now
  unless delivery
    errors.add(:base, :email_sending_error)
    return
  end

  self.id = user.id
  reload

  c_password_update_code(code)

  true
end

#request_verify_emailObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/lato/user.rb', line 148

def request_verify_email
  if c_email_verification_semaphore
    errors.add(:base, :email_verification_limit)
    return
  end

  code = SecureRandom.hex.upcase
  delivery = Lato::UserMailer.email_verification_mail(id, code).deliver_now
  unless delivery
    errors.add(:base, :email_sending_error)
    return
  end

  c_email_verification_code(code)
  c_email_verification_semaphore(true)

  true
end

#signin(params) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/lato/user.rb', line 85

def (params)
  self.email = params[:email]

  user = Lato::User.find_by(email: params[:email])
  unless user
    errors.add(:email, :not_correct)
    return
  end

  unless user.authenticate(params[:password])
    errors.add(:password, :not_correct)
    return
  end

  self.id = user.id
  reload

  begin
    lato_log_user_signins.create(
      ip_address: params[:ip_address],
      user_agent: params[:user_agent]
    )
  rescue StandardError => e
    Rails.logger.error(e)
  end

  true
end

#signup(params = {}) ⇒ Object

Operations



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/lato/user.rb', line 70

def (params = {})
  return unless save

  begin
    lato_log_user_signups.create(
      ip_address: params[:ip_address],
      user_agent: params[:user_agent]
    )
  rescue StandardError => e
    Rails.logger.error(e)
  end

  true
end

#update_accepted_privacy_policy_version(params) ⇒ Object



227
228
229
230
231
232
233
234
# File 'app/models/lato/user.rb', line 227

def update_accepted_privacy_policy_version(params)
  unless params[:confirm]
    errors.add(:base, :privacy_policy_invalid)
    return
  end

  update(accepted_privacy_policy_version: Lato.config.legal_privacy_policy_version)
end

#update_accepted_terms_and_conditions_version(params) ⇒ Object



236
237
238
239
240
241
242
243
# File 'app/models/lato/user.rb', line 236

def update_accepted_terms_and_conditions_version(params)
  unless params[:confirm]
    errors.add(:base, :terms_and_conditions_invalid)
    return
  end

  update(accepted_terms_and_conditions_version: Lato.config.legal_terms_and_conditions_version)
end

#update_password(params) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/models/lato/user.rb', line 209

def update_password(params)
  password_update_code = c_password_update_code

  if password_update_code.blank?
    errors.add(:base, :password_update_code_expired)
    return
  end

  unless password_update_code == params[:code]
    errors.add(:base, :password_update_code_invalid)
    return
  end

  c_password_update_code('')

  update(params.permit(:password, :password_confirmation))
end

#valid_accepted_privacy_policy_version?Boolean

Questions

Returns:

  • (Boolean)


48
49
50
# File 'app/models/lato/user.rb', line 48

def valid_accepted_privacy_policy_version?
  @valid_accepted_privacy_policy_version ||= accepted_privacy_policy_version >= Lato.config.legal_privacy_policy_version
end

#valid_accepted_terms_and_conditions_version?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/lato/user.rb', line 52

def valid_accepted_terms_and_conditions_version?
  @valid_accepted_terms_and_conditions_version ||= accepted_terms_and_conditions_version >= Lato.config.legal_terms_and_conditions_version
end

#verify_email(params) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/models/lato/user.rb', line 167

def verify_email(params)
  email_verification_code = c_email_verification_code

  if email_verification_code.blank?
    errors.add(:base, :email_verification_code_expired)
    return
  end

  unless email_verification_code == params[:code]
    errors.add(:base, :email_verification_code_invalid)
    return
  end

  c_email_verification_code('')
  c_email_verification_semaphore(false)

  update_column(:email_verified_at, Time.now)
  true
end

#web3_signin(params) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/lato/user.rb', line 114

def (params)
  self.web3_address = params[:web3_address]

  user = Lato::User.find_by(web3_address: params[:web3_address].downcase)
  unless user
    errors.add(:web3_address, :not_correct)
    return
  end

  signature_pubkey = Eth::Signature.personal_recover(params[:web3_nonce], params[:web3_signed_nonce])
  signature_address = Eth::Util.public_key_to_address signature_pubkey
  unless signature_address.to_s.downcase == params[:web3_address].downcase
    errors.add(:web3_signed_nonce, :not_correct)
    return
  end

  self.id = user.id
  reload

  begin
    lato_log_user_signins.create(
      ip_address: params[:ip_address],
      user_agent: params[:user_agent]
    )
  rescue StandardError => e
    Rails.logger.error(e)
  end

  true
rescue StandardError => e
  errors.add(:base, :web3_connection_error)
  false
end