user_device.rb 5.4 KB

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