2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2021-04-22 07:17:01 +00:00
|
|
|
class SessionTimeoutJob < ApplicationJob
|
|
|
|
def perform
|
2021-06-11 12:17:25 +00:00
|
|
|
sessions.each do |session|
|
2021-04-22 07:17:01 +00:00
|
|
|
perform_session(session)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform_session(session)
|
|
|
|
|
2021-06-06 13:07:48 +00:00
|
|
|
# user is optional because it can be deleted already
|
2021-06-11 12:17:25 +00:00
|
|
|
if session.user?
|
|
|
|
return if session.active?
|
|
|
|
|
|
|
|
# if the user has no active sessions then we
|
|
|
|
# make sure to definitely log him out if there
|
|
|
|
# is any frontends opened
|
|
|
|
if !active_session(session.user)
|
|
|
|
session.frontend_timeout
|
|
|
|
end
|
2021-06-06 13:07:48 +00:00
|
|
|
end
|
2021-04-22 07:17:01 +00:00
|
|
|
|
|
|
|
session.destroy
|
|
|
|
end
|
|
|
|
|
2021-06-11 12:17:25 +00:00
|
|
|
def active_session(user)
|
|
|
|
@active_session ||= {}
|
|
|
|
return @active_session[user.id] if @active_session[user.id].present?
|
2021-04-22 07:17:01 +00:00
|
|
|
|
2021-06-24 15:37:59 +00:00
|
|
|
@active_session[user.id] = sessions.detect { |session| session.active? && session.user? && session.user.id == user.id }
|
2021-04-22 07:17:01 +00:00
|
|
|
end
|
|
|
|
|
2021-06-11 12:17:25 +00:00
|
|
|
def sessions
|
|
|
|
@sessions ||= ActiveRecord::SessionStore::Session.order(updated_at: :desc).limit(10_000).map { |session| SessionTimeoutJob::Session.new(session) }
|
2021-04-22 07:17:01 +00:00
|
|
|
end
|
|
|
|
end
|