2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AuthenticationEvent < ApplicationRecord
|
2021-01-03 14:25:43 +05:30
|
|
|
include UsageStatistics
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
TWO_FACTOR = 'two-factor'
|
|
|
|
TWO_FACTOR_U2F = 'two-factor-via-u2f-device'
|
|
|
|
TWO_FACTOR_WEBAUTHN = 'two-factor-via-webauthn-device'
|
|
|
|
STANDARD = 'standard'
|
2021-01-29 00:20:46 +05:30
|
|
|
STATIC_PROVIDERS = [TWO_FACTOR, TWO_FACTOR_U2F, TWO_FACTOR_WEBAUTHN, STANDARD].freeze
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
belongs_to :user, optional: true
|
|
|
|
|
|
|
|
validates :provider, :user_name, :result, presence: true
|
2021-01-03 14:25:43 +05:30
|
|
|
validates :ip_address, ip_address: true
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
enum result: {
|
|
|
|
failed: 0,
|
|
|
|
success: 1
|
|
|
|
}
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
scope :for_provider, ->(provider) { where(provider: provider) }
|
2022-08-27 11:52:29 +05:30
|
|
|
scope :ldap, -> { where('provider LIKE ?', 'ldap%') }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
def self.providers
|
2021-01-29 00:20:46 +05:30
|
|
|
STATIC_PROVIDERS | Devise.omniauth_providers.map(&:to_s)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2022-08-13 15:12:31 +05:30
|
|
|
|
|
|
|
def self.initial_login_or_known_ip_address?(user, ip_address)
|
|
|
|
!where(user_id: user).exists? ||
|
|
|
|
where(user_id: user, ip_address: ip_address).success.exists?
|
|
|
|
end
|
2023-07-09 08:55:56 +05:30
|
|
|
|
|
|
|
def self.most_used_ip_address_for_user(user)
|
|
|
|
select('mode() within group (order by ip_address) as ip_address').find_by(user: user).ip_address
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|