user_device.rb 6.7 KB

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