2019-07-03 19:59:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-03 19:33:26 +00:00
|
|
|
# Usuarie de la plataforma
|
|
|
|
class Usuarie < ApplicationRecord
|
2023-03-28 21:18:53 +00:00
|
|
|
include Usuarie::Consent
|
|
|
|
|
2019-07-05 23:55:59 +00:00
|
|
|
devise :invitable, :database_authenticatable,
|
2019-07-03 19:59:21 +00:00
|
|
|
:recoverable, :rememberable, :validatable,
|
|
|
|
:confirmable, :lockable, :registerable
|
|
|
|
|
|
|
|
validates_uniqueness_of :email
|
2021-02-24 20:25:37 +00:00
|
|
|
validates_with EmailAddress::ActiveRecordValidator, field: :email
|
2023-05-24 13:04:22 +00:00
|
|
|
validate :locale_available!
|
2019-07-03 22:04:50 +00:00
|
|
|
|
2022-08-27 22:30:52 +00:00
|
|
|
before_create :lang_from_locale!
|
2023-04-24 21:07:57 +00:00
|
|
|
before_update :remove_confirmation_invitation_inconsistencies!
|
2023-04-24 20:03:38 +00:00
|
|
|
before_update :accept_invitation_after_confirmation!
|
2022-08-27 22:30:52 +00:00
|
|
|
|
2019-07-06 00:21:49 +00:00
|
|
|
has_many :roles
|
2019-07-08 16:40:41 +00:00
|
|
|
has_many :sites, through: :roles
|
2021-08-02 20:51:35 +00:00
|
|
|
has_many :blazer_audits, foreign_key: 'user_id', class_name: 'Blazer::Audit'
|
|
|
|
has_many :blazer_queries, foreign_key: 'creator_id', class_name: 'Blazer::Query'
|
2019-07-08 17:55:19 +00:00
|
|
|
|
2019-07-16 19:47:44 +00:00
|
|
|
def name
|
|
|
|
email.split('@', 2).first
|
|
|
|
end
|
|
|
|
|
2020-05-12 14:58:07 +00:00
|
|
|
# Encuentra el rol que tiene le usuarie en el sitio
|
2019-07-08 17:55:19 +00:00
|
|
|
def rol_for_site(site)
|
2020-05-12 14:58:07 +00:00
|
|
|
roles.find_by(site: site)
|
2019-07-08 17:55:19 +00:00
|
|
|
end
|
2019-07-08 20:58:14 +00:00
|
|
|
|
|
|
|
# XXX: Ver increment_and_lock
|
|
|
|
def can_sign_in?(password)
|
|
|
|
active_for_authentication? && valid_password?(password)
|
|
|
|
end
|
|
|
|
|
|
|
|
# XXX: Estamos duplicando la forma en que Devise bloquea acceso
|
|
|
|
# por intentos fallidos porque no tenemos forma de correr
|
|
|
|
# validate() desde la estrategia DatabaseAuthenticatable sin
|
|
|
|
# generar una redirección.
|
|
|
|
#
|
|
|
|
# lib/devise/models/lockable.rb
|
|
|
|
def increment_and_lock!
|
|
|
|
increment_failed_attempts
|
|
|
|
lock_access! if attempts_exceeded? && !access_locked?
|
|
|
|
end
|
2022-08-27 22:30:52 +00:00
|
|
|
|
2023-04-04 14:33:51 +00:00
|
|
|
def send_devise_notification(notification, *args)
|
|
|
|
I18n.with_locale(lang) do
|
|
|
|
devise_mailer.send(notification, self, *args).deliver_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-24 20:21:24 +00:00
|
|
|
# Les usuaries necesitan link de invitación si no tenían cuenta
|
2023-04-24 20:44:04 +00:00
|
|
|
# y todavía no aceptaron la invitación anterior.
|
2023-04-24 20:21:24 +00:00
|
|
|
def needs_invitation_link?
|
2023-04-24 20:44:04 +00:00
|
|
|
created_by_invite? && !invitation_accepted?
|
2023-04-24 20:21:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-27 22:30:52 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def lang_from_locale!
|
|
|
|
self.lang = I18n.locale.to_s
|
|
|
|
end
|
2023-04-24 20:03:38 +00:00
|
|
|
|
2023-04-24 21:07:57 +00:00
|
|
|
# El invitation_token solo es necesario cuando fue creade por otre
|
|
|
|
# usuarie. De lo contrario lo que queremos es un proceso de
|
|
|
|
# confirmación.
|
|
|
|
def remove_confirmation_invitation_inconsistencies!
|
|
|
|
self.invitation_token = nil unless created_by_invite?
|
|
|
|
end
|
|
|
|
|
2023-04-24 20:03:38 +00:00
|
|
|
# Si le usuarie (re)confirma su cuenta con una invitación pendiente,
|
|
|
|
# considerarla aceptada también.
|
|
|
|
def accept_invitation_after_confirmation!
|
|
|
|
if confirmed?
|
|
|
|
self.invitation_token = nil
|
|
|
|
self.invitation_accepted_at ||= Time.now.utc
|
|
|
|
end
|
|
|
|
end
|
2023-05-24 13:04:22 +00:00
|
|
|
|
|
|
|
# Muestra un error si el idioma no está disponible al cambiar el
|
|
|
|
# idioma de la cuenta.
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
def locale_available!
|
|
|
|
return if I18n.locale_available? self.lang
|
|
|
|
|
|
|
|
errors.add(:lang, I18n.t('activerecord.errors.models.usuarie.attributes.lang.not_available'))
|
|
|
|
nil
|
|
|
|
end
|
2019-07-03 19:33:26 +00:00
|
|
|
end
|