authorization.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Authorization < ApplicationModel
  3. belongs_to :user, optional: true
  4. after_create :delete_user_cache, :notification_send
  5. after_update :delete_user_cache
  6. after_destroy :delete_user_cache
  7. validates :user_id, presence: true
  8. validates :uid, presence: true, uniqueness: { case_sensitive: true, scope: :provider }
  9. validates :provider, presence: true
  10. def self.find_from_hash(hash)
  11. auth = Authorization.find_by(provider: hash['provider'], uid: hash['uid'])
  12. if auth
  13. # update auth tokens
  14. auth.update!(
  15. token: hash['credentials']['token'],
  16. secret: hash['credentials']['secret']
  17. )
  18. # update username of auth entry if empty
  19. if !auth.username && hash['info']['nickname'].present?
  20. auth.update!(
  21. username: hash['info']['nickname'],
  22. )
  23. end
  24. # update firstname/lastname if needed
  25. user = User.find(auth.user_id)
  26. if user.firstname.blank? && user.lastname.blank?
  27. if hash['info']['first_name'].present? && hash['info']['last_name'].present?
  28. user.firstname = hash['info']['first_name']
  29. user.lastname = hash['info']['last_name']
  30. elsif hash['info']['display_name'].present?
  31. user.firstname = hash['info']['display_name']
  32. end
  33. end
  34. # update image if needed
  35. if hash['info']['image'].present?
  36. avatar = Avatar.add(
  37. object: 'User',
  38. o_id: user.id,
  39. url: hash['info']['image'],
  40. source: hash['provider'],
  41. deletable: true,
  42. updated_by_id: user.id,
  43. created_by_id: user.id,
  44. )
  45. if avatar && user.image != avatar.store_hash
  46. user.image = avatar.store_hash
  47. end
  48. end
  49. if user.changed?
  50. user.save
  51. end
  52. end
  53. auth
  54. end
  55. def self.create_from_hash(hash, user = nil)
  56. auth_provider = "#{PROVIDER_CLASS_PREFIX}#{hash['provider'].camelize}".constantize.new(hash, user)
  57. # save/update avatar
  58. if hash['info'].present? && hash['info']['image'].present?
  59. avatar = Avatar.add(
  60. object: 'User',
  61. o_id: auth_provider.user.id,
  62. url: hash['info']['image'],
  63. source: auth_provider.name,
  64. deletable: true,
  65. updated_by_id: auth_provider.user.id,
  66. created_by_id: auth_provider.user.id,
  67. )
  68. # update user link
  69. if avatar && auth_provider.user.image != avatar.store_hash
  70. auth_provider.user.image = avatar.store_hash
  71. auth_provider.user.save
  72. end
  73. end
  74. Authorization.create!(
  75. user: auth_provider.user,
  76. uid: auth_provider.uid,
  77. username: hash['info']['nickname'] || hash['info']['username'] || hash['info']['name'] || hash['info']['email'] || hash['username'],
  78. provider: auth_provider.name,
  79. token: hash['credentials']['token'],
  80. secret: hash['credentials']['secret']
  81. )
  82. end
  83. private
  84. PROVIDER_CLASS_PREFIX = 'Authorization::Provider::'.freeze
  85. def delete_user_cache
  86. return if !user
  87. user.touch # rubocop:disable Rails/SkipsModelValidations
  88. end
  89. # An account is considered linked if the user originates from a source other than the current authorization provider.
  90. def linked_account?
  91. user.source != provider
  92. end
  93. def notification_send
  94. # Send a notification only if the feature is turned on and the account is linked.
  95. return if !Setting.get('auth_third_party_linking_notification') || !user || !linked_account?
  96. template = 'user_auth_provider'
  97. if user.email.blank?
  98. Rails.logger.info { "Unable to send a notification (#{template}) to user_id: #{user.id} be cause of missing email address." }
  99. return
  100. end
  101. Rails.logger.debug { "Send notification (#{template}) to: #{user.email}" }
  102. NotificationFactory::Mailer.notification(
  103. template: template,
  104. user: user,
  105. objects: {
  106. user: user,
  107. provider: provider_name(provider),
  108. }
  109. )
  110. end
  111. def provider_name(provider)
  112. return saml_display_name(provider) if provider == 'saml'
  113. provider_title(provider)
  114. end
  115. # In case of SAML authentication provider, there is a separate display name setting that may be defined.
  116. def saml_display_name(provider)
  117. begin
  118. Setting.get('auth_saml_credentials')['display_name']
  119. rescue
  120. provider_title(provider)
  121. end
  122. end
  123. def provider_title(provider)
  124. begin
  125. Setting.find_by(name: "auth_#{provider}").preferences['title_i18n'].shift
  126. rescue
  127. provider
  128. end
  129. end
  130. end