Improved user device management.
This commit is contained in:
parent
6b219b724b
commit
2a295e78ab
4 changed files with 62 additions and 13 deletions
|
@ -123,12 +123,16 @@ class ApplicationController < ActionController::Base
|
||||||
return if session[:user_device_update_at] && session[:user_device_update_at] > diff
|
return if session[:user_device_update_at] && session[:user_device_update_at] > diff
|
||||||
session[:user_device_update_at] = Time.zone.now
|
session[:user_device_update_at] = Time.zone.now
|
||||||
|
|
||||||
UserDevice.action(
|
user_device = UserDevice.action(
|
||||||
session[:user_device_id],
|
session[:user_device_id],
|
||||||
session[:user_agent],
|
session[:user_agent],
|
||||||
session[:remote_ip],
|
session[:remote_ip],
|
||||||
session[:user_id],
|
session[:user_id],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# remember if location has changed
|
||||||
|
return if !user_device
|
||||||
|
session[:user_device_id] = user_device.id
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_device_log(user, type)
|
def user_device_log(user, type)
|
||||||
|
|
|
@ -93,6 +93,7 @@ store new device for user if device not already known
|
||||||
os: browser[:plattform],
|
os: browser[:plattform],
|
||||||
browser: browser[:name],
|
browser: browser[:name],
|
||||||
location: location,
|
location: location,
|
||||||
|
fingerprint: fingerprint,
|
||||||
)
|
)
|
||||||
|
|
||||||
if user_device
|
if user_device
|
||||||
|
@ -141,11 +142,10 @@ log user device action
|
||||||
|
|
||||||
=end
|
=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)
|
user_device = UserDevice.find(user_device_id)
|
||||||
|
|
||||||
# update location if needed
|
# update location if needed
|
||||||
notify = false
|
|
||||||
if user_device.ip != ip
|
if user_device.ip != ip
|
||||||
user_device.ip = ip
|
user_device.ip = ip
|
||||||
location_details = Service::GeoIp.location(ip)
|
location_details = Service::GeoIp.location(ip)
|
||||||
|
@ -155,19 +155,19 @@ log user device action
|
||||||
|
|
||||||
# notify if country has changed
|
# notify if country has changed
|
||||||
if user_device.location != location
|
if user_device.location != location
|
||||||
notify = true
|
return UserDevice.add(
|
||||||
user_device.location = location
|
user_agent,
|
||||||
|
ip,
|
||||||
|
user_id,
|
||||||
|
user_device.fingerprint,
|
||||||
|
'session',
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# update attributes
|
# update attributes
|
||||||
user_device.updated_at = Time.zone.now # force update, also if no other attribute has changed
|
user_device.updated_at = Time.zone.now # force update, also if no other attribute has changed
|
||||||
user_device.save
|
user_device.save
|
||||||
|
|
||||||
if notify
|
|
||||||
user_device.notification_send('user_device_new_location')
|
|
||||||
end
|
|
||||||
|
|
||||||
user_device
|
user_device
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ class Service::GeoIp::Zammad
|
||||||
|
|
||||||
# check cache
|
# check cache
|
||||||
cache_key = "zammadgeoip::#{address}"
|
cache_key = "zammadgeoip::#{address}"
|
||||||
cache = Cache.get( cache_key )
|
cache = Cache.get(cache_key)
|
||||||
return cache if cache
|
return cache if cache
|
||||||
|
|
||||||
# do lookup
|
# do lookup
|
||||||
|
@ -36,10 +36,10 @@ class Service::GeoIp::Zammad
|
||||||
data['country_code'] = data['country_code2']
|
data['country_code'] = data['country_code2']
|
||||||
end
|
end
|
||||||
|
|
||||||
Cache.write( cache_key, data, { expires_in: 90.days } )
|
Cache.write(cache_key, data, { expires_in: 90.days })
|
||||||
rescue => e
|
rescue => e
|
||||||
Rails.logger.error "#{host}#{url}: #{e.inspect}"
|
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
|
end
|
||||||
data
|
data
|
||||||
end
|
end
|
||||||
|
|
|
@ -197,4 +197,49 @@ class UserDeviceTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue