2017-06-24 02:50:53 +10:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2017-06-24 02:50:53 +10:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: session_activations
|
|
|
|
#
|
2018-04-23 19:29:17 +10:00
|
|
|
# id :bigint(8) not null, primary key
|
2017-07-14 06:15:32 +10:00
|
|
|
# session_id :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# user_agent :string default(""), not null
|
|
|
|
# ip :inet
|
2018-04-23 19:29:17 +10:00
|
|
|
# access_token_id :bigint(8)
|
|
|
|
# user_id :bigint(8) not null
|
|
|
|
# web_push_subscription_id :bigint(8)
|
2017-07-14 06:15:32 +10:00
|
|
|
#
|
|
|
|
|
2017-06-24 02:50:53 +10:00
|
|
|
class SessionActivation < ApplicationRecord
|
2024-03-20 02:39:14 +11:00
|
|
|
include BrowserDetection
|
|
|
|
|
2018-01-20 06:56:47 +11:00
|
|
|
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
|
2017-06-26 07:51:32 +10:00
|
|
|
|
|
|
|
delegate :token,
|
|
|
|
to: :access_token,
|
|
|
|
allow_nil: true
|
|
|
|
|
2023-05-03 02:23:35 +10:00
|
|
|
before_create :assign_access_token
|
2017-06-24 02:50:53 +10:00
|
|
|
|
2024-10-03 00:45:12 +10:00
|
|
|
DEFAULT_SCOPES = %w(read write follow).freeze
|
|
|
|
|
2017-06-26 00:54:30 +10:00
|
|
|
class << self
|
|
|
|
def active?(id)
|
2024-01-24 20:57:49 +11:00
|
|
|
id && exists?(session_id: id)
|
2017-06-26 00:54:30 +10:00
|
|
|
end
|
|
|
|
|
2017-12-06 21:41:57 +11:00
|
|
|
def activate(**options)
|
2021-05-06 22:22:54 +10:00
|
|
|
activation = create!(**options)
|
2017-06-26 00:54:30 +10:00
|
|
|
purge_old
|
|
|
|
activation
|
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate(id)
|
|
|
|
return unless id
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2017-06-26 00:54:30 +10:00
|
|
|
where(session_id: id).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def purge_old
|
|
|
|
order('created_at desc').offset(Rails.configuration.x.max_session_activations).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def exclusive(id)
|
2023-02-08 20:39:57 +11:00
|
|
|
where.not(session_id: id).destroy_all
|
2017-06-26 00:54:30 +10:00
|
|
|
end
|
2017-06-24 02:50:53 +10:00
|
|
|
end
|
2017-06-26 07:51:32 +10:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assign_access_token
|
2020-11-13 09:05:24 +11:00
|
|
|
self.access_token = Doorkeeper::AccessToken.create!(access_token_attributes)
|
|
|
|
end
|
2017-06-26 07:51:32 +10:00
|
|
|
|
2020-11-13 09:05:24 +11:00
|
|
|
def access_token_attributes
|
|
|
|
{
|
|
|
|
application_id: Doorkeeper::Application.find_by(superapp: true)&.id,
|
|
|
|
resource_owner_id: user_id,
|
2024-10-03 00:45:12 +10:00
|
|
|
scopes: DEFAULT_SCOPES.join(' '),
|
2020-11-13 09:05:24 +11:00
|
|
|
expires_in: Doorkeeper.configuration.access_token_expires_in,
|
|
|
|
use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?,
|
|
|
|
}
|
2017-06-26 07:51:32 +10:00
|
|
|
end
|
2017-06-24 02:50:53 +10:00
|
|
|
end
|