9613a53cb3
* Update annotate to version 2.7.3 * Update aws-sdk-s3 to version 1.9.2 * Update browser to version 2.5.3 * Update capistrano to version 3.10.2 * Update domain_name to version 0.5.20180417 * Update http to version 3.2.0 * Update lograge to version 0.10.0 * Update oj to version 3.5.1 * Update parallel_tests to version 2.21.3 * Update puma to version 3.11.4 * Update rubocop to version 0.55.0 * Update scss_lint to version 0.57.0 * Update simplecov to version 0.16.1 * Update tty-command to version 0.8.0 * Update tty-prompt to version 0.16.0 * Update pkg-config to version 1.3.0 * Update fog-local to version 0.5.0 * Update fog-openstack to version 0.1.25 * Update devise-two-factor to version 3.0.3 * bundle update
81 lines
2.3 KiB
Ruby
81 lines
2.3 KiB
Ruby
# frozen_string_literal: true
|
|
# == Schema Information
|
|
#
|
|
# Table name: session_activations
|
|
#
|
|
# id :bigint(8) not null, primary key
|
|
# session_id :string not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# user_agent :string default(""), not null
|
|
# ip :inet
|
|
# access_token_id :bigint(8)
|
|
# user_id :bigint(8) not null
|
|
# web_push_subscription_id :bigint(8)
|
|
#
|
|
|
|
class SessionActivation < ApplicationRecord
|
|
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
|
|
|
|
delegate :token,
|
|
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_create :assign_access_token
|
|
before_save :assign_user_agent
|
|
|
|
class << self
|
|
def active?(id)
|
|
id && where(session_id: id).exists?
|
|
end
|
|
|
|
def activate(**options)
|
|
activation = create!(options)
|
|
purge_old
|
|
activation
|
|
end
|
|
|
|
def deactivate(id)
|
|
return unless id
|
|
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)
|
|
where('session_id != ?', id).destroy_all
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def assign_user_agent
|
|
self.user_agent = '' if user_agent.nil?
|
|
end
|
|
|
|
def assign_access_token
|
|
superapp = Doorkeeper::Application.find_by(superapp: true)
|
|
|
|
self.access_token = Doorkeeper::AccessToken.create!(application_id: superapp&.id,
|
|
resource_owner_id: user_id,
|
|
scopes: 'read write follow',
|
|
expires_in: Doorkeeper.configuration.access_token_expires_in,
|
|
use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?)
|
|
end
|
|
end
|