123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- class UserDevice < ApplicationModel
- include UserDevice::TriggersSubscriptions
- store :device_details
- store :location_details
- validates :name, presence: true
- belongs_to :user
- before_create :fingerprint_validation
- before_update :fingerprint_validation
- association_attributes_ignored :user
- def self.add(user_agent, ip, user_id, fingerprint, type)
- if user_agent.blank?
- user_agent = 'unknown'
- end
-
- location_details = Service::GeoIp.location(ip)
- location = 'unknown'
- if location_details && location_details['country_name']
- location = location_details['country_name']
- end
-
- device_exists_by_fingerprint = false
- if fingerprint.present?
- UserDevice.fingerprint_validation(fingerprint)
- user_devices = UserDevice.where(
- user_id: user_id,
- fingerprint: fingerprint,
- )
- user_devices.each do |local_user_device|
- device_exists_by_fingerprint = true
- next if local_user_device.location != location
- return action(local_user_device.id, user_agent, ip, user_id, type) if local_user_device
- end
- end
-
- device_exists_by_user_agent = false
- if %w[basic_auth token_auth].include?(type)
- user_devices = UserDevice.where(
- user_id: user_id,
- user_agent: user_agent,
- )
- user_devices.each do |local_user_device|
- 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
- end
- end
-
- browser = {}
- if user_agent != 'unknown'
- browser = Browser.new(user_agent, accept_language: 'en-us')
- browser = {
- plattform: browser.platform.to_s.camelize,
- name: browser.name,
- version: browser.version,
- full_version: browser.full_version,
- }
- end
-
- if browser[:name] == 'Unknown Browser'
- browser[:name] = user_agent
- end
- name = ''
- if browser[:plattform].present? && browser[:plattform] != 'UnknownPlatform'
- name = browser[:plattform]
- end
- if browser[:name].present? && browser[:name] != 'Other'
- if name.present?
- name += ', '
- end
- name += browser[:name]
- end
-
- if name.blank? || name == 'Other, Other' || name == 'Other'
- name = user_agent
- browser[:name] = user_agent
- end
-
- user_device = find_by(
- user_id: user_id,
- os: browser[:plattform],
- browser: browser[:name],
- location: location,
- fingerprint: fingerprint,
- )
- return action(user_device.id, user_agent, ip, user_id, type) if user_device
-
- user_device = create!(
- user_id: user_id,
- name: name,
- os: browser[:plattform],
- browser: browser[:name],
- location: location,
- device_details: browser,
- location_details: location_details,
- user_agent: user_agent,
- ip: ip,
- fingerprint: fingerprint,
- )
-
- user_devices = UserDevice.where(user_id: user_id).count
- if user_devices >= 2
-
- if device_exists_by_fingerprint || device_exists_by_user_agent
- user_device.notification_send('user_device_new_location')
- else
- user_device.notification_send('user_device_new')
- end
- end
- user_device
- end
- def self.action(user_device_id, user_agent, ip, user_id, type)
- user_device = UserDevice.lookup(id: user_device_id)
-
- if user_device.ip != ip
- user_device.ip = ip
- location_details = Service::GeoIp.location(ip)
-
- if location_details && location_details['country_name']
- user_device.location_details = location_details
- location = location_details['country_name']
-
- if user_device.location != location
- return UserDevice.add(
- user_agent,
- ip,
- user_id,
- user_device.fingerprint,
- type,
- )
- end
- end
- end
-
- return user_device if type != 'session' && (user_device.updated_at + 5.minutes) > Time.zone.now
-
- user_device.updated_at = Time.zone.now
- user_device.save!
- user_device
- end
- def notification_send(template)
- user = User.find(user_id)
- 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
- Rails.logger.debug { "Send notification (#{template}) to: #{user.email}" }
- NotificationFactory::Mailer.notification(
- template: template,
- user: user,
- objects: {
- user_device: self,
- user: user,
- }
- )
- true
- end
- def self.remove(user_id)
- UserDevice.where(user_id: user_id).destroy_all
- end
- def self.fingerprint_validation(fingerprint)
- return true if fingerprint.blank?
- raise Exceptions::UnprocessableEntity, "fingerprint is #{fingerprint.to_s.length} chars but can only be 160 chars!" if fingerprint.to_s.length > 160
- true
- end
- private
- def fingerprint_validation
- UserDevice.fingerprint_validation(fingerprint)
- end
- end
|