user_device.rb 5.8 KB

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