2018-03-06 13:44:29 +01:00
|
|
|
class Administrateur < ApplicationRecord
|
2018-02-28 14:30:59 +01:00
|
|
|
include CredentialsSyncableConcern
|
|
|
|
include EmailSanitizableConcern
|
|
|
|
|
2018-05-26 00:06:40 +02:00
|
|
|
devise :database_authenticatable, :registerable, :async,
|
2017-06-12 13:49:51 +02:00
|
|
|
:recoverable, :rememberable, :trackable, :validatable
|
2015-11-10 10:23:15 +01:00
|
|
|
|
2016-05-20 15:39:17 +02:00
|
|
|
has_and_belongs_to_many :gestionnaires
|
2015-11-10 10:23:15 +01:00
|
|
|
has_many :procedures
|
2018-03-23 11:39:36 +01:00
|
|
|
has_many :administrateurs_procedures
|
|
|
|
has_many :admin_procedures, through: :administrateurs_procedures, source: :procedure
|
2018-04-17 16:11:49 +02:00
|
|
|
has_many :services
|
2015-12-14 17:28:36 +01:00
|
|
|
|
2018-02-28 14:30:59 +01:00
|
|
|
before_validation -> { sanitize_email(:email) }
|
2015-12-14 17:28:36 +01:00
|
|
|
before_save :ensure_api_token
|
2017-02-07 16:56:21 +01:00
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
scope :inactive, -> { where(active: false) }
|
|
|
|
|
|
|
|
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 ensure_api_token
|
|
|
|
if api_token.nil?
|
|
|
|
self.api_token = generate_api_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def renew_api_token
|
2018-03-02 16:27:03 +01:00
|
|
|
update(api_token: generate_api_token)
|
2015-12-14 17:28:36 +01:00
|
|
|
end
|
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
def registration_state
|
|
|
|
if active?
|
|
|
|
'Actif'
|
|
|
|
elsif reset_password_period_valid?
|
|
|
|
'En attente'
|
|
|
|
else
|
|
|
|
'Expiré'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invite!
|
|
|
|
if active?
|
|
|
|
raise "Impossible d'inviter un utilisateur déjà actif !"
|
|
|
|
end
|
2018-01-15 22:13:52 +01:00
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
reset_password_token = set_reset_password_token
|
2018-01-15 22:13:52 +01:00
|
|
|
|
2018-05-25 18:05:28 +02:00
|
|
|
AdministrationMailer.invite_admin(self, reset_password_token).deliver_later
|
2018-01-11 14:17:50 +01:00
|
|
|
|
|
|
|
reset_password_token
|
|
|
|
end
|
|
|
|
|
2018-05-03 18:30:40 +02:00
|
|
|
def remind_invitation!
|
|
|
|
if active?
|
|
|
|
raise "Impossible d'envoyer un rappel d'invitation à un utilisateur déjà actif !"
|
|
|
|
end
|
|
|
|
|
|
|
|
reset_password_token = set_reset_password_token
|
|
|
|
|
|
|
|
AdministrateurMailer.activate_before_expiration(self, reset_password_token).deliver_later
|
|
|
|
end
|
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
def invitation_expired?
|
|
|
|
!active && !reset_password_period_valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset_password(reset_password_token, password)
|
|
|
|
administrateur = self.reset_password_by_token({
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password,
|
|
|
|
reset_password_token: reset_password_token
|
|
|
|
})
|
|
|
|
|
|
|
|
if administrateur && administrateur.errors.empty?
|
|
|
|
administrateur.update_column(:active, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
administrateur
|
|
|
|
end
|
|
|
|
|
2018-03-27 15:43:59 +02:00
|
|
|
def feature_enabled?(feature)
|
2018-04-18 12:16:25 +02:00
|
|
|
Flipflop.feature_set.feature(feature)
|
|
|
|
features[feature.to_s]
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_feature(feature)
|
|
|
|
Flipflop.feature_set.feature(feature)
|
|
|
|
features.delete(feature.to_s)
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_feature(feature)
|
|
|
|
Flipflop.feature_set.feature(feature)
|
|
|
|
features[feature.to_s] = true
|
|
|
|
save
|
2018-03-27 15:43:59 +02:00
|
|
|
end
|
|
|
|
|
2018-05-17 15:39:37 +02:00
|
|
|
def owns?(procedure)
|
|
|
|
id == procedure.administrateur_id
|
|
|
|
end
|
|
|
|
|
2015-12-14 17:28:36 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def generate_api_token
|
|
|
|
loop do
|
|
|
|
token = SecureRandom.hex(20)
|
2018-01-11 19:04:39 +01:00
|
|
|
break token if !Administrateur.find_by(api_token: token)
|
2015-12-14 17:28:36 +01:00
|
|
|
end
|
|
|
|
end
|
2015-10-23 16:19:55 +02:00
|
|
|
end
|