2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: administrateurs
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# active :boolean default(FALSE)
|
|
|
|
# encrypted_token :string
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2022-03-08 17:45:36 +01:00
|
|
|
# user_id :bigint
|
2020-08-06 16:35:45 +02:00
|
|
|
#
|
2018-03-06 13:44:29 +01:00
|
|
|
class Administrateur < ApplicationRecord
|
2018-08-24 16:45:43 +02:00
|
|
|
include ActiveRecord::SecureToken
|
2018-02-28 14:30:59 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
has_and_belongs_to_many :instructeurs
|
2022-02-23 17:04:24 +01:00
|
|
|
has_and_belongs_to_many :procedures
|
2018-04-17 16:11:49 +02:00
|
|
|
has_many :services
|
2015-12-14 17:28:36 +01:00
|
|
|
|
2019-08-19 10:47:37 +02:00
|
|
|
has_one :user, dependent: :nullify
|
2019-08-09 09:45:11 +02:00
|
|
|
|
2020-02-03 13:44:12 +01:00
|
|
|
default_scope { eager_load(:user) }
|
|
|
|
|
2019-10-07 15:39:35 +02:00
|
|
|
scope :inactive, -> { joins(:user).where(users: { last_sign_in_at: nil }) }
|
2019-12-18 13:28:29 +01:00
|
|
|
scope :with_publiees_ou_closes, -> { joins(:procedures).where(procedures: { aasm_state: [:publiee, :close, :depubliee] }) }
|
2018-01-11 14:17:50 +01:00
|
|
|
|
2020-01-30 17:08:09 +01:00
|
|
|
def self.by_email(email)
|
2020-02-03 13:44:12 +01:00
|
|
|
Administrateur.find_by(users: { email: email })
|
2020-01-30 17:08:09 +01:00
|
|
|
end
|
2020-02-03 11:07:53 +01:00
|
|
|
|
|
|
|
def email
|
2020-03-24 14:45:53 +01:00
|
|
|
user&.email
|
2020-02-03 11:07:53 +01:00
|
|
|
end
|
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
def self.find_inactive_by_token(reset_password_token)
|
|
|
|
self.inactive.with_reset_password_token(reset_password_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_inactive_by_id(id)
|
|
|
|
self.inactive.find(id)
|
|
|
|
end
|
|
|
|
|
2015-12-14 17:28:36 +01:00
|
|
|
def renew_api_token
|
2018-08-24 16:45:43 +02:00
|
|
|
api_token = Administrateur.generate_unique_secure_token
|
|
|
|
encrypted_token = BCrypt::Password.create(api_token)
|
2018-09-26 17:22:36 +02:00
|
|
|
update(encrypted_token: encrypted_token)
|
2018-08-24 16:45:43 +02:00
|
|
|
api_token
|
2015-12-14 17:28:36 +01:00
|
|
|
end
|
|
|
|
|
2018-09-26 15:39:45 +02:00
|
|
|
def valid_api_token?(api_token)
|
|
|
|
BCrypt::Password.new(encrypted_token) == api_token
|
|
|
|
rescue BCrypt::Errors::InvalidHash
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
def registration_state
|
2019-11-05 10:05:59 +01:00
|
|
|
if user.active?
|
2018-01-11 14:17:50 +01:00
|
|
|
'Actif'
|
2019-08-19 09:48:55 +02:00
|
|
|
elsif user.reset_password_period_valid?
|
2018-01-11 14:17:50 +01:00
|
|
|
'En attente'
|
|
|
|
else
|
|
|
|
'Expiré'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invitation_expired?
|
2019-11-05 10:05:59 +01:00
|
|
|
!user.active? && !user.reset_password_period_valid?
|
2018-01-11 14:17:50 +01:00
|
|
|
end
|
|
|
|
|
2018-05-17 15:39:37 +02:00
|
|
|
def owns?(procedure)
|
2019-02-26 16:18:04 +01:00
|
|
|
procedure.administrateurs.include?(self)
|
2018-05-17 15:39:37 +02:00
|
|
|
end
|
2019-01-07 15:11:55 +01:00
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
def instructeur
|
2019-10-15 17:44:59 +02:00
|
|
|
user.instructeur
|
2019-01-07 15:11:55 +01:00
|
|
|
end
|
2019-07-22 15:33:58 +02:00
|
|
|
|
|
|
|
def can_be_deleted?
|
2020-02-03 16:33:47 +01:00
|
|
|
procedures.all? { |p| p.administrateurs.count > 1 }
|
2019-07-22 15:33:58 +02:00
|
|
|
end
|
2020-01-30 10:48:28 +01:00
|
|
|
|
|
|
|
def delete_and_transfer_services
|
|
|
|
if !can_be_deleted?
|
2020-02-03 17:46:24 +01:00
|
|
|
fail "Impossible de supprimer cet administrateur car il a des démarches où il est le seul administrateur"
|
2020-01-30 10:48:28 +01:00
|
|
|
end
|
|
|
|
|
2020-04-10 17:41:51 +02:00
|
|
|
procedures.with_discarded.each do |procedure|
|
2020-01-30 10:48:28 +01:00
|
|
|
next_administrateur = procedure.administrateurs.where.not(id: self.id).first
|
|
|
|
procedure.service.update(administrateur: next_administrateur)
|
|
|
|
end
|
2020-03-16 17:07:39 +01:00
|
|
|
|
|
|
|
services.each do |service|
|
2020-04-10 17:41:51 +02:00
|
|
|
# We can't destroy a service if it has procedures, even if those procedures are archived
|
|
|
|
service.destroy unless service.procedures.with_discarded.any?
|
2020-03-16 17:07:39 +01:00
|
|
|
end
|
|
|
|
|
2020-01-30 10:48:28 +01:00
|
|
|
destroy
|
|
|
|
end
|
2020-03-26 16:17:07 +01:00
|
|
|
|
2021-10-18 13:10:35 +02:00
|
|
|
def merge(old_admin)
|
|
|
|
return if old_admin.nil?
|
|
|
|
|
|
|
|
procedures_with_new_admin, procedures_without_new_admin = old_admin.procedures
|
|
|
|
.partition { |p| p.administrateurs.exists?(id) }
|
|
|
|
|
|
|
|
procedures_with_new_admin.each do |p|
|
|
|
|
p.administrateurs.delete(old_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
procedures_without_new_admin.each do |p|
|
|
|
|
p.administrateurs << self
|
|
|
|
p.administrateurs.delete(old_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
old_admin.services.update_all(administrateur_id: id)
|
|
|
|
|
|
|
|
instructeurs_with_new_admin, instructeurs_without_new_admin = old_admin.instructeurs
|
|
|
|
.partition { |i| i.administrateurs.exists?(id) }
|
|
|
|
|
|
|
|
instructeurs_with_new_admin.each do |i|
|
|
|
|
i.administrateurs.delete(old_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
instructeurs_without_new_admin.each do |i|
|
|
|
|
i.administrateurs << self
|
|
|
|
i.administrateurs.delete(old_admin)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 16:17:07 +01:00
|
|
|
# required to display feature flags field in manager
|
|
|
|
def features
|
|
|
|
end
|
2015-10-23 16:19:55 +02:00
|
|
|
end
|