Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -3,36 +3,78 @@
|
||||
#
|
||||
# Table name: session_activations
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# user_id :integer not null
|
||||
# session_id :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# id :integer not null, primary key
|
||||
# user_id :integer not null
|
||||
# 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 :integer
|
||||
#
|
||||
|
||||
class SessionActivation < ApplicationRecord
|
||||
LIMIT = Rails.configuration.x.max_session_activations
|
||||
belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy
|
||||
|
||||
def self.active?(id)
|
||||
id && where(session_id: id).exists?
|
||||
delegate :token,
|
||||
to: :access_token,
|
||||
allow_nil: true
|
||||
|
||||
def detection
|
||||
@detection ||= Browser.new(user_agent)
|
||||
end
|
||||
|
||||
def self.activate(id)
|
||||
activation = create!(session_id: id)
|
||||
purge_old
|
||||
activation
|
||||
def browser
|
||||
detection.id
|
||||
end
|
||||
|
||||
def self.deactivate(id)
|
||||
return unless id
|
||||
where(session_id: id).destroy_all
|
||||
def platform
|
||||
detection.platform.id
|
||||
end
|
||||
|
||||
def self.purge_old
|
||||
order('created_at desc').offset(LIMIT).destroy_all
|
||||
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
|
||||
|
||||
def self.exclusive(id)
|
||||
where('session_id != ?', id).destroy_all
|
||||
private
|
||||
|
||||
def assign_user_agent
|
||||
self.user_agent = '' if user_agent.nil?
|
||||
end
|
||||
|
||||
def assign_access_token
|
||||
superapp = Doorkeeper::Application.find_by(superapp: true)
|
||||
|
||||
return if superapp.nil?
|
||||
|
||||
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
|
||||
|
||||
@@ -91,8 +91,10 @@ class User < ApplicationRecord
|
||||
settings.auto_play_gif
|
||||
end
|
||||
|
||||
def activate_session
|
||||
session_activations.activate(SecureRandom.hex).session_id
|
||||
def activate_session(request)
|
||||
session_activations.activate(session_id: SecureRandom.hex,
|
||||
user_agent: request.user_agent,
|
||||
ip: request.ip).session_id
|
||||
end
|
||||
|
||||
def exclusive_session(id)
|
||||
|
||||
Reference in New Issue
Block a user