2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2015-08-17 13:25:41 +00:00
|
|
|
|
|
|
|
class UserDevice < ApplicationModel
|
|
|
|
store :device_details
|
|
|
|
store :location_details
|
|
|
|
validates :name, presence: true
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
belongs_to :user
|
|
|
|
|
2018-04-12 11:23:48 +00:00
|
|
|
before_create :fingerprint_validation
|
|
|
|
before_update :fingerprint_validation
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
association_attributes_ignored :user
|
|
|
|
|
2015-08-17 13:25:41 +00:00
|
|
|
=begin
|
|
|
|
|
2016-03-30 08:00:21 +00:00
|
|
|
store new device for user if device not already known
|
2015-08-17 13:25:41 +00:00
|
|
|
|
2015-08-17 16:14:44 +00:00
|
|
|
user_device = UserDevice.add(
|
2015-08-17 13:25:41 +00:00
|
|
|
'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',
|
|
|
|
'172.0.0.1',
|
|
|
|
user.id,
|
2015-08-18 22:36:58 +00:00
|
|
|
'fingerprintABC123',
|
|
|
|
'session', # session|basic_auth|token_auth|sso
|
2015-08-17 13:25:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
def self.add(user_agent, ip, user_id, fingerprint, type)
|
|
|
|
|
2017-06-27 06:07:43 +00:00
|
|
|
if user_agent.blank?
|
|
|
|
user_agent = 'unknown'
|
|
|
|
end
|
2016-03-01 14:45:33 +00:00
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
# get location info
|
|
|
|
location_details = Service::GeoIp.location(ip)
|
2016-01-14 09:39:52 +00:00
|
|
|
location = 'unknown'
|
2016-04-26 14:55:52 +00:00
|
|
|
if location_details && location_details['country_name']
|
2016-01-14 09:39:52 +00:00
|
|
|
location = location_details['country_name']
|
|
|
|
end
|
2015-08-18 22:36:58 +00:00
|
|
|
|
|
|
|
# find device by fingerprint
|
2016-03-30 08:00:21 +00:00
|
|
|
device_exists_by_fingerprint = false
|
2018-04-12 11:23:48 +00:00
|
|
|
if fingerprint.present?
|
|
|
|
UserDevice.fingerprint_validation(fingerprint)
|
2016-03-30 08:00:21 +00:00
|
|
|
user_devices = UserDevice.where(
|
2018-12-19 17:31:51 +00:00
|
|
|
user_id: user_id,
|
2015-08-18 22:36:58 +00:00
|
|
|
fingerprint: fingerprint,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
user_devices.each do |local_user_device|
|
2016-03-30 08:00:21 +00:00
|
|
|
device_exists_by_fingerprint = true
|
|
|
|
next if local_user_device.location != location
|
2016-04-11 13:15:27 +00:00
|
|
|
return action(local_user_device.id, user_agent, ip, user_id, type) if local_user_device
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-18 22:36:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# for basic_auth|token_auth search for user agent
|
2016-04-11 13:15:27 +00:00
|
|
|
device_exists_by_user_agent = false
|
2017-11-23 08:09:44 +00:00
|
|
|
if %w[basic_auth token_auth].include?(type)
|
2016-04-11 13:15:27 +00:00
|
|
|
user_devices = UserDevice.where(
|
2018-12-19 17:31:51 +00:00
|
|
|
user_id: user_id,
|
2015-08-18 22:36:58 +00:00
|
|
|
user_agent: user_agent,
|
|
|
|
)
|
2017-10-01 12:25:52 +00:00
|
|
|
user_devices.each do |local_user_device|
|
2016-04-11 13:15:27 +00:00
|
|
|
device_exists_by_user_agent = true
|
|
|
|
next if local_user_device.location != location
|
|
|
|
return action(local_user_device.id, user_agent, ip, user_id, type) if local_user_device
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-18 22:36:58 +00:00
|
|
|
end
|
2015-08-17 13:25:41 +00:00
|
|
|
|
|
|
|
# get browser details
|
2017-06-27 06:07:43 +00:00
|
|
|
browser = {}
|
|
|
|
if user_agent != 'unknown'
|
|
|
|
browser = Browser.new(user_agent, accept_language: 'en-us')
|
|
|
|
browser = {
|
2018-12-19 17:31:51 +00:00
|
|
|
plattform: browser.platform.to_s.camelize,
|
|
|
|
name: browser.name,
|
|
|
|
version: browser.version,
|
2017-06-27 06:07:43 +00:00
|
|
|
full_version: browser.full_version,
|
|
|
|
}
|
|
|
|
end
|
2015-08-17 13:25:41 +00:00
|
|
|
|
|
|
|
# generate device name
|
2016-03-01 15:53:25 +00:00
|
|
|
if browser[:name] == 'Generic Browser'
|
|
|
|
browser[:name] = user_agent
|
|
|
|
end
|
2015-08-19 00:46:14 +00:00
|
|
|
name = ''
|
2017-06-27 06:07:43 +00:00
|
|
|
if browser[:plattform].present? && browser[:plattform] != 'Other'
|
2015-08-19 00:46:14 +00:00
|
|
|
name = browser[:plattform]
|
|
|
|
end
|
2017-06-27 06:07:43 +00:00
|
|
|
if browser[:name].present? && browser[:name] != 'Other'
|
2016-12-02 11:24:00 +00:00
|
|
|
if name.present?
|
2015-08-17 13:25:41 +00:00
|
|
|
name += ', '
|
|
|
|
end
|
|
|
|
name += browser[:name]
|
|
|
|
end
|
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
# if not identified, use user agent
|
2017-06-27 06:07:43 +00:00
|
|
|
if name.blank? || name == 'Other, Other' || name == 'Other'
|
2015-08-18 22:36:58 +00:00
|
|
|
name = user_agent
|
|
|
|
browser[:name] = user_agent
|
|
|
|
end
|
2015-08-17 13:25:41 +00:00
|
|
|
|
|
|
|
# check if exists
|
2015-08-21 21:53:53 +00:00
|
|
|
user_device = find_by(
|
2018-12-19 17:31:51 +00:00
|
|
|
user_id: user_id,
|
|
|
|
os: browser[:plattform],
|
|
|
|
browser: browser[:name],
|
|
|
|
location: location,
|
2016-04-11 07:35:27 +00:00
|
|
|
fingerprint: fingerprint,
|
2015-08-17 13:25:41 +00:00
|
|
|
)
|
|
|
|
|
2020-09-30 09:07:01 +00:00
|
|
|
return action(user_device.id, user_agent, ip, user_id, type) if user_device
|
2015-08-17 13:25:41 +00:00
|
|
|
|
|
|
|
# create new device
|
2017-06-27 06:07:43 +00:00
|
|
|
user_device = create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
user_id: user_id,
|
|
|
|
name: name,
|
|
|
|
os: browser[:plattform],
|
|
|
|
browser: browser[:name],
|
|
|
|
location: location,
|
|
|
|
device_details: browser,
|
2015-08-18 22:36:58 +00:00
|
|
|
location_details: location_details,
|
2018-12-19 17:31:51 +00:00
|
|
|
user_agent: user_agent,
|
|
|
|
ip: ip,
|
|
|
|
fingerprint: fingerprint,
|
2015-08-17 13:25:41 +00:00
|
|
|
)
|
2015-08-18 22:36:58 +00:00
|
|
|
|
2015-08-18 23:00:07 +00:00
|
|
|
# send notification if needed
|
|
|
|
user_devices = UserDevice.where(user_id: user_id).count
|
|
|
|
if user_devices >= 2
|
2016-03-30 08:00:21 +00:00
|
|
|
|
|
|
|
# notify on now device of if country has changed
|
2016-04-11 13:15:27 +00:00
|
|
|
if device_exists_by_fingerprint || device_exists_by_user_agent
|
2016-03-30 08:00:21 +00:00
|
|
|
user_device.notification_send('user_device_new_location')
|
|
|
|
else
|
|
|
|
user_device.notification_send('user_device_new')
|
|
|
|
end
|
2015-08-18 23:00:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
user_device
|
2015-08-18 22:36:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
log user device action
|
|
|
|
|
|
|
|
UserDevice.action(
|
|
|
|
user_device_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',
|
|
|
|
'172.0.0.1',
|
|
|
|
user.id,
|
2016-04-11 13:15:27 +00:00
|
|
|
'session', # session|basic_auth|token_auth|sso
|
2015-08-18 22:36:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-04-11 13:15:27 +00:00
|
|
|
def self.action(user_device_id, user_agent, ip, user_id, type)
|
2016-08-01 21:04:24 +00:00
|
|
|
user_device = UserDevice.lookup(id: user_device_id)
|
2015-08-18 22:36:58 +00:00
|
|
|
|
|
|
|
# update location if needed
|
|
|
|
if user_device.ip != ip
|
|
|
|
user_device.ip = ip
|
|
|
|
location_details = Service::GeoIp.location(ip)
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# if we do not have any data from backend (e.g. geo ip is out of service), ignore log
|
2016-04-26 14:55:52 +00:00
|
|
|
if location_details && location_details['country_name']
|
|
|
|
|
|
|
|
user_device.location_details = location_details
|
|
|
|
location = location_details['country_name']
|
|
|
|
|
|
|
|
# notify if country has changed
|
|
|
|
if user_device.location != location
|
|
|
|
return UserDevice.add(
|
|
|
|
user_agent,
|
|
|
|
ip,
|
|
|
|
user_id,
|
|
|
|
user_device.fingerprint,
|
|
|
|
type,
|
|
|
|
)
|
|
|
|
end
|
2016-03-30 08:00:21 +00:00
|
|
|
end
|
2015-08-18 22:36:58 +00:00
|
|
|
end
|
|
|
|
|
2016-08-01 21:04:24 +00:00
|
|
|
# only update updated_at every 5 min.
|
|
|
|
return user_device if type != 'session' && (user_device.updated_at + 5.minutes) > Time.zone.now
|
|
|
|
|
2015-08-18 22:36:58 +00:00
|
|
|
# update attributes
|
2016-03-25 12:10:31 +00:00
|
|
|
user_device.updated_at = Time.zone.now # force update, also if no other attribute has changed
|
2016-08-01 21:04:24 +00:00
|
|
|
user_device.save!
|
2015-08-18 22:36:58 +00:00
|
|
|
user_device
|
2015-08-17 13:25:41 +00:00
|
|
|
end
|
|
|
|
|
2015-08-18 23:00:07 +00:00
|
|
|
=begin
|
|
|
|
|
2016-03-30 08:00:21 +00:00
|
|
|
send user notification about new device or new location for device
|
2015-08-18 23:00:07 +00:00
|
|
|
|
|
|
|
user_device = UserDevice.find(id)
|
|
|
|
|
2016-03-30 08:00:21 +00:00
|
|
|
user_device.notification_send('user_device_new_location')
|
2015-08-18 23:00:07 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-03-30 08:00:21 +00:00
|
|
|
def notification_send(template)
|
2015-08-18 23:00:07 +00:00
|
|
|
user = User.find(user_id)
|
|
|
|
|
2020-02-19 14:57:32 +00:00
|
|
|
if user.email.blank?
|
|
|
|
Rails.logger.info { "Unable to notification (#{template}) to user_id: #{user.id} be cause of missing email address." }
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { "Send notification (#{template}) to: #{user.email}" }
|
2016-06-14 07:37:46 +00:00
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
NotificationFactory::Mailer.notification(
|
2016-03-30 08:00:21 +00:00
|
|
|
template: template,
|
2018-12-19 17:31:51 +00:00
|
|
|
user: user,
|
|
|
|
objects: {
|
2016-02-19 21:05:36 +00:00
|
|
|
user_device: self,
|
2018-12-19 17:31:51 +00:00
|
|
|
user: user,
|
2016-02-19 21:05:36 +00:00
|
|
|
}
|
2015-08-18 23:00:07 +00:00
|
|
|
)
|
2020-02-19 14:57:32 +00:00
|
|
|
|
|
|
|
true
|
2015-08-18 23:00:07 +00:00
|
|
|
end
|
2016-03-30 08:00:21 +00:00
|
|
|
|
2017-07-24 07:06:15 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
delete device devices of user
|
|
|
|
|
|
|
|
user_devices = UserDevice.remove(user.id)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.remove(user_id)
|
|
|
|
UserDevice.where(user_id: user_id).destroy_all
|
|
|
|
end
|
2018-04-12 11:23:48 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check fingerprint string
|
|
|
|
|
|
|
|
UserDevice.fingerprint_validation(fingerprint)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.fingerprint_validation(fingerprint)
|
|
|
|
return true if fingerprint.blank?
|
2018-04-25 12:50:36 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, "fingerprint is #{fingerprint.to_s.length} chars but can only be 160 chars!" if fingerprint.to_s.length > 160
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-12 11:23:48 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fingerprint_validation
|
|
|
|
UserDevice.fingerprint_validation(fingerprint)
|
|
|
|
end
|
2015-08-17 13:25:41 +00:00
|
|
|
end
|