registrations_controller.rb 1.7 KB
Newer Older
M
Marin Jankovski 已提交
1
class RegistrationsController < Devise::RegistrationsController
2
  before_action :signup_enabled?
3
  include Recaptcha::Verify
M
Marin Jankovski 已提交
4

5 6 7 8
  def new
    redirect_to(new_user_session_path)
  end

9
  def create
10
    if !Gitlab::Recaptcha.load_configurations! || verify_recaptcha
11 12 13 14 15 16 17
      # To avoid duplicate form fields on the login page, the registration form
      # names fields using `new_user`, but Devise still wants the params in
      # `user`.
      if params["new_#{resource_name}"].present? && params[resource_name].blank?
        params[resource_name] = params.delete(:"new_#{resource_name}")
      end

18 19 20 21 22 23 24 25
      super
    else
      flash[:alert] = "There was an error with the reCAPTCHA code below. Please re-enter the code."
      flash.delete :recaptcha_error
      render action: 'new'
    end
  end

M
Marin Jankovski 已提交
26
  def destroy
27
    DeleteUserService.new(current_user).execute(current_user)
M
Marin Jankovski 已提交
28 29

    respond_to do |format|
30 31 32 33
      format.html do
        session.try(:destroy)
        redirect_to new_user_session_path, notice: "Account successfully removed."
      end 
M
Marin Jankovski 已提交
34 35 36
    end
  end

37 38
  protected

39
  def build_resource(hash = nil)
40 41 42
    super
  end

43
  def after_sign_up_path_for(user)
44
    user.confirmed? ? dashboard_projects_path : users_almost_there_path
45 46
  end

47
  def after_inactive_sign_up_path_for(_resource)
P
Phil Hughes 已提交
48
    users_almost_there_path
49 50
  end

M
Marin Jankovski 已提交
51 52 53
  private

  def signup_enabled?
D
Dmitriy Zaporozhets 已提交
54
    unless current_application_settings.signup_enabled?
55
      redirect_to(new_user_session_path)
56
    end
M
Marin Jankovski 已提交
57
  end
58 59 60 61

  def sign_up_params
    params.require(:user).permit(:username, :email, :name, :password, :password_confirmation)
  end
62 63 64 65 66 67

  def resource_name
    :user
  end

  def resource
S
Stan Hu 已提交
68
    @resource ||= User.new(sign_up_params)
69 70 71 72 73
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
74
end