omniauth_callbacks_controller.rb 2.6 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

18 19
  # We only find ourselves here
  # if the authentication to LDAP was successful.
V
vsizov 已提交
20
  def ldap
21 22 23 24
    @user = Gitlab::LDAP::User.new(oauth)
    @user.save if @user.changed? # will also save new users
    gl_user = @user.gl_user
    gl_user.remember_me = true if @user.persisted?
25

26
    # Do additional LDAP checks for the user filter and EE features
27 28
    if @user.allowed?
      sign_in_and_redirect(gl_user)
29 30 31
    else
      flash[:alert] = "Access denied for your LDAP account."
      redirect_to new_user_session_path
32
    end
V
vsizov 已提交
33 34
  end

35 36 37 38 39 40
  def omniauth_error
    @provider = params[:provider]
    @error = params[:error]
    render 'errors/omniauth_error', layout: "errors", status: 422
  end

F
Florian Unglaub 已提交
41 42 43 44
  private

  def handle_omniauth
    if current_user
45 46
      # Add new authentication method
      current_user.identities.find_or_create_by(extern_uid: oauth['uid'], provider: oauth['provider'])
47
      redirect_to profile_account_path, notice: 'Authentication method updated'
F
Florian Unglaub 已提交
48
    else
49
      @user = Gitlab::OAuth::User.new(oauth)
50
      @user.save
F
Florian Unglaub 已提交
51

52 53
      # Only allow properly saved users to login.
      if @user.persisted? && @user.valid?
54
        sign_in_and_redirect(@user.gl_user)
D
Dmitriy Zaporozhets 已提交
55 56 57 58 59 60 61 62 63 64
      else
        error_message =
          if @user.gl_user.errors.any?
            @user.gl_user.errors.map do |attribute, message|
              "#{attribute} #{message}"
            end.join(", ")
          else
            ''
          end

65
        redirect_to omniauth_error_path(oauth['provider'], error: error_message) and return
F
Florian Unglaub 已提交
66 67
      end
    end
D
Douwe Maan 已提交
68 69 70 71 72 73 74 75 76
  rescue Gitlab::OAuth::SignupDisabledError => e
    message = "Signing in using your #{oauth['provider']} account without a pre-existing GitLab account is not allowed."

    if current_application_settings.signup_enabled?
      message << " Create a GitLab account first, and then connect it to your #{oauth['provider']} account."
    end

    flash[:notice] = message
    
77
    redirect_to new_user_session_path
F
Florian Unglaub 已提交
78
  end
D
Dmitriy Zaporozhets 已提交
79 80 81 82

  def oauth
    @oauth ||= request.env['omniauth.auth']
  end
V
vsizov 已提交
83
end