demarches-normaliennes/app/models/user.rb

45 lines
1.4 KiB
Ruby
Raw Normal View History

2018-03-06 13:44:29 +01:00
class User < ApplicationRecord
include CredentialsSyncableConcern
include EmailSanitizableConcern
enum loged_in_with_france_connect: {
particulier: 'particulier',
entreprise: 'entreprise'
}
2015-09-23 10:02:01 +02:00
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
2018-05-14 14:21:03 +02:00
:recoverable, :rememberable, :trackable, :validatable, :confirmable
2015-09-23 12:16:21 +02:00
has_many :dossiers, dependent: :destroy
has_many :invites, dependent: :destroy
has_many :dossiers_invites, through: :invites, source: :dossier
has_many :piece_justificative, dependent: :destroy
has_many :cerfa, dependent: :destroy
has_one :france_connect_information, dependent: :destroy
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
before_validation -> { sanitize_email(:email) }
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?
return User.create(email: email, password: Devise.friendly_token[0, 20], siret: siret)
2015-10-07 10:29:44 +02:00
else
user.update(siret: siret)
2015-10-07 10:29:44 +02:00
user
end
end
def loged_in_with_france_connect?
2018-01-11 19:08:04 +01:00
loged_in_with_france_connect.present?
end
def invite?(dossier_id)
invites.pluck(:dossier_id).include?(dossier_id.to_i)
end
2015-09-23 10:02:01 +02:00
end