session.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class SessionTimeoutJob::Session
  3. attr_accessor :session, :user
  4. def initialize(session)
  5. @session = session
  6. @user = User.find_by(id: session.data['user_id'])
  7. end
  8. def user?
  9. user.present?
  10. end
  11. def active?
  12. return true if timeout < 1
  13. return true if session.data['ping'] > timeout.seconds.ago
  14. end
  15. def frontend_timeout
  16. return if !user?
  17. PushMessages.send_to(user.id, { event: 'session_timeout' })
  18. end
  19. def timeout
  20. return -1 if !user?
  21. timeout_user
  22. end
  23. def timeout_user
  24. @timeout_user ||= begin
  25. permissions = user.permissions_with_child_names
  26. result = -1
  27. config.each do |key, value|
  28. next if key == 'default'
  29. next if permissions.exclude?(key)
  30. next if value.to_i < result
  31. result = value.to_i
  32. end
  33. if result < 1
  34. result = config['default'].to_i
  35. end
  36. result
  37. end
  38. end
  39. def config
  40. Setting.get('session_timeout')
  41. end
  42. def destroy
  43. session.destroy
  44. Rails.logger.info "SessionTimeoutJob removed session '#{session.id}' for user id '#{user&.id}' (last ping: '#{session.data['ping']}', timeout: '#{timeout.seconds}')"
  45. end
  46. end