demarches-normaliennes/app/models/user.rb

122 lines
2.9 KiB
Ruby
Raw Normal View History

2018-03-06 13:44:29 +01:00
class User < ApplicationRecord
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
2018-05-26 00:06:40 +02:00
devise :database_authenticatable, :registerable, :async,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable
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
2018-08-08 17:37:41 +02:00
has_many :feedbacks, dependent: :destroy
has_one :france_connect_information, dependent: :destroy
2019-08-08 16:49:40 +02:00
belongs_to :instructeur
2019-08-09 09:45:11 +02:00
belongs_to :administrateur
accepts_nested_attributes_for :france_connect_information
2017-02-07 16:56:21 +01:00
default_scope { eager_load(:instructeur, :administrateur) }
before_validation -> { sanitize_email(:email) }
# Callback provided by Devise
def after_confirmation
link_invites!
end
2018-05-30 18:26:23 +02:00
def owns?(dossier)
dossier.user_id == id
end
def invite?(dossier_id)
invites.pluck(:dossier_id).include?(dossier_id.to_i)
end
def owns_or_invite?(dossier)
owns?(dossier) || invite?(dossier.id)
end
2019-08-07 15:52:38 +02:00
def invite!
UserMailer.invite_instructeur(self, set_reset_password_token).deliver_later
end
2019-08-09 11:41:36 +02:00
def invite_administrateur!(administration_id)
reset_password_token = nil
2019-11-05 10:05:59 +01:00
if !active?
reset_password_token = set_reset_password_token
2019-08-09 11:41:36 +02:00
end
AdministrationMailer.invite_admin(self, reset_password_token, administration_id).deliver_later
end
def remind_invitation!
reset_password_token = set_reset_password_token
AdministrateurMailer.activate_before_expiration(self, reset_password_token).deliver_later
end
2019-08-09 11:41:36 +02:00
def self.create_or_promote_to_instructeur(email, password, administrateurs: [])
user = User
.create_with(password: password, confirmed_at: Time.zone.now)
.find_or_create_by(email: email)
if user.valid?
if user.instructeur_id.nil?
user.create_instructeur!
end
user.instructeur.administrateurs << administrateurs
end
user
end
def self.create_or_promote_to_administrateur(email, password)
user = User.create_or_promote_to_instructeur(email, password)
if user.valid? && user.administrateur_id.nil?
user.create_administrateur!(email: email)
end
user
end
2019-07-04 12:36:17 +02:00
def flipper_id
"User:#{id}"
end
2019-11-05 10:01:07 +01:00
def active?
last_sign_in_at.present?
end
2020-01-06 17:33:09 +01:00
def can_be_deleted?
dossiers.state_instruction_commencee.empty?
2020-01-06 17:33:09 +01:00
end
2020-01-08 10:50:16 +01:00
def delete_and_keep_track_dossiers(administration)
if !can_be_deleted?
raise "Cannot delete this user because instruction has started for some dossiers"
end
if can_be_deleted?
dossiers.each do |dossier|
dossier.delete_and_keep_track(administration)
end
destroy
2020-01-08 10:50:16 +01:00
end
end
private
def link_invites!
Invite.where(email: email).update_all(user_id: id)
end
2015-09-23 10:02:01 +02:00
end