2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 13:44:29 +01:00
|
|
|
class FranceConnectInformation < ApplicationRecord
|
2021-10-13 00:14:12 +02:00
|
|
|
MERGE_VALIDITY = 15.minutes
|
2024-07-01 16:41:02 +02:00
|
|
|
CONFIRMATION_EMAIL_VALIDITY = 2.days
|
2021-10-13 00:14:12 +02:00
|
|
|
|
2020-07-20 16:06:03 +02:00
|
|
|
belongs_to :user, optional: true
|
2016-01-21 17:06:09 +01:00
|
|
|
|
|
|
|
validates :france_connect_particulier_id, presence: true, allow_blank: false, allow_nil: false
|
2021-02-01 14:28:04 +01:00
|
|
|
|
2024-07-31 17:50:14 +02:00
|
|
|
def safely_associate_user!(email)
|
2021-10-11 11:39:14 +02:00
|
|
|
begin
|
|
|
|
user = User.create!(
|
2021-10-13 00:18:00 +02:00
|
|
|
email: email.downcase,
|
2021-10-11 11:39:14 +02:00
|
|
|
password: Devise.friendly_token[0, 20],
|
|
|
|
confirmed_at: Time.zone.now
|
|
|
|
)
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2024-07-01 16:41:02 +02:00
|
|
|
# ignore this exception because we check before if user is nil.
|
2021-10-11 11:39:14 +02:00
|
|
|
# exception can be raised in race conditions, when FranceConnect calls callback 2 times.
|
|
|
|
# At the 2nd call, user is nil but exception is raised at the creation of the user
|
|
|
|
# because the first call has already created a user
|
2021-02-01 14:28:04 +01:00
|
|
|
end
|
|
|
|
|
2024-07-31 17:50:14 +02:00
|
|
|
clean_tokens_and_requested_email
|
2021-02-01 14:28:04 +01:00
|
|
|
update_attribute('user_id', user.id)
|
2024-07-31 17:50:14 +02:00
|
|
|
save!
|
2021-02-01 14:28:04 +01:00
|
|
|
end
|
2021-10-13 00:14:12 +02:00
|
|
|
|
2024-07-31 17:50:14 +02:00
|
|
|
def safely_update_user(user:)
|
|
|
|
self.user = user
|
|
|
|
clean_tokens_and_requested_email
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_custom_confirmation_instructions
|
2024-07-01 16:41:02 +02:00
|
|
|
token = SecureRandom.hex(10)
|
|
|
|
user.update!(confirmation_token: token, confirmation_sent_at: Time.zone.now)
|
|
|
|
UserMailer.custom_confirmation_instructions(user, token).deliver_later
|
|
|
|
end
|
|
|
|
|
2021-10-13 00:40:15 +02:00
|
|
|
def create_merge_token!
|
|
|
|
merge_token = SecureRandom.uuid
|
2024-01-11 10:44:27 +01:00
|
|
|
update(merge_token:, merge_token_created_at: Time.zone.now)
|
2021-10-13 00:40:15 +02:00
|
|
|
|
|
|
|
merge_token
|
|
|
|
end
|
|
|
|
|
2024-01-10 12:43:17 +01:00
|
|
|
def create_email_merge_token!
|
|
|
|
email_merge_token = SecureRandom.uuid
|
|
|
|
update(email_merge_token:, email_merge_token_created_at: Time.zone.now)
|
|
|
|
|
|
|
|
email_merge_token
|
|
|
|
end
|
|
|
|
|
2021-10-13 00:14:12 +02:00
|
|
|
def valid_for_merge?
|
|
|
|
(MERGE_VALIDITY.ago < merge_token_created_at) && user_id.nil?
|
|
|
|
end
|
2021-10-13 09:23:40 +02:00
|
|
|
|
2024-01-10 12:43:17 +01:00
|
|
|
def valid_for_email_merge?
|
|
|
|
(MERGE_VALIDITY.ago < email_merge_token_created_at) && user_id.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_email_merge_token!
|
|
|
|
update(email_merge_token: nil, email_merge_token_created_at: nil)
|
|
|
|
end
|
|
|
|
|
2024-07-31 17:50:14 +02:00
|
|
|
def clean_tokens_and_requested_email
|
|
|
|
self.merge_token = nil
|
|
|
|
self.merge_token_created_at = nil
|
|
|
|
self.email_merge_token = nil
|
|
|
|
self.email_merge_token_created_at = nil
|
|
|
|
self.requested_email = nil
|
|
|
|
end
|
|
|
|
|
2022-09-13 14:27:07 +02:00
|
|
|
def full_name
|
|
|
|
[given_name, family_name].compact.join(" ")
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|