From f4d610c62cd475bc029ce9d0e7c3c66f0f112c31 Mon Sep 17 00:00:00 2001 From: Gerardo Navarro Date: Thu, 11 Apr 2024 08:19:07 +0200 Subject: [PATCH 1/2] Group visibility levels: Show alert for restricted visibility levels - Ensure that a hint ("The following visibility levels have been restricted ...") is shown when any visibility level is restricted Changelog: fixed --- app/helpers/visibility_level_helper.rb | 15 +++++- app/views/shared/_visibility_radios.html.haml | 11 ++--- locale/gitlab.pot | 5 ++ .../general_visibility_levels_spec.rb | 46 +++++++++++++++++++ spec/helpers/visibility_level_helper_spec.rb | 13 +++--- 5 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 spec/features/groups/settings/general_visibility_levels_spec.rb diff --git a/app/helpers/visibility_level_helper.rb b/app/helpers/visibility_level_helper.rb index cddfc48c649395..e248bb657f61b0 100644 --- a/app/helpers/visibility_level_helper.rb +++ b/app/helpers/visibility_level_helper.rb @@ -84,8 +84,8 @@ def multiple_visibility_levels_restricted? restricted_visibility_levels.many? # rubocop: disable CodeReuse/ActiveRecord end - def all_visibility_levels_restricted? - Gitlab::VisibilityLevel.values == restricted_visibility_levels + def any_visibility_levels_restricted? + restricted_visibility_levels.any? end private @@ -151,6 +151,17 @@ def group_visibility_level_description(level, group = nil) end end + def group_visibility_level_name(level) + case level + when Gitlab::VisibilityLevel::PRIVATE + s_("VisibilityLevel|Private") + when Gitlab::VisibilityLevel::INTERNAL + s_("VisibilityLevel|Internal") + when Gitlab::VisibilityLevel::PUBLIC + s_('VisibilityLevel|Public') + end + end + def project_visibility_icon_description(level) "#{visibility_level_label(level)} - #{project_visibility_level_description(level)}" end diff --git a/app/views/shared/_visibility_radios.html.haml b/app/views/shared/_visibility_radios.html.haml index b4013cb5b80981..30e683117769d0 100644 --- a/app/views/shared/_visibility_radios.html.haml +++ b/app/views/shared/_visibility_radios.html.haml @@ -9,9 +9,8 @@ radio_options: { checked: (selected_level == level), data: { track_label: "blank_project", track_action: "activate_form_input", track_property: "#{model_method}_#{level}", track_value: "" } }, label_options: { class: 'js-visibility-level-radio' } - -.text-muted - - if all_visibility_levels_restricted? - = _('Visibility settings have been disabled by the administrator.') - - elsif multiple_visibility_levels_restricted? - = _('Other visibility settings have been disabled by the administrator.') +- if any_visibility_levels_restricted? + = render Pajamas::AlertComponent.new(variant: :warning, dismissible: false, alert_options: { class: 'gl-my-5' }) do |c| + - c.with_body do + = n_('The following visibility level have been restricted by the administrator:', 'The following visibility levels have been restricted by the administrator:', restricted_visibility_levels.length) + = restricted_visibility_levels.map { |level| group_visibility_level_name(level) }.join(', ') diff --git a/locale/gitlab.pot b/locale/gitlab.pot index e032315abf9054..2b9671f0c14d0c 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -51485,6 +51485,11 @@ msgstr[1] "" msgid "The following personal access tokens have expired:" msgstr "" +msgid "The following visibility level have been restricted by the administrator:" +msgid_plural "The following visibility levels have been restricted by the administrator:" +msgstr[0] "" +msgstr[1] "" + msgid "The fork relationship has been removed." msgstr "" diff --git a/spec/features/groups/settings/general_visibility_levels_spec.rb b/spec/features/groups/settings/general_visibility_levels_spec.rb new file mode 100644 index 00000000000000..1f812529023c1d --- /dev/null +++ b/spec/features/groups/settings/general_visibility_levels_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'General visibility level', feature_category: :groups_and_projects do + let_it_be(:parent_group) { create(:group, :public) } + let_it_be(:user) { create(:user).tap { |user| parent_group.add_owner(user) } } + + before do + sign_in(user) + end + + context 'with multiple restricted visibility levels' do + before do + stub_application_setting(restricted_visibility_levels: Gitlab::VisibilityLevel.values) + end + + it 'shows warning of restricted visibility levels' do + visit edit_group_path(parent_group) + + expect(page).to have_content( + 'The following visibility levels have been restricted by the administrator: Private, Internal, Public' + ) + end + end + + context 'with restricted visibility level public' do + before do + stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel.string_options['public']]) + end + + it 'shows warning of restricted visibility levels' do + visit edit_group_path(parent_group) + + expect(page).to have_content('The following visibility level have been restricted by the administrator: Public') + end + end + + context 'without restricted visibility levels' do + it 'does not show warning of restricted visibility levels' do + visit edit_group_path(parent_group) + + expect(page).not_to have_content('The following visibility levels have been restricted by the administrator:') + end + end +end diff --git a/spec/helpers/visibility_level_helper_spec.rb b/spec/helpers/visibility_level_helper_spec.rb index 4af7fae400ee32..f29d8be4ec23bf 100644 --- a/spec/helpers/visibility_level_helper_spec.rb +++ b/spec/helpers/visibility_level_helper_spec.rb @@ -301,23 +301,24 @@ end end - describe 'all_visibility_levels_restricted?' do + describe 'any_visibility_levels_restricted?' do using RSpec::Parameterized::TableSyntax let(:user) { create(:user) } - subject { helper.all_visibility_levels_restricted? } + subject { helper.any_visibility_levels_restricted? } where(:restricted_visibility_levels, :expected) do - [Gitlab::VisibilityLevel::PUBLIC] | false - [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL] | false - Gitlab::VisibilityLevel.values | true + [Gitlab::VisibilityLevel::PUBLIC] | true + [Gitlab::VisibilityLevel::INTERNAL] | true + [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PRIVATE] | true + [] | false end with_them do before do allow(helper).to receive(:current_user) { user } - allow(Gitlab::CurrentSettings.current_application_settings).to receive(:restricted_visibility_levels) { restricted_visibility_levels } + stub_application_setting(restricted_visibility_levels: restricted_visibility_levels) end it { is_expected.to eq(expected) } -- GitLab From 578685c650e8952e9ba21bc8fdd75407eeafcf6f Mon Sep 17 00:00:00 2001 From: Gerardo Navarro Date: Wed, 17 Apr 2024 17:21:39 +0000 Subject: [PATCH 2/2] refactor: Apply suggestion from @lciutacu --- app/views/shared/_visibility_radios.html.haml | 2 +- locale/gitlab.pot | 2 +- spec/features/groups/settings/general_visibility_levels_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/shared/_visibility_radios.html.haml b/app/views/shared/_visibility_radios.html.haml index 30e683117769d0..0f67f453664c7e 100644 --- a/app/views/shared/_visibility_radios.html.haml +++ b/app/views/shared/_visibility_radios.html.haml @@ -12,5 +12,5 @@ - if any_visibility_levels_restricted? = render Pajamas::AlertComponent.new(variant: :warning, dismissible: false, alert_options: { class: 'gl-my-5' }) do |c| - c.with_body do - = n_('The following visibility level have been restricted by the administrator:', 'The following visibility levels have been restricted by the administrator:', restricted_visibility_levels.length) + = n_('The following visibility level has been restricted by the administrator:', 'The following visibility levels have been restricted by the administrator:', restricted_visibility_levels.length) = restricted_visibility_levels.map { |level| group_visibility_level_name(level) }.join(', ') diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 2b9671f0c14d0c..899abb52305181 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -51485,7 +51485,7 @@ msgstr[1] "" msgid "The following personal access tokens have expired:" msgstr "" -msgid "The following visibility level have been restricted by the administrator:" +msgid "The following visibility level has been restricted by the administrator:" msgid_plural "The following visibility levels have been restricted by the administrator:" msgstr[0] "" msgstr[1] "" diff --git a/spec/features/groups/settings/general_visibility_levels_spec.rb b/spec/features/groups/settings/general_visibility_levels_spec.rb index 1f812529023c1d..dd63109876462f 100644 --- a/spec/features/groups/settings/general_visibility_levels_spec.rb +++ b/spec/features/groups/settings/general_visibility_levels_spec.rb @@ -32,7 +32,7 @@ it 'shows warning of restricted visibility levels' do visit edit_group_path(parent_group) - expect(page).to have_content('The following visibility level have been restricted by the administrator: Public') + expect(page).to have_content('The following visibility level has been restricted by the administrator: Public') end end -- GitLab