mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-22 16:26:21 +00:00
53 lines
1.3 KiB
Ruby
53 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'open-uri'
|
|
|
|
# Usuarie de la plataforma
|
|
class Usuarie < ApplicationRecord
|
|
devise :invitable, :database_authenticatable,
|
|
:recoverable, :rememberable, :validatable,
|
|
:confirmable, :lockable, :registerable
|
|
|
|
validates_uniqueness_of :email
|
|
|
|
has_many :roles
|
|
has_many :sites, through: :roles
|
|
|
|
before_create :register_in_lounge!,
|
|
if: proc { Rails.env.production? }
|
|
|
|
def name
|
|
email.split('@', 2).first
|
|
end
|
|
|
|
def rol_for_site(site)
|
|
site.roles.merge(roles).first
|
|
end
|
|
|
|
# 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
|
|
|
|
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
|
|
end
|