user_device.rb 6.3 KB

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