registrations_controller.rb 1.64 KB
Newer Older
Marin Jankovski's avatar
Marin Jankovski committed
1
class RegistrationsController < Devise::RegistrationsController
2
  before_action :signup_enabled?
3
  include Recaptcha::Verify
Marin Jankovski's avatar
Marin Jankovski committed
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

Marin Jankovski's avatar
Marin Jankovski committed
26
  def destroy
27
    DeleteUserService.new(current_user).execute(current_user)
Marin Jankovski's avatar
Marin Jankovski committed
28 29 30 31 32 33

    respond_to do |format|
      format.html { redirect_to new_user_session_path, notice: "Account successfully removed." }
    end
  end

34 35 36 37 38 39
  protected

  def build_resource(hash=nil)
    super
  end

40
  def after_sign_up_path_for(_resource)
41 42 43
    new_user_session_path
  end

44
  def after_inactive_sign_up_path_for(_resource)
45 46 47
    new_user_session_path
  end

Marin Jankovski's avatar
Marin Jankovski committed
48 49 50
  private

  def signup_enabled?
51
    unless current_application_settings.signup_enabled?
52
      redirect_to(new_user_session_path)
53
    end
Marin Jankovski's avatar
Marin Jankovski committed
54
  end
55 56 57 58

  def sign_up_params
    params.require(:user).permit(:username, :email, :name, :password, :password_confirmation)
  end
59 60 61 62 63 64

  def resource_name
    :user
  end

  def resource
Stan Hu's avatar
Stan Hu committed
65
    @resource ||= User.new(sign_up_params)
66 67 68 69 70
  end

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