user_device.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class UserDevice < ApplicationModel
  3. store :device_details
  4. store :location_details
  5. validates :name, presence: true
  6. =begin
  7. store new device for user if device not already known
  8. user_device = UserDevice.add(
  9. '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',
  10. '172.0.0.1',
  11. user.id,
  12. 'fingerprintABC123',
  13. 'session', # session|basic_auth|token_auth|sso
  14. )
  15. =end
  16. def self.add(user_agent, ip, user_id, fingerprint, type)
  17. # since gem browser 2 is not handling nil for user_agent, set it to ''
  18. user_agent ||= ''
  19. # get location info
  20. location_details = Service::GeoIp.location(ip)
  21. location = 'unknown'
  22. if location_details && location_details['country_name']
  23. location = location_details['country_name']
  24. end
  25. # find device by fingerprint
  26. device_exists_by_fingerprint = false
  27. if fingerprint
  28. user_devices = UserDevice.where(
  29. user_id: user_id,
  30. fingerprint: fingerprint,
  31. )
  32. user_devices.each { |local_user_device|
  33. device_exists_by_fingerprint = true
  34. next if local_user_device.location != location
  35. return action(local_user_device.id, user_agent, ip, user_id, type) if local_user_device
  36. }
  37. end
  38. # for basic_auth|token_auth search for user agent
  39. device_exists_by_user_agent = false
  40. if type == 'basic_auth' || type == 'token_auth'
  41. user_devices = UserDevice.where(
  42. user_id: user_id,
  43. user_agent: user_agent,
  44. )
  45. user_devices.each { |local_user_device|
  46. device_exists_by_user_agent = true
  47. next if local_user_device.location != location
  48. return action(local_user_device.id, user_agent, ip, user_id, type) if local_user_device
  49. }
  50. end
  51. # get browser details
  52. browser = Browser.new(user_agent, accept_language: 'en-us')
  53. browser = {
  54. plattform: browser.platform.to_s.camelize,
  55. name: browser.name,
  56. version: browser.version,
  57. full_version: browser.full_version,
  58. }
  59. # generate device name
  60. if browser[:name] == 'Generic Browser'
  61. browser[:name] = user_agent
  62. end
  63. name = ''
  64. if browser[:plattform] && browser[:plattform] != 'Other'
  65. name = browser[:plattform]
  66. end
  67. if browser[:name] && browser[:name] != 'Other'
  68. if name.present?
  69. name += ', '
  70. end
  71. name += browser[:name]
  72. end
  73. # if not identified, use user agent
  74. if !name || name == '' || name == 'Other, Other' || name == 'Other'
  75. name = user_agent
  76. browser[:name] = user_agent
  77. end
  78. # check if exists
  79. user_device = find_by(
  80. user_id: user_id,
  81. os: browser[:plattform],
  82. browser: browser[:name],
  83. location: location,
  84. fingerprint: fingerprint,
  85. )
  86. if user_device
  87. return action(user_device.id, user_agent, ip, user_id, type) if user_device
  88. end
  89. # create new device
  90. user_device = create(
  91. user_id: user_id,
  92. name: name,
  93. os: browser[:plattform],
  94. browser: browser[:name],
  95. location: location,
  96. device_details: browser,
  97. location_details: location_details,
  98. user_agent: user_agent,
  99. ip: ip,
  100. fingerprint: fingerprint,
  101. )
  102. # send notification if needed
  103. user_devices = UserDevice.where(user_id: user_id).count
  104. if user_devices >= 2
  105. # notify on now device of if country has changed
  106. if device_exists_by_fingerprint || device_exists_by_user_agent
  107. user_device.notification_send('user_device_new_location')
  108. else
  109. user_device.notification_send('user_device_new')
  110. end
  111. end
  112. user_device
  113. end
  114. =begin
  115. log user device action
  116. UserDevice.action(
  117. user_device_id,
  118. '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',
  119. '172.0.0.1',
  120. user.id,
  121. 'session', # session|basic_auth|token_auth|sso
  122. )
  123. =end
  124. def self.action(user_device_id, user_agent, ip, user_id, type)
  125. user_device = UserDevice.lookup(id: user_device_id)
  126. # update location if needed
  127. if user_device.ip != ip
  128. user_device.ip = ip
  129. location_details = Service::GeoIp.location(ip)
  130. # if we do not have any data from backend (e. g. geo ip ist out of service), ignore log
  131. if location_details && location_details['country_name']
  132. user_device.location_details = location_details
  133. location = location_details['country_name']
  134. # notify if country has changed
  135. if user_device.location != location
  136. return UserDevice.add(
  137. user_agent,
  138. ip,
  139. user_id,
  140. user_device.fingerprint,
  141. type,
  142. )
  143. end
  144. end
  145. end
  146. # only update updated_at every 5 min.
  147. return user_device if type != 'session' && (user_device.updated_at + 5.minutes) > Time.zone.now
  148. # update attributes
  149. user_device.updated_at = Time.zone.now # force update, also if no other attribute has changed
  150. user_device.save!
  151. user_device
  152. end
  153. =begin
  154. send user notification about new device or new location for device
  155. user_device = UserDevice.find(id)
  156. user_device.notification_send('user_device_new_location')
  157. =end
  158. def notification_send(template)
  159. user = User.find(user_id)
  160. Rails.logger.debug "Send notification (#{template}) to: #{user.email}"
  161. NotificationFactory::Mailer.notification(
  162. template: template,
  163. user: user,
  164. objects: {
  165. user_device: self,
  166. user: user,
  167. }
  168. )
  169. end
  170. end