user_device.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class UserDevice < ApplicationModel
  3. store :device_details
  4. store :location_details
  5. validates :name, presence: true
  6. belongs_to :user
  7. before_create :fingerprint_validation
  8. before_update :fingerprint_validation
  9. association_attributes_ignored :user
  10. =begin
  11. store new device for user if device not already known
  12. user_device = UserDevice.add(
  13. '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',
  14. '172.0.0.1',
  15. user.id,
  16. 'fingerprintABC123',
  17. 'session', # session|basic_auth|token_auth|sso
  18. )
  19. =end
  20. def self.add(user_agent, ip, user_id, fingerprint, type)
  21. if user_agent.blank?
  22. user_agent = 'unknown'
  23. end
  24. # get location info
  25. location_details = Service::GeoIp.location(ip)
  26. location = 'unknown'
  27. if location_details && location_details['country_name']
  28. location = location_details['country_name']
  29. end
  30. # find device by fingerprint
  31. device_exists_by_fingerprint = false
  32. if fingerprint.present?
  33. UserDevice.fingerprint_validation(fingerprint)
  34. user_devices = UserDevice.where(
  35. user_id: user_id,
  36. fingerprint: fingerprint,
  37. )
  38. user_devices.each do |local_user_device|
  39. device_exists_by_fingerprint = true
  40. next if local_user_device.location != location
  41. return action(local_user_device.id, user_agent, ip, user_id, type) if local_user_device
  42. end
  43. end
  44. # for basic_auth|token_auth search for user agent
  45. device_exists_by_user_agent = false
  46. if %w[basic_auth token_auth].include?(type)
  47. user_devices = UserDevice.where(
  48. user_id: user_id,
  49. user_agent: user_agent,
  50. )
  51. user_devices.each do |local_user_device|
  52. device_exists_by_user_agent = true
  53. next if local_user_device.location != location
  54. return action(local_user_device.id, user_agent, ip, user_id, type) if local_user_device
  55. end
  56. end
  57. # get browser details
  58. browser = {}
  59. if user_agent != 'unknown'
  60. browser = Browser.new(user_agent, accept_language: 'en-us')
  61. browser = {
  62. plattform: browser.platform.to_s.camelize,
  63. name: browser.name,
  64. version: browser.version,
  65. full_version: browser.full_version,
  66. }
  67. end
  68. # generate device name
  69. if browser[:name] == 'Unknown Browser'
  70. browser[:name] = user_agent
  71. end
  72. name = ''
  73. if browser[:plattform].present? && browser[:plattform] != 'UnknownPlatform'
  74. name = browser[:plattform]
  75. end
  76. if browser[:name].present? && browser[:name] != 'Other'
  77. if name.present?
  78. name += ', '
  79. end
  80. name += browser[:name]
  81. end
  82. # if not identified, use user agent
  83. if name.blank? || name == 'Other, Other' || name == 'Other'
  84. name = user_agent
  85. browser[:name] = user_agent
  86. end
  87. # check if exists
  88. user_device = find_by(
  89. user_id: user_id,
  90. os: browser[:plattform],
  91. browser: browser[:name],
  92. location: location,
  93. fingerprint: fingerprint,
  94. )
  95. return action(user_device.id, user_agent, ip, user_id, type) if user_device
  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 is 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. if user.email.blank?
  168. Rails.logger.info { "Unable to notification (#{template}) to user_id: #{user.id} be cause of missing email address." }
  169. return false
  170. end
  171. Rails.logger.debug { "Send notification (#{template}) to: #{user.email}" }
  172. NotificationFactory::Mailer.notification(
  173. template: template,
  174. user: user,
  175. objects: {
  176. user_device: self,
  177. user: user,
  178. }
  179. )
  180. true
  181. end
  182. =begin
  183. delete device devices of user
  184. user_devices = UserDevice.remove(user.id)
  185. =end
  186. def self.remove(user_id)
  187. UserDevice.where(user_id: user_id).destroy_all
  188. end
  189. =begin
  190. check fingerprint string
  191. UserDevice.fingerprint_validation(fingerprint)
  192. =end
  193. def self.fingerprint_validation(fingerprint)
  194. return true if fingerprint.blank?
  195. raise Exceptions::UnprocessableEntity, "fingerprint is #{fingerprint.to_s.length} chars but can only be 160 chars!" if fingerprint.to_s.length > 160
  196. true
  197. end
  198. private
  199. def fingerprint_validation
  200. UserDevice.fingerprint_validation(fingerprint)
  201. end
  202. end