demarches-normaliennes/app/models/france_connect_information.rb

63 lines
1.7 KiB
Ruby
Raw Normal View History

# 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
belongs_to :user, optional: true
validates :france_connect_particulier_id, presence: true, allow_blank: false, allow_nil: false
2021-10-13 00:18:00 +02:00
def associate_user!(email)
begin
user = User.create!(
2021-10-13 00:18:00 +02:00
email: email.downcase,
password: Devise.friendly_token[0, 20],
confirmed_at: Time.zone.now
)
user.after_confirmation
rescue ActiveRecord::RecordNotUnique
# ignore this exception because we check before is user is nil.
# 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
end
update_attribute('user_id', user.id)
2021-04-26 16:26:09 +02:00
touch # needed to update updated_at column
end
2021-10-13 00:14:12 +02:00
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
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
def valid_for_email_merge?
(MERGE_VALIDITY.ago < email_merge_token_created_at) && user_id.nil?
end
def delete_merge_token!
update(merge_token: nil, merge_token_created_at: nil)
end
def delete_email_merge_token!
update(email_merge_token: nil, email_merge_token_created_at: nil)
end
def full_name
[given_name, family_name].compact.join(" ")
end
2017-04-04 15:27:04 +02:00
end