provider.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Authorization::Provider
  3. include Mixin::RequiredSubPaths
  4. attr_reader :auth_hash, :user, :info, :uid
  5. def initialize(auth_hash, user = nil)
  6. @auth_hash = auth_hash
  7. @uid = auth_hash['uid']
  8. @info = auth_hash['info'] || {}
  9. @user = user.presence || fetch_user
  10. end
  11. def name
  12. self.class.name.demodulize.underscore
  13. end
  14. private
  15. def fetch_user
  16. if Setting.get('auth_third_party_auto_link_at_inital_login')
  17. user = find_user
  18. return user if user.present?
  19. end
  20. if Setting.get('auth_third_party_no_create_user')
  21. account = uid || info['email']
  22. message = "User account '#{account}' not found for authentication provider '#{name.capitalize}'."
  23. Rails.logger.error { message }
  24. raise AccountError
  25. end
  26. User.create_from_hash!(auth_hash)
  27. end
  28. def find_user
  29. return if info['email'].nil?
  30. User.find_by(email: info['email'].downcase)
  31. end
  32. class AccountError < StandardError
  33. def initialize
  34. super(__('The user account does not exist. Please contact your administrator.'))
  35. end
  36. end
  37. end