2018-03-06 13:44:29 +01:00
|
|
|
class Administrateur < ApplicationRecord
|
2019-09-17 10:16:52 +02:00
|
|
|
self.ignored_columns = ['features', 'encrypted_password', 'reset_password_token', 'reset_password_sent_at', 'remember_created_at', 'sign_in_count', 'current_sign_in_at', 'last_sign_in_at', 'current_sign_in_ip', 'last_sign_in_ip', 'failed_attempts', 'unlock_token', 'locked_at']
|
2018-02-28 14:30:59 +01:00
|
|
|
include EmailSanitizableConcern
|
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
|
2018-03-23 11:39:36 +01:00
|
|
|
has_many :administrateurs_procedures
|
2019-02-26 16:18:04 +01:00
|
|
|
has_many :procedures, through: :administrateurs_procedures
|
2018-04-17 16:11:49 +02:00
|
|
|
has_many :services
|
2018-09-07 18:17:51 +02:00
|
|
|
has_many :dossiers, -> { state_not_brouillon }, through: :procedures
|
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
|
|
|
|
2018-02-28 14:30:59 +01:00
|
|
|
before_validation -> { sanitize_email(:email) }
|
2017-02-07 16:56:21 +01:00
|
|
|
|
2018-01-11 14:17:50 +01:00
|
|
|
scope :inactive, -> { where(active: false) }
|
2019-03-12 15:34:30 +01:00
|
|
|
scope :with_publiees_ou_archivees, -> { joins(:procedures).where(procedures: { aasm_state: [:publiee, :archivee] }) }
|
2018-01-11 14:17:50 +01:00
|
|
|
|
2019-08-09 10:41:10 +02:00
|
|
|
# validate :password_complexity, if: Proc.new { |a| Devise.password_length.include?(a.password.try(:size)) }
|
2018-01-25 10:50:06 +01:00
|
|
|
|
|
|
|
def password_complexity
|
2019-06-20 00:30:49 +02:00
|
|
|
if password.present? && ZxcvbnService.new(password).score < PASSWORD_COMPLEXITY_FOR_ADMIN
|
|
|
|
errors.add(:password, :not_strong)
|
2018-01-25 10:50:06 +01:00
|
|
|
end
|
|
|
|
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
|
|
|
|
if active?
|
|
|
|
'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-08-19 09:48:55 +02:00
|
|
|
!active && !user.reset_password_period_valid?
|
2018-01-11 14:17:50 +01:00
|
|
|
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-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
|
|
|
|
Instructeur.find_by(email: email)
|
2019-01-07 15:11:55 +01:00
|
|
|
end
|
2019-07-22 15:33:58 +02:00
|
|
|
|
|
|
|
def can_be_deleted?
|
|
|
|
dossiers.state_instruction_commencee.none? && procedures.none?
|
|
|
|
end
|
2015-10-23 16:19:55 +02:00
|
|
|
end
|