/app/controllers/omniauth_callbacks_controller.rb

https://gitlab.com/webx32/gitlab · Ruby · 96 lines · 74 code · 15 blank · 7 comment · 13 complexity · 27b1ea27d0a9e19f5da9d30eecb56e17 MD5 · raw file

  1. #encoding: utf-8
  2. class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  3. protect_from_forgery except: [:kerberos, :saml]
  4. Gitlab.config.omniauth.providers.each do |provider|
  5. define_method provider['name'] do
  6. handle_omniauth
  7. end
  8. end
  9. # Extend the standard message generation to accept our custom exception
  10. def failure_message
  11. exception = env["omniauth.error"]
  12. error = exception.error_reason if exception.respond_to?(:error_reason)
  13. error ||= exception.error if exception.respond_to?(:error)
  14. error ||= exception.message if exception.respond_to?(:message)
  15. error ||= env["omniauth.error.type"].to_s
  16. error.to_s.humanize if error
  17. end
  18. # We only find ourselves here
  19. # if the authentication to LDAP was successful.
  20. def ldap
  21. @user = Gitlab::LDAP::User.new(oauth)
  22. @user.save if @user.changed? # will also save new users
  23. gl_user = @user.gl_user
  24. gl_user.remember_me = params[:remember_me] if @user.persisted?
  25. # Do additional LDAP checks for the user filter and EE features
  26. if @user.allowed?
  27. log_audit_event(gl_user, with: :ldap)
  28. sign_in_and_redirect(gl_user)
  29. else
  30. flash[:alert] = "Access denied for your LDAP account."
  31. redirect_to new_user_session_path
  32. end
  33. end
  34. def omniauth_error
  35. @provider = params[:provider]
  36. @error = params[:error]
  37. render 'errors/omniauth_error', layout: "errors", status: 422
  38. end
  39. private
  40. def handle_omniauth
  41. if current_user
  42. # Add new authentication method
  43. current_user.identities.find_or_create_by(extern_uid: oauth['uid'], provider: oauth['provider'])
  44. log_audit_event(current_user, with: oauth['provider'])
  45. redirect_to profile_account_path, notice: '认证方法已更新'
  46. else
  47. @user = Gitlab::OAuth::User.new(oauth)
  48. @user.save
  49. # Only allow properly saved users to login.
  50. if @user.persisted? && @user.valid?
  51. log_audit_event(@user.gl_user, with: oauth['provider'])
  52. sign_in_and_redirect(@user.gl_user)
  53. else
  54. error_message =
  55. if @user.gl_user.errors.any?
  56. @user.gl_user.errors.map do |attribute, message|
  57. "#{attribute} #{message}"
  58. end.join(", ")
  59. else
  60. ''
  61. end
  62. redirect_to omniauth_error_path(oauth['provider'], error: error_message) and return
  63. end
  64. end
  65. rescue Gitlab::OAuth::SignupDisabledError
  66. label = Gitlab::OAuth::Provider.label_for(oauth['provider'])
  67. message = "没有绑定 GitLab 账号前是无法使用 #{label} 账号登陆系统。"
  68. if current_application_settings.signup_enabled?
  69. message << " 请先创建一个 GitLab 账号,然后再绑定 #{label} 账号。"
  70. end
  71. flash[:notice] = message
  72. redirect_to new_user_session_path
  73. end
  74. def oauth
  75. @oauth ||= request.env['omniauth.auth']
  76. end
  77. def log_audit_event(user, options = {})
  78. AuditEventService.new(user, user, options).
  79. for_authentication.security_event
  80. end
  81. end