Fixed issue #1208 - Too many emails about new client access if user agent is "".

This commit is contained in:
Martin Edenhofer 2017-06-27 08:07:43 +02:00
parent 3e92e4f9f4
commit 6a4e17a224
2 changed files with 53 additions and 13 deletions

View file

@ -21,8 +21,9 @@ store new device for user if device not already known
def self.add(user_agent, ip, user_id, fingerprint, type)
# since gem browser 2 is not handling nil for user_agent, set it to ''
user_agent ||= ''
if user_agent.blank?
user_agent = 'unknown'
end
# get location info
location_details = Service::GeoIp.location(ip)
@ -60,6 +61,8 @@ store new device for user if device not already known
end
# get browser details
browser = {}
if user_agent != 'unknown'
browser = Browser.new(user_agent, accept_language: 'en-us')
browser = {
plattform: browser.platform.to_s.camelize,
@ -67,16 +70,17 @@ store new device for user if device not already known
version: browser.version,
full_version: browser.full_version,
}
end
# generate device name
if browser[:name] == 'Generic Browser'
browser[:name] = user_agent
end
name = ''
if browser[:plattform] && browser[:plattform] != 'Other'
if browser[:plattform].present? && browser[:plattform] != 'Other'
name = browser[:plattform]
end
if browser[:name] && browser[:name] != 'Other'
if browser[:name].present? && browser[:name] != 'Other'
if name.present?
name += ', '
end
@ -84,7 +88,7 @@ store new device for user if device not already known
end
# if not identified, use user agent
if !name || name == '' || name == 'Other, Other' || name == 'Other'
if name.blank? || name == 'Other, Other' || name == 'Other'
name = user_agent
browser[:name] = user_agent
end
@ -103,7 +107,7 @@ store new device for user if device not already known
end
# create new device
user_device = create(
user_device = create!(
user_id: user_id,
name: name,
os: browser[:plattform],

View file

@ -204,6 +204,42 @@ class UserDeviceTest < ActiveSupport::TestCase
)
assert_equal(user_device2.id, user_device6.id)
# signin without ua from country A via basic auth -> new device #3
user_device7 = UserDevice.add(
'',
'91.115.248.231',
@agent.id,
nil,
'basic_auth',
)
assert_not_equal(user_device6.id, user_device7.id)
user_device8 = UserDevice.add(
'',
'91.115.248.231',
@agent.id,
nil,
'basic_auth',
)
assert_equal(user_device7.id, user_device8.id)
user_device9 = UserDevice.add(
nil,
'91.115.248.231',
@agent.id,
nil,
'basic_auth',
)
assert_equal(user_device8.id, user_device9.id)
user_device10 = UserDevice.add(
nil,
'176.198.137.254',
@agent.id,
nil,
'basic_auth',
)
assert_not_equal(user_device9.id, user_device10.id)
end
test 'ddd - api test' do