scinote-web/app/controllers/users/omniauth_callbacks_controller.rb

169 lines
6.3 KiB
Ruby
Raw Normal View History

2019-12-11 00:24:53 +08:00
# frozen_string_literal: true
2017-08-30 00:49:07 +08:00
module Users
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2018-02-26 18:05:05 +08:00
include UsersGenerator
2017-08-30 00:49:07 +08:00
skip_before_action :verify_authenticity_token
2018-02-26 18:05:05 +08:00
before_action :sign_up_with_provider_enabled?,
only: :linkedin
2016-02-12 23:52:43 +08:00
2017-08-30 00:49:07 +08:00
# You should configure your model like this:
# devise :omniauthable, omniauth_providers: [:twitter]
2016-02-12 23:52:43 +08:00
2017-08-30 00:49:07 +08:00
# You should also create an action method in this controller like this:
# def twitter
# end
2016-02-12 23:52:43 +08:00
2019-12-11 00:24:53 +08:00
def customazureactivedirectory
auth = request.env['omniauth.auth']
provider_id = auth.dig(:extra, :raw_info, :id_token_claims, :aud)
provider_conf = Rails.configuration.x.azure_ad_apps[provider_id]
raise StandardError, 'No matching Azure AD provider config found' if provider_conf.empty?
auth.provider = provider_conf[:provider]
return redirect_to connected_accounts_path if current_user
email = auth.info.email
email ||= auth.dig(:extra, :raw_info, :id_token_claims, :emails)&.first
user = User.from_omniauth(auth)
# User found in database so just signing in
return sign_in_and_redirect(user) if user.present?
if email.blank?
# No email in the token so can not link or create user
error_message = I18n.t('devise.azure.errors.no_email')
return redirect_to after_omniauth_failure_path_for(resource_name)
end
user = User.find_by(email: email)
if user.blank?
# Create new user and identity
full_name = "#{auth.info.first_name} #{auth.info.last_name}"
user = User.new(full_name: full_name,
initials: generate_initials(full_name),
email: email,
password: generate_user_password)
User.transaction do
user.save!
2019-12-11 00:24:53 +08:00
user.user_identities.create!(provider: auth.provider, uid: auth.uid)
user.update!(confirmed_at: user.created_at)
2019-12-11 00:24:53 +08:00
end
sign_in_and_redirect(user)
elsif provider_conf[:auto_link_on_sign_in]
# Link to existing local account
user.user_identities.create!(provider: auth.provider, uid: auth.uid)
sign_in_and_redirect(user)
else
# Cannot do anything with it, so just return an error
error_message = I18n.t('devise.azure.errors.no_local_user_map')
redirect_to after_omniauth_failure_path_for(resource_name)
2019-12-11 00:24:53 +08:00
end
rescue StandardError => e
Rails.logger.error e.message
Rails.logger.error e.backtrace.join("\n")
error_message = I18n.t('devise.azure.errors.failed_to_save') if e.is_a?(ActiveRecord::RecordInvalid)
error_message ||= I18n.t('devise.azure.errors.generic')
redirect_to after_omniauth_failure_path_for(resource_name)
ensure
if error_message
set_flash_message(:alert, :failure, kind: I18n.t('devise.azure.provider_name'), reason: error_message)
else
set_flash_message(:notice, :success, kind: I18n.t('devise.azure.provider_name'))
end
end
2018-02-26 18:05:05 +08:00
def linkedin
auth_hash = request.env['omniauth.auth']
@user = User.from_omniauth(auth_hash)
if @user && @user.current_team_id?
# User already exists and has been signed up with LinkedIn; just sign in
set_flash_message(:notice,
:success,
kind: I18n.t('devise.linkedin.provider_name'))
sign_in_and_redirect @user
elsif @user
# User already exists and has started sign up with LinkedIn;
# but doesn't have team (needs to complete sign up - agrees to TOS)
set_flash_message(:alert,
:failure,
kind: I18n.t('devise.linkedin.provider_name'),
reason: I18n.t('devise.linkedin.complete_sign_up'))
redirect_to users_sign_up_provider_path(user: @user)
elsif User.find_by_email(auth_hash['info']['email'])
# email is already taken, so sign up with Linked in is not allowed
set_flash_message(:alert,
:failure,
kind: I18n.t('devise.linkedin.provider_name'),
reason: I18n.t('devise.linkedin.email_already_taken',
email: auth_hash['info']['email']))
redirect_to after_omniauth_failure_path_for(resource_name)
else
# Create new user and identity; and redirect to complete sign up form
2019-10-29 23:20:19 +08:00
full_name = "#{auth_hash['info']['first_name']} #{auth_hash['info']['last_name']}"
2018-02-26 18:05:05 +08:00
@user = User.new(
2019-10-29 23:20:19 +08:00
full_name: full_name,
initials: generate_initials(full_name),
2018-02-26 18:05:05 +08:00
email: auth_hash['info']['email'],
password: generate_user_password
)
2019-10-29 23:20:19 +08:00
if auth_hash['info']['picture_url']
avatar = URI.open(auth_hash['info']['picture_url'])
@user.avatar.attach(io: avatar, filename: 'linkedin_avatar.jpg')
end
2018-02-26 18:05:05 +08:00
user_identity = UserIdentity.new(user: @user,
provider: auth_hash['provider'],
uid: auth_hash['uid'])
unless @user.save && user_identity.save
set_flash_message(:alert,
:failure,
kind: I18n.t('devise.linkedin.provider_name'),
reason: I18n.t('devise.linkedin.failed_to_save'))
redirect_to after_omniauth_failure_path_for(resource_name) and return
end
# Confirm user
@user.update_column(:confirmed_at, @user.created_at)
2018-02-26 18:05:05 +08:00
redirect_to users_sign_up_provider_path(user: @user)
end
end
2017-08-30 00:49:07 +08:00
# More info at:
# https://github.com/plataformatec/devise#omniauth
2016-02-12 23:52:43 +08:00
2017-08-30 00:49:07 +08:00
# GET|POST /resource/auth/twitter
# def passthru
# super
# end
2016-02-12 23:52:43 +08:00
2017-08-30 00:49:07 +08:00
# GET|POST /users/auth/twitter/callback
# def failure
# super
# end
2016-02-12 23:52:43 +08:00
2017-08-30 00:49:07 +08:00
# protected
# The path used when OmniAuth fails
# def after_omniauth_failure_path_for(scope)
# super(scope)
# end
2018-02-26 18:05:05 +08:00
private
def sign_up_with_provider_enabled?
render_403 unless Rails.configuration.x.enable_user_registration
render_403 unless Rails.configuration.x.linkedin_signin_enabled
end
def generate_initials(full_name)
initials = full_name.titleize.scan(/[A-Z]+/).join
initials = initials.strip.empty? ? 'PLCH' : initials[0..3]
initials
end
2017-08-30 00:49:07 +08:00
end
2016-02-12 23:52:43 +08:00
end