From 2a295e78ab5ccf77f4203a8792928dbb2c0b062a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 11 Apr 2016 09:35:27 +0200 Subject: [PATCH] Improved user device management. --- app/controllers/application_controller.rb | 6 ++- app/models/user_device.rb | 18 ++++----- lib/service/geo_ip/zammad.rb | 6 +-- test/unit/user_device_test.rb | 45 +++++++++++++++++++++++ 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 460211f5c..43af0e2d0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -123,12 +123,16 @@ class ApplicationController < ActionController::Base return if session[:user_device_update_at] && session[:user_device_update_at] > diff session[:user_device_update_at] = Time.zone.now - UserDevice.action( + user_device = UserDevice.action( session[:user_device_id], session[:user_agent], session[:remote_ip], session[:user_id], ) + + # remember if location has changed + return if !user_device + session[:user_device_id] = user_device.id end def user_device_log(user, type) diff --git a/app/models/user_device.rb b/app/models/user_device.rb index 4a05276d9..af6f96370 100644 --- a/app/models/user_device.rb +++ b/app/models/user_device.rb @@ -93,6 +93,7 @@ store new device for user if device not already known os: browser[:plattform], browser: browser[:name], location: location, + fingerprint: fingerprint, ) if user_device @@ -141,11 +142,10 @@ log user device action =end - def self.action(user_device_id, _user_agent, ip, _user_id) + def self.action(user_device_id, user_agent, ip, user_id) user_device = UserDevice.find(user_device_id) # update location if needed - notify = false if user_device.ip != ip user_device.ip = ip location_details = Service::GeoIp.location(ip) @@ -155,19 +155,19 @@ log user device action # notify if country has changed if user_device.location != location - notify = true - user_device.location = location + return UserDevice.add( + user_agent, + ip, + user_id, + user_device.fingerprint, + 'session', + ) end end # update attributes user_device.updated_at = Time.zone.now # force update, also if no other attribute has changed user_device.save - - if notify - user_device.notification_send('user_device_new_location') - end - user_device end diff --git a/lib/service/geo_ip/zammad.rb b/lib/service/geo_ip/zammad.rb index 7820a50f3..00925de50 100644 --- a/lib/service/geo_ip/zammad.rb +++ b/lib/service/geo_ip/zammad.rb @@ -7,7 +7,7 @@ class Service::GeoIp::Zammad # check cache cache_key = "zammadgeoip::#{address}" - cache = Cache.get( cache_key ) + cache = Cache.get(cache_key) return cache if cache # do lookup @@ -36,10 +36,10 @@ class Service::GeoIp::Zammad data['country_code'] = data['country_code2'] end - Cache.write( cache_key, data, { expires_in: 90.days } ) + Cache.write(cache_key, data, { expires_in: 90.days }) rescue => e Rails.logger.error "#{host}#{url}: #{e.inspect}" - Cache.write( cache_key, data, { expires_in: 60.minutes } ) + Cache.write(cache_key, data, { expires_in: 60.minutes }) end data end diff --git a/test/unit/user_device_test.rb b/test/unit/user_device_test.rb index d9fa244ab..44f58aff4 100644 --- a/test/unit/user_device_test.rb +++ b/test/unit/user_device_test.rb @@ -197,4 +197,49 @@ class UserDeviceTest < ActiveSupport::TestCase end + test 'ddd - api test' do + + # signin with fingerprint A from country A via session -> new device #1 + user_device1 = UserDevice.add( + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', + '91.115.248.231', + @agent.id, + 'fingerprint1234', + 'session', + ) + user_device1_1 = UserDevice.action( + user_device1.id, + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', + '91.115.248.231', + @agent.id, + ) + assert_equal(user_device1.id, user_device1_1.id) + user_device1_2 = UserDevice.add( + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', + '91.115.248.231', + @agent.id, + 'fingerprint1234', + 'session', + ) + assert_equal(user_device1.id, user_device1_2.id) + user_device1_3 = UserDevice.add( + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', + '91.115.248.231', + @agent.id, + 'fingerprintABC', + 'session', + ) + assert_not_equal(user_device1.id, user_device1_3.id) + + # log with fingerprint A from country B via session -> new device #2 + user_device2 = UserDevice.action( + user_device1.id, + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', + '176.198.137.254', + @agent.id, + ) + assert_not_equal(user_device1.id, user_device2.id) + + end + end