session_timeout_job.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class SessionTimeoutJob < ApplicationJob
  3. def perform
  4. sessions.each do |session|
  5. perform_session(session)
  6. end
  7. end
  8. def perform_session(session)
  9. # user is optional because it can be deleted already
  10. if session.user?
  11. return if session.active?
  12. # if the user has no active sessions then we
  13. # make sure to definitely log him out if there
  14. # is any frontends opened
  15. if !active_session(session.user)
  16. session.frontend_timeout
  17. end
  18. end
  19. session.destroy
  20. end
  21. def active_session(user)
  22. @active_session ||= {}
  23. return @active_session[user.id] if @active_session[user.id].present?
  24. @active_session[user.id] = sessions.detect { |session| session.active? && session.user? && session.user.id == user.id }
  25. end
  26. def sessions
  27. @sessions ||= ActiveRecord::SessionStore::Session.order(updated_at: :desc).limit(10_000).map { |session| SessionTimeoutJob::Session.new(session) }
  28. end
  29. end