handles_devices.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ApplicationController::HandlesDevices
  3. extend ActiveSupport::Concern
  4. included do
  5. before_action :user_device_log
  6. end
  7. def user_device_log(user = current_user, type = 'session')
  8. switched_from_user_id = ENV['SWITCHED_FROM_USER_ID'] || session[:switched_from_user_id]
  9. return true if %w[init mobile].include?(params[:controller]) # do no device logging on static initial page
  10. return true if switched_from_user_id
  11. return true if current_user_on_behalf # do no device logging for the user on behalf feature
  12. return true if !user
  13. return true if !policy(UserDevice).log?
  14. return true if type == 'SSO'
  15. time_to_check = true
  16. user_device_updated_at = session[:user_device_updated_at]
  17. if ENV['USER_DEVICE_UPDATED_AT']
  18. user_device_updated_at = Time.zone.parse(ENV['USER_DEVICE_UPDATED_AT'])
  19. end
  20. if user_device_updated_at
  21. # check if entry exists / only if write action
  22. diff = 10.minutes.ago
  23. if %w[GET OPTIONS HEAD].include?(request.method)
  24. diff = 30.minutes.ago
  25. end
  26. # only update if needed
  27. if user_device_updated_at > diff
  28. time_to_check = false
  29. end
  30. end
  31. # if ip has not changed and ttl in still valid
  32. remote_ip = ENV['TEST_REMOTE_IP'] || request.remote_ip
  33. return true if time_to_check == false && session[:user_device_remote_ip] == remote_ip
  34. session[:user_device_remote_ip] = remote_ip
  35. # for sessions we need the fingperprint
  36. if type == 'session'
  37. fingerprint = params[:fingerprint] || request.headers['X-Browser-Fingerprint']
  38. if !session[:user_device_updated_at] && !fingerprint && !session[:user_device_fingerprint]
  39. raise Exceptions::UnprocessableEntity, __('Need fingerprint param!')
  40. end
  41. if fingerprint
  42. UserDevice.fingerprint_validation(fingerprint)
  43. session[:user_device_fingerprint] = fingerprint
  44. end
  45. end
  46. session[:user_device_updated_at] = Time.zone.now
  47. # add device if needed
  48. http_user_agent = ENV['HTTP_USER_AGENT'] || request.env['HTTP_USER_AGENT']
  49. UserDeviceLogJob.perform_later(
  50. http_user_agent,
  51. remote_ip,
  52. user.id,
  53. session[:user_device_fingerprint],
  54. type,
  55. )
  56. end
  57. end