2018-03-06 13:44:29 +01:00
|
|
|
class User < ApplicationRecord
|
2018-02-28 14:31:03 +01:00
|
|
|
include CredentialsSyncableConcern
|
|
|
|
include EmailSanitizableConcern
|
|
|
|
|
2017-05-26 21:31:51 +02:00
|
|
|
enum loged_in_with_france_connect: {
|
|
|
|
particulier: 'particulier',
|
|
|
|
entreprise: 'entreprise'
|
|
|
|
}
|
2015-12-24 10:12:23 +01:00
|
|
|
|
2015-09-23 10:02:01 +02:00
|
|
|
# Include default devise modules. Others available are:
|
|
|
|
# :confirmable, :lockable, :timeoutable and :omniauthable
|
2018-05-26 00:06:40 +02:00
|
|
|
devise :database_authenticatable, :registerable, :async,
|
2018-05-14 14:21:03 +02:00
|
|
|
:recoverable, :rememberable, :trackable, :validatable, :confirmable
|
2015-09-23 12:16:21 +02:00
|
|
|
|
2016-01-18 16:20:51 +01:00
|
|
|
has_many :dossiers, dependent: :destroy
|
2016-02-08 18:16:18 +01:00
|
|
|
has_many :invites, dependent: :destroy
|
2018-04-03 10:48:46 +02:00
|
|
|
has_many :dossiers_invites, through: :invites, source: :dossier
|
2016-03-17 17:33:38 +01:00
|
|
|
has_many :piece_justificative, dependent: :destroy
|
2016-01-21 17:06:09 +01:00
|
|
|
has_one :france_connect_information, dependent: :destroy
|
2015-10-06 11:21:20 +02:00
|
|
|
|
2016-01-21 17:06:09 +01:00
|
|
|
delegate :given_name, :family_name, :email_france_connect, :gender, :birthdate, :birthplace, :france_connect_particulier_id, to: :france_connect_information
|
|
|
|
accepts_nested_attributes_for :france_connect_information
|
2017-02-07 16:56:21 +01:00
|
|
|
|
2018-02-28 14:31:03 +01:00
|
|
|
before_validation -> { sanitize_email(:email) }
|
2016-10-11 10:31:32 +02:00
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
def self.find_for_france_connect(email, siret)
|
2018-03-06 13:44:29 +01:00
|
|
|
user = User.find_by(email: email)
|
2015-10-07 10:29:44 +02:00
|
|
|
if user.nil?
|
2015-12-24 10:12:23 +01:00
|
|
|
return User.create(email: email, password: Devise.friendly_token[0, 20], siret: siret)
|
2015-10-07 10:29:44 +02:00
|
|
|
else
|
2018-03-02 16:27:03 +01:00
|
|
|
user.update(siret: siret)
|
2015-10-07 10:29:44 +02:00
|
|
|
user
|
|
|
|
end
|
2015-10-06 11:21:20 +02:00
|
|
|
end
|
2015-12-24 10:12:23 +01:00
|
|
|
|
|
|
|
def loged_in_with_france_connect?
|
2018-01-11 19:08:04 +01:00
|
|
|
loged_in_with_france_connect.present?
|
2015-12-24 10:12:23 +01:00
|
|
|
end
|
2016-03-22 17:36:36 +01:00
|
|
|
|
2018-05-30 18:26:23 +02:00
|
|
|
def owns?(dossier)
|
|
|
|
dossier.user_id == id
|
|
|
|
end
|
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
def invite?(dossier_id)
|
2016-03-22 17:36:36 +01:00
|
|
|
invites.pluck(:dossier_id).include?(dossier_id.to_i)
|
|
|
|
end
|
2018-05-30 18:31:02 +02:00
|
|
|
|
|
|
|
def owns_or_invite?(dossier)
|
|
|
|
owns?(dossier) || invite?(dossier.id)
|
|
|
|
end
|
2015-09-23 10:02:01 +02:00
|
|
|
end
|