diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 995303a631afb64c000e269746aba1bb2777581a..2be7b72159e9ba62885664350975e40e196cfd85 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -15,6 +15,7 @@ class RegistrationsController < Devise::RegistrationsController layout 'devise' prepend_before_action :check_captcha, only: :create + before_action :ensure_first_name_and_last_name_not_empty, only: :create before_action :ensure_destroy_prerequisites_met, only: [:destroy] before_action :init_preferred_language, only: :new before_action :load_recaptcha, only: :new @@ -172,6 +173,21 @@ def check_captcha render action: 'new' end + def ensure_first_name_and_last_name_not_empty + # The key here will be affected by feature flag 'arkose_labs_signup_challenge' + # When flag is disabled, the key will be 'user' because #check_captcha will remove 'new_' prefix + # When flag is enabled, #check_captcha will be skipped, so the key will have 'new_' prefix + first_name = params.dig(resource_name, :first_name) || params.dig("new_#{resource_name}", :first_name) + last_name = params.dig(resource_name, :last_name) || params.dig("new_#{resource_name}", :last_name) + + return if first_name.present? && last_name.present? + + resource.errors.add(_('First name'), _("cannot be blank")) if first_name.blank? + resource.errors.add(_('Last name'), _("cannot be blank")) if last_name.blank? + + render action: 'new' + end + def pending_approval? return false unless Gitlab::CurrentSettings.require_admin_approval_after_user_signup diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 78b870a8266bfcccd7c534b39c8041443d041183..de0aacb2c6699ea6e901115132f68e01b5b865a9 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -47938,6 +47938,9 @@ msgstr "" msgid "cannot be associated with both a Group and a Project" msgstr "" +msgid "cannot be blank" +msgstr "" + msgid "cannot be changed" msgstr "" diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb index 8775f68a5dea8205ad9ecca9c84ac8ce3f377dfa..489461d8876ceeed68ccdd8dbcfda6bc95e4d5ca 100644 --- a/spec/controllers/registrations_controller_spec.rb +++ b/spec/controllers/registrations_controller_spec.rb @@ -552,6 +552,39 @@ end end end + + context 'when the first or last name is not "present?"' do + using RSpec::Parameterized::TableSyntax + + render_views + + shared_examples 'a user without present first name or last name' do + it 'renders the form with errors' do + subject + expect(controller.current_user).to be_nil + expect(response).to render_template(:new) + expect(response.body).to include(_('name cannot be blank')) # include 'First name' or 'Last name' or both + end + end + + where(:first_name, :last_name) do + nil | 'last' + '' | 'last' + ' ' | 'last' + 'first' | nil + 'first' | '' + 'first' | ' ' + '' | '' + end + + with_them do + before do + base_user_params.merge!({ first_name: first_name, last_name: last_name }) + end + + it_behaves_like 'a user without present first name or last name' + end + end end describe '#destroy' do