authorization.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Authorization < ApplicationModel
  3. belongs_to :user
  4. after_create :delete_user_cache
  5. after_update :delete_user_cache
  6. after_destroy :delete_user_cache
  7. validates :user_id, presence: true
  8. validates :uid, presence: true, uniqueness: { 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. if !user && Setting.get('auth_third_party_auto_link_at_inital_login')
  57. if hash['info'] && hash['info']['email'].present?
  58. user = User.find_by(email: hash['info']['email'].downcase)
  59. end
  60. end
  61. if !user
  62. user = User.create_from_hash!(hash)
  63. end
  64. # save/update avatar
  65. if hash['info'].present? && hash['info']['image'].present?
  66. avatar = Avatar.add(
  67. object: 'User',
  68. o_id: user.id,
  69. url: hash['info']['image'],
  70. source: hash['provider'],
  71. deletable: true,
  72. updated_by_id: user.id,
  73. created_by_id: user.id,
  74. )
  75. # update user link
  76. if avatar && user.image != avatar.store_hash
  77. user.image = avatar.store_hash
  78. user.save
  79. end
  80. end
  81. Authorization.create!(
  82. user: user,
  83. uid: hash['uid'],
  84. username: hash['info']['nickname'] || hash['info']['username'] || hash['info']['name'] || hash['info']['email'] || hash['username'],
  85. provider: hash['provider'],
  86. token: hash['credentials']['token'],
  87. secret: hash['credentials']['secret']
  88. )
  89. end
  90. private
  91. def delete_user_cache
  92. return if !user
  93. user.touch # rubocop:disable Rails/SkipsModelValidations
  94. end
  95. end