2018-11-29 15:57:57 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class User < ApplicationRecord
|
2019-10-18 00:31:05 -04:00
|
|
|
MIN_PASSWORD_LENGTH = 8
|
|
|
|
MAX_PASSWORD_LENGTH = 128
|
|
|
|
|
2018-11-29 15:57:57 -05:00
|
|
|
devise(
|
2018-12-03 12:58:39 -05:00
|
|
|
:confirmable,
|
2018-11-29 15:57:57 -05:00
|
|
|
:database_authenticatable,
|
2018-12-03 15:19:41 -05:00
|
|
|
:lockable,
|
2018-12-03 21:28:16 -05:00
|
|
|
:omniauthable,
|
2018-12-03 15:03:01 -05:00
|
|
|
:recoverable,
|
2018-11-29 15:57:57 -05:00
|
|
|
:registerable,
|
|
|
|
:rememberable,
|
2018-12-03 21:12:07 -05:00
|
|
|
:timeoutable,
|
2018-11-29 15:57:57 -05:00
|
|
|
:trackable,
|
|
|
|
:validatable,
|
2018-12-03 21:28:16 -05:00
|
|
|
omniauth_providers: %i[github],
|
2018-11-29 15:57:57 -05:00
|
|
|
)
|
2018-12-01 21:50:10 -05:00
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
################
|
|
|
|
# Associations #
|
|
|
|
################
|
|
|
|
|
2018-12-01 21:50:10 -05:00
|
|
|
belongs_to :account
|
|
|
|
|
2019-07-20 06:25:23 -04:00
|
|
|
has_many :user_omniauths
|
2018-12-03 22:08:33 -05:00
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###############
|
|
|
|
# Validations #
|
|
|
|
###############
|
|
|
|
|
2019-03-23 21:42:27 -04:00
|
|
|
validates :account, uniqueness: true
|
2018-12-01 21:50:10 -05:00
|
|
|
|
2019-09-04 17:38:59 -04:00
|
|
|
###########
|
|
|
|
# Methods #
|
|
|
|
###########
|
|
|
|
|
2019-09-09 18:17:13 -04:00
|
|
|
def active_for_authentication?
|
|
|
|
super && (
|
|
|
|
!Rails.env.production? ||
|
|
|
|
Rails.application.restricted? ||
|
|
|
|
!account.restricted?
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-04 17:38:59 -04:00
|
|
|
def remember_exists_and_not_expired?
|
|
|
|
return false unless respond_to? :remember_created_at
|
|
|
|
return false unless respond_to? :remember_expired?
|
|
|
|
|
|
|
|
remember_created_at && !remember_expired?
|
|
|
|
end
|
|
|
|
|
|
|
|
def remember_expired?
|
|
|
|
remember_created_at.nil? || (remember_expires_at <= Time.now.utc)
|
|
|
|
end
|
|
|
|
|
|
|
|
def timedout?(last_access)
|
|
|
|
return false if remember_exists_and_not_expired?
|
|
|
|
|
|
|
|
!timeout_in.nil? && last_access && last_access <= timeout_in.ago
|
|
|
|
end
|
2018-11-29 15:57:57 -05:00
|
|
|
end
|