diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 995303a631afb64c000e269746aba1bb2777581a..35f395ac904ac085379aa09ae4642becf6584077 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,14 @@ def check_captcha render action: 'new' end + def ensure_first_name_and_last_name_not_empty + return if params[resource_name][:first_name].present? && params[resource_name][:last_name].present? + + resource.errors.add(_('First name'), _("cannot be blank")) if params[resource_name][:first_name].blank? + resource.errors.add(_('Last name'), _("cannot be blank")) if params[resource_name][: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 36f3a7f3ad73a98cd15701df7623e74c65e428a7..039121dd04e0f1abb16468af2d61e0999f8af58e 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -47610,6 +47610,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