From 62e266fbd6231bd8083df774a93e88155d3f0bd9 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 19 Mar 2024 11:39:14 -0400 Subject: [PATCH] Add `BrowserDetection` model concern (#29513) --- app/models/concerns/browser_detection.rb | 27 ++++++++++++++++++++++++ app/models/login_activity.rb | 14 ++---------- app/models/session_activation.rb | 19 ++--------------- 3 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 app/models/concerns/browser_detection.rb diff --git a/app/models/concerns/browser_detection.rb b/app/models/concerns/browser_detection.rb new file mode 100644 index 0000000000..d42d0a5c99 --- /dev/null +++ b/app/models/concerns/browser_detection.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module BrowserDetection + extend ActiveSupport::Concern + + included do + before_save :assign_user_agent + end + + def detection + @detection ||= Browser.new(user_agent) + end + + def browser + detection.id + end + + def platform + detection.platform.id + end + + private + + def assign_user_agent + self.user_agent ||= '' + end +end diff --git a/app/models/login_activity.rb b/app/models/login_activity.rb index 654dd623ad..240d571c0e 100644 --- a/app/models/login_activity.rb +++ b/app/models/login_activity.rb @@ -16,21 +16,11 @@ # class LoginActivity < ApplicationRecord + include BrowserDetection + enum :authentication_method, { password: 'password', otp: 'otp', webauthn: 'webauthn', sign_in_token: 'sign_in_token', omniauth: 'omniauth' } belongs_to :user validates :authentication_method, inclusion: { in: authentication_methods.keys } - - def detection - @detection ||= Browser.new(user_agent) - end - - def browser - detection.id - end - - def platform - detection.platform.id - end end diff --git a/app/models/session_activation.rb b/app/models/session_activation.rb index c67180d3ba..d0a77daf0a 100644 --- a/app/models/session_activation.rb +++ b/app/models/session_activation.rb @@ -16,6 +16,8 @@ # class SessionActivation < ApplicationRecord + include BrowserDetection + belongs_to :user, inverse_of: :session_activations belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy, optional: true belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy, optional: true @@ -24,19 +26,6 @@ class SessionActivation < ApplicationRecord to: :access_token, allow_nil: true - def detection - @detection ||= Browser.new(user_agent) - end - - def browser - detection.id - end - - def platform - detection.platform.id - end - - before_save :assign_user_agent before_create :assign_access_token class << self @@ -67,10 +56,6 @@ class SessionActivation < ApplicationRecord private - def assign_user_agent - self.user_agent = '' if user_agent.nil? - end - def assign_access_token self.access_token = Doorkeeper::AccessToken.create!(access_token_attributes) end