2014-01-10 16:35:58 +01:00
|
|
|
require 'digest/md5'
|
|
|
|
|
2014-08-06 14:47:47 +02:00
|
|
|
# Moderators, but using a failed pwd mechanism
|
|
|
|
# TODO, migrate to full active_admin
|
2017-05-27 09:34:24 +02:00
|
|
|
class User < ApplicationRecord
|
2014-01-10 16:35:58 +01:00
|
|
|
# Include default devise modules. Others available are:
|
|
|
|
# :confirmable, :lockable, :timeoutable and :omniauthable
|
2019-05-10 15:33:19 +02:00
|
|
|
devise :database_authenticatable,
|
|
|
|
:recoverable, :rememberable, :trackable, :validatable,
|
|
|
|
authentication_keys: [:login]
|
2014-01-10 16:35:58 +01:00
|
|
|
|
2014-09-03 02:14:21 +02:00
|
|
|
validates :login, presence: true
|
|
|
|
|
2014-01-10 16:35:58 +01:00
|
|
|
def encrypted_password=(pass)
|
2014-08-06 14:47:47 +02:00
|
|
|
self[:password] = pass
|
2014-01-10 16:35:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def encrypted_password
|
2014-08-06 14:47:47 +02:00
|
|
|
self[:password]
|
2014-01-10 16:35:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_first_by_auth_conditions(warden_conditions)
|
|
|
|
conditions = warden_conditions.dup
|
2014-08-06 14:47:47 +02:00
|
|
|
login = conditions.delete(:login)
|
|
|
|
if login.present?
|
2016-10-15 16:39:15 +02:00
|
|
|
where(conditions).find_by login: login
|
2014-01-10 16:35:58 +01:00
|
|
|
else
|
2019-05-10 15:33:19 +02:00
|
|
|
where(conditions).limit(1).to_a[0]
|
2014-01-10 16:35:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_password?(password)
|
|
|
|
encrypted_password == password_digest(password)
|
|
|
|
end
|
|
|
|
|
2014-06-09 12:18:40 +02:00
|
|
|
def to_s
|
2020-01-11 18:28:07 +01:00
|
|
|
if firstname.present? || lastname.present?
|
|
|
|
[firstname, lastname].join ' '
|
|
|
|
else
|
|
|
|
login
|
|
|
|
end
|
2014-06-09 12:18:40 +02:00
|
|
|
end
|
|
|
|
|
2014-01-10 16:35:58 +01:00
|
|
|
protected
|
2014-08-06 14:47:47 +02:00
|
|
|
|
|
|
|
def password_digest(password)
|
|
|
|
Digest::MD5.hexdigest password
|
|
|
|
end
|
2013-11-14 10:54:09 +01:00
|
|
|
end
|