2019-07-03 19:59:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-09-11 16:31:14 +00:00
|
|
|
require 'open-uri'
|
|
|
|
|
2019-07-03 19:33:26 +00:00
|
|
|
# Usuarie de la plataforma
|
|
|
|
class Usuarie < ApplicationRecord
|
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
|
2019-07-03 22:04:50 +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
|
2019-07-08 17:55:19 +00:00
|
|
|
|
2019-09-11 16:31:14 +00:00
|
|
|
after_create :register_in_lounge!, if: Rails.env.production?
|
|
|
|
|
2019-07-16 19:47:44 +00:00
|
|
|
def name
|
|
|
|
email.split('@', 2).first
|
|
|
|
end
|
|
|
|
|
2019-07-08 17:55:19 +00:00
|
|
|
def rol_for_site(site)
|
|
|
|
site.roles.merge(roles).first
|
|
|
|
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
|
2019-09-11 16:31:14 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# XXX: Tenemos que hacer esto en línea porque necesitamos la
|
|
|
|
# contraseña y no la queremos dejar registrada por error en ningún
|
|
|
|
# lado
|
|
|
|
def register_in_lounge!
|
|
|
|
Thread.new do
|
|
|
|
open("https://chat.#{Site.domain}/#{ENV['LOUNGE']}/#{email}/#{password}")
|
|
|
|
end
|
|
|
|
end
|
2019-07-03 19:33:26 +00:00
|
|
|
end
|