2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: france_connect_informations
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# birthdate :date
|
|
|
|
# birthplace :string
|
2021-04-12 11:29:31 +02:00
|
|
|
# data :jsonb
|
2020-08-06 16:35:45 +02:00
|
|
|
# email_france_connect :string
|
|
|
|
# family_name :string
|
|
|
|
# gender :string
|
|
|
|
# given_name :string
|
2021-10-11 12:32:33 +02:00
|
|
|
# merge_token :string
|
|
|
|
# merge_token_created_at :datetime
|
2021-04-12 15:05:05 +02:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2020-08-06 16:35:45 +02:00
|
|
|
# france_connect_particulier_id :string
|
2021-10-12 23:53:11 +02:00
|
|
|
# user_id :integer
|
2020-08-06 16:35:45 +02:00
|
|
|
#
|
2018-03-06 13:44:29 +01:00
|
|
|
class FranceConnectInformation < ApplicationRecord
|
2021-10-13 00:14:12 +02:00
|
|
|
MERGE_VALIDITY = 15.minutes
|
|
|
|
|
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
|
|
|
|
2021-10-13 00:18:00 +02:00
|
|
|
def 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
|
|
|
|
# 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
|
2021-02-01 14:28:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
update_attribute('user_id', user.id)
|
2021-04-26 16:26:09 +02:00
|
|
|
touch # needed to update updated_at column
|
2021-02-01 14:28:04 +01:00
|
|
|
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
|
|
|
|
update(merge_token: merge_token, merge_token_created_at: Time.zone.now)
|
|
|
|
|
|
|
|
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
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|