BigW Consortium Gitlab

omniauth_callbacks_controller.rb 1.45 KB
Newer Older
vsizov committed
1
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2 3 4 5 6
  Gitlab.config.omniauth_providers.each do |provider|
    define_method provider['name'] do
      handle_omniauth
    end
  end
7 8 9 10

  # Extend the standard message generation to accept our custom exception
  def failure_message
    exception = env["omniauth.error"]
11 12 13 14
    error   = exception.error_reason if exception.respond_to?(:error_reason)
    error ||= exception.error        if exception.respond_to?(:error)
    error ||= exception.message      if exception.respond_to?(:message)
    error ||= env["omniauth.error.type"].to_s
15 16
    error.to_s.humanize if error
  end
Florian Unglaub committed
17

vsizov committed
18 19
  def ldap
    # We only find ourselves here if the authentication to LDAP was successful.
20
    @user = User.find_for_ldap_auth(request.env["omniauth.auth"], current_user)
vsizov committed
21 22 23 24 25 26
    if @user.persisted?
      @user.remember_me = true
    end
    sign_in_and_redirect @user
  end

Florian Unglaub committed
27 28 29 30 31 32 33 34
  private

  def handle_omniauth
    oauth = request.env['omniauth.auth']
    provider, uid = oauth['provider'], oauth['uid']

    if current_user
      # Change a logged-in user's authentication method:
Florian Unglaub committed
35
      current_user.extern_uid = uid
Florian Unglaub committed
36 37 38 39
      current_user.provider = provider
      current_user.save
      redirect_to profile_path
    else
40
      @user = User.find_or_new_for_omniauth(oauth)
Florian Unglaub committed
41 42 43 44 45 46 47 48 49

      if @user
        sign_in_and_redirect @user
      else
        flash[:notice] = "There's no such user!"
        redirect_to new_user_session_path
      end
    end
  end
vsizov committed
50
end