1
0

omniauthable.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. module Omniauthable
  3. extend ActiveSupport::Concern
  4. TEMP_EMAIL_PREFIX = 'change@me'
  5. TEMP_EMAIL_REGEX = /\A#{TEMP_EMAIL_PREFIX}/.freeze
  6. included do
  7. devise :omniauthable
  8. def omniauth_providers
  9. Devise.omniauth_configs.keys
  10. end
  11. def email_present?
  12. email && email !~ TEMP_EMAIL_REGEX
  13. end
  14. end
  15. class_methods do
  16. def find_for_oauth(auth, signed_in_resource = nil)
  17. # EOLE-SSO Patch
  18. auth.uid = (auth.uid[0][:uid] || auth.uid[0][:user]) if auth.uid.is_a? Hashie::Array
  19. identity = Identity.find_for_oauth(auth)
  20. # If a signed_in_resource is provided it always overrides the existing user
  21. # to prevent the identity being locked with accidentally created accounts.
  22. # Note that this may leave zombie accounts (with no associated identity) which
  23. # can be cleaned up at a later date.
  24. user = signed_in_resource || identity.user
  25. user ||= create_for_oauth(auth)
  26. if identity.user.nil?
  27. identity.user = user
  28. identity.save!
  29. end
  30. user
  31. end
  32. def create_for_oauth(auth)
  33. # Check if the user exists with provided email. If no email was provided,
  34. # we assign a temporary email and ask the user to verify it on
  35. # the next step via Auth::SetupController.show
  36. strategy = Devise.omniauth_configs[auth.provider.to_sym].strategy
  37. assume_verified = strategy&.security&.assume_email_is_verified
  38. email_is_verified = auth.info.verified || auth.info.verified_email || auth.info.email_verified || assume_verified
  39. email = auth.info.verified_email || auth.info.email
  40. user = User.find_by(email: email) if email_is_verified
  41. return user unless user.nil?
  42. user = User.new(user_params_from_auth(email, auth))
  43. user.account.avatar_remote_url = auth.info.image if /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/.match?(auth.info.image)
  44. user.skip_confirmation! if email_is_verified
  45. user.save!
  46. user
  47. end
  48. private
  49. def user_params_from_auth(email, auth)
  50. {
  51. email: email || "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
  52. agreement: true,
  53. external: true,
  54. account_attributes: {
  55. username: ensure_unique_username(ensure_valid_username(auth.uid)),
  56. display_name: auth.info.full_name || auth.info.name || [auth.info.first_name, auth.info.last_name].join(' '),
  57. },
  58. }
  59. end
  60. def ensure_unique_username(starting_username)
  61. username = starting_username
  62. i = 0
  63. while Account.exists?(username: username, domain: nil)
  64. i += 1
  65. username = "#{starting_username}_#{i}"
  66. end
  67. username
  68. end
  69. def ensure_valid_username(starting_username)
  70. starting_username = starting_username.split('@')[0]
  71. temp_username = starting_username.gsub(/[^a-z0-9_]+/i, '')
  72. validated_username = temp_username.truncate(30, omission: '')
  73. validated_username
  74. end
  75. end
  76. end