omniauth_callbacks_controller.rb 1.5 KB
Newer Older
V
vsizov 已提交
1
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2
  Gitlab.config.omniauth.providers.each do |provider|
D
Dmitriy Zaporozhets 已提交
3 4 5 6
    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
F
Florian Unglaub 已提交
17

V
vsizov 已提交
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)
V
vsizov 已提交
21 22 23 24 25 26
    if @user.persisted?
      @user.remember_me = true
    end
    sign_in_and_redirect @user
  end

F
Florian Unglaub 已提交
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:
F
Florian Unglaub 已提交
35
      current_user.extern_uid = uid
F
Florian Unglaub 已提交
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)
F
Florian Unglaub 已提交
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
V
vsizov 已提交
50
end