2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
module ApplicationController::HandlesDevices
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2021-07-23 13:07:16 +00:00
|
|
|
before_action :user_device_log
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 13:07:16 +00:00
|
|
|
def user_device_log(user = current_user, type = 'session')
|
2017-03-09 11:44:51 +00:00
|
|
|
switched_from_user_id = ENV['SWITCHED_FROM_USER_ID'] || session[:switched_from_user_id]
|
2019-07-31 08:23:48 +00:00
|
|
|
return true if params[:controller] == 'init' # do no device logging on static initial page
|
2017-03-09 11:44:51 +00:00
|
|
|
return true if switched_from_user_id
|
2021-07-05 10:19:06 +00:00
|
|
|
return true if current_user_on_behalf # do no device logging for the user on behalf feature
|
2017-03-09 11:44:51 +00:00
|
|
|
return true if !user
|
2021-07-23 13:07:16 +00:00
|
|
|
return true if !policy(UserDevice).log?
|
2019-09-30 17:34:13 +00:00
|
|
|
return true if type == 'SSO'
|
2017-03-09 11:44:51 +00:00
|
|
|
|
|
|
|
time_to_check = true
|
|
|
|
user_device_updated_at = session[:user_device_updated_at]
|
|
|
|
if ENV['USER_DEVICE_UPDATED_AT']
|
|
|
|
user_device_updated_at = Time.zone.parse(ENV['USER_DEVICE_UPDATED_AT'])
|
|
|
|
end
|
|
|
|
|
|
|
|
if user_device_updated_at
|
|
|
|
# check if entry exists / only if write action
|
2022-01-03 09:47:32 +00:00
|
|
|
diff = 10.minutes.ago
|
2017-11-23 08:09:44 +00:00
|
|
|
if %w[GET OPTIONS HEAD].include?(request.method)
|
2022-01-03 09:47:32 +00:00
|
|
|
diff = 30.minutes.ago
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# only update if needed
|
|
|
|
if user_device_updated_at > diff
|
|
|
|
time_to_check = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# if ip has not changed and ttl in still valid
|
|
|
|
remote_ip = ENV['TEST_REMOTE_IP'] || request.remote_ip
|
2021-07-05 10:19:06 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
return true if time_to_check == false && session[:user_device_remote_ip] == remote_ip
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
session[:user_device_remote_ip] = remote_ip
|
|
|
|
|
|
|
|
# for sessions we need the fingperprint
|
|
|
|
if type == 'session'
|
|
|
|
if !session[:user_device_updated_at] && !params[:fingerprint] && !session[:user_device_fingerprint]
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('Need fingerprint param!')
|
2017-03-09 11:44:51 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-03-09 11:44:51 +00:00
|
|
|
if params[:fingerprint]
|
2018-04-12 11:23:48 +00:00
|
|
|
UserDevice.fingerprint_validation(params[:fingerprint])
|
2017-03-09 11:44:51 +00:00
|
|
|
session[:user_device_fingerprint] = params[:fingerprint]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
session[:user_device_updated_at] = Time.zone.now
|
|
|
|
|
|
|
|
# add device if needed
|
|
|
|
http_user_agent = ENV['HTTP_USER_AGENT'] || request.env['HTTP_USER_AGENT']
|
2018-12-12 12:51:29 +00:00
|
|
|
UserDeviceLogJob.perform_later(
|
|
|
|
http_user_agent,
|
|
|
|
remote_ip,
|
|
|
|
user.id,
|
|
|
|
session[:user_device_fingerprint],
|
|
|
|
type,
|
2017-03-09 11:44:51 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|