diff --git a/ee/app/assets/javascripts/usage_quotas/usage_billing/components/app.vue b/ee/app/assets/javascripts/usage_quotas/usage_billing/components/app.vue index 1b6815f9c20954baa6f29340171ff66d0294d061..be0aded0dbf73b0ecf7a2c574ef2daf840a89e27 100644 --- a/ee/app/assets/javascripts/usage_quotas/usage_billing/components/app.vue +++ b/ee/app/assets/javascripts/usage_quotas/usage_billing/components/app.vue @@ -65,6 +65,9 @@ export default { isUsageBillingDisabled() { return this.subscriptionUsage?.enabled === false; }, + shouldDisplayUserData() { + return gon.display_gitlab_credits_user_data; + }, subscriptionsUrl() { return gon.subscriptions_url; }, @@ -249,7 +252,10 @@ export default { /> - + + + {{ s__('UsageBilling|Displaying user data is disabled.') }} + diff --git a/ee/app/controllers/admin/gitlab_credits_dashboard/users_controller.rb b/ee/app/controllers/admin/gitlab_credits_dashboard/users_controller.rb index 2e17371c63edd3f53e4f60e3a0cb999bd4eb65eb..769998a3e9e25ffd16fe7ca5c9a653b200f7b389 100644 --- a/ee/app/controllers/admin/gitlab_credits_dashboard/users_controller.rb +++ b/ee/app/controllers/admin/gitlab_credits_dashboard/users_controller.rb @@ -7,6 +7,9 @@ class UsersController < Admin::ApplicationController urgency :low before_action :ensure_feature_available! + before_action do + push_application_setting(:display_gitlab_credits_user_data) + end def show @username = params.permit(:username)[:username] @@ -17,8 +20,9 @@ def show def ensure_feature_available! return render_404 unless Feature.enabled?(:usage_billing_dev, :instance) return render_404 unless License.feature_available?(:usage_billing) + return render_404 if Gitlab::Saas.feature_available?(:gitlab_com_subscriptions) - render_404 if Gitlab::Saas.feature_available?(:gitlab_com_subscriptions) + render_404 unless ::Gitlab::CurrentSettings.display_gitlab_credits_user_data end end end diff --git a/ee/app/controllers/admin/gitlab_credits_dashboard_controller.rb b/ee/app/controllers/admin/gitlab_credits_dashboard_controller.rb index 96fe7e42fbe5cb0bd336998a3d3ed7b4097d896d..9c6457b1b72b7a07cea931358224788186c3dde8 100644 --- a/ee/app/controllers/admin/gitlab_credits_dashboard_controller.rb +++ b/ee/app/controllers/admin/gitlab_credits_dashboard_controller.rb @@ -6,6 +6,9 @@ class GitlabCreditsDashboardController < Admin::ApplicationController urgency :low before_action :ensure_feature_available! + before_action do + push_application_setting(:display_gitlab_credits_user_data) + end private diff --git a/ee/app/controllers/groups/settings/gitlab_duo/usage/users_controller.rb b/ee/app/controllers/groups/settings/gitlab_duo/usage/users_controller.rb index 09ff1358dc0da91701ceb638b0bef865171e41b4..0726a65f82d47cd4c9b563178020903dce9421aa 100644 --- a/ee/app/controllers/groups/settings/gitlab_duo/usage/users_controller.rb +++ b/ee/app/controllers/groups/settings/gitlab_duo/usage/users_controller.rb @@ -19,8 +19,9 @@ def show def ensure_feature_available! return render_404 unless Feature.enabled?(:usage_billing_dev, @group) return render_404 unless Gitlab::Saas.feature_available?(:gitlab_com_subscriptions) + return render_404 unless @group.licensed_feature_available?(:group_usage_billing) - render_404 unless @group.licensed_feature_available?(:group_usage_billing) + render_404 unless @group.namespace_settings.display_gitlab_credits_user_data end end end diff --git a/ee/app/controllers/groups/settings/gitlab_duo/usage_controller.rb b/ee/app/controllers/groups/settings/gitlab_duo/usage_controller.rb index 8c7b3842d5da0c444675a59b2cfc41d150a69934..e1bce20119eadaea0797fcdc4d565bf081bc3e84 100644 --- a/ee/app/controllers/groups/settings/gitlab_duo/usage_controller.rb +++ b/ee/app/controllers/groups/settings/gitlab_duo/usage_controller.rb @@ -6,6 +6,9 @@ module GitlabDuo class UsageController < Groups::ApplicationController before_action :authorize_read_usage_quotas! before_action :ensure_feature_available! + before_action do + push_namespace_setting(:display_gitlab_credits_user_data, @group) + end feature_category :consumables_cost_management diff --git a/ee/spec/frontend/usage_quotas/usage_billing/components/app_spec.js b/ee/spec/frontend/usage_quotas/usage_billing/components/app_spec.js index 18ef76824fbcbbe6b4f8ca4a709009be36dd96c6..19c73ee1b51e73530953d561f2e269bb556191f7 100644 --- a/ee/spec/frontend/usage_quotas/usage_billing/components/app_spec.js +++ b/ee/spec/frontend/usage_quotas/usage_billing/components/app_spec.js @@ -63,6 +63,10 @@ describe('UsageBillingApp', () => { const findOutdatedClientAlert = () => wrapper.findByTestId('outdated-client-alert'); const findDisabledStateAlert = () => wrapper.findByTestId('usage-billing-disabled-alert'); + beforeEach(() => { + window.gon = { display_gitlab_credits_user_data: true }; + }); + describe('loading state', () => { beforeEach(() => { const loadingQueryHandler = jest.fn().mockImplementation(() => new Promise(() => {})); @@ -428,6 +432,14 @@ describe('UsageBillingApp', () => { it('renders UsageByUserTab component', () => { expect(findUsageByUserTab().exists()).toBe(true); }); + + it('does not render UsageByUserTab component if display_gitlab_credits_user_data is false', async () => { + window.gon = { display_gitlab_credits_user_data: false }; + createComponent(); + await waitForPromises(); + + expect(findUsageByUserTab().exists()).toBe(false); + }); }); }); diff --git a/ee/spec/requests/admin/gitlab_credits_dashboard/users_controller_spec.rb b/ee/spec/requests/admin/gitlab_credits_dashboard/users_controller_spec.rb index e9b10fe40774a0d8c05d8459ed47d22d81f99837..be03d7e3bdab2162198d97945594a6a86c63e715 100644 --- a/ee/spec/requests/admin/gitlab_credits_dashboard/users_controller_spec.rb +++ b/ee/spec/requests/admin/gitlab_credits_dashboard/users_controller_spec.rb @@ -7,10 +7,12 @@ let(:admin) { create(:admin) } let(:user) { create(:user) } let(:usage_billing_dev_enabled) { true } + let(:display_gitlab_credits_user_data) { true } before do sign_in(admin) stub_feature_flags(usage_billing_dev: usage_billing_dev_enabled) + stub_application_setting(display_gitlab_credits_user_data: display_gitlab_credits_user_data) end describe 'GET /admin/gitlab_credits_dashboard/users' do @@ -44,5 +46,15 @@ expect(response).to have_gitlab_http_status(:not_found) end end + + context 'when display_gitlab_credits_user_data is false' do + let(:display_gitlab_credits_user_data) { false } + + it 'renders 404' do + request + + expect(response).to have_gitlab_http_status(:not_found) + end + end end end diff --git a/ee/spec/requests/admin/gitlab_credits_dashboard_controller_spec.rb b/ee/spec/requests/admin/gitlab_credits_dashboard_controller_spec.rb index 9b0d2c7cf150d5277cb55140ab2740bbaadc866f..175b41c13315c28c8240a1b6f82034c1b3396f67 100644 --- a/ee/spec/requests/admin/gitlab_credits_dashboard_controller_spec.rb +++ b/ee/spec/requests/admin/gitlab_credits_dashboard_controller_spec.rb @@ -42,5 +42,17 @@ expect(response).to have_gitlab_http_status(:not_found) end end + + context 'for display_gitlab_credits_user_data' do + before do + stub_application_setting(display_gitlab_credits_user_data: true) + end + + it 'pushes the setting to the frontend' do + request + + expect(response.body).to include('gon.display_gitlab_credits_user_data=true') + end + end end end diff --git a/ee/spec/requests/groups/settings/gitlab_duo/usage/users_controller_spec.rb b/ee/spec/requests/groups/settings/gitlab_duo/usage/users_controller_spec.rb index bf44c7b7a60f363ef02b8a2fb108ce25875cd7b3..84447551f1e00af7b9c1ea02306bf799f2d26723 100644 --- a/ee/spec/requests/groups/settings/gitlab_duo/usage/users_controller_spec.rb +++ b/ee/spec/requests/groups/settings/gitlab_duo/usage/users_controller_spec.rb @@ -4,8 +4,10 @@ RSpec.describe Groups::Settings::GitlabDuo::Usage::UsersController, feature_category: :consumables_cost_management do let_it_be(:user) { create(:user) } - let_it_be(:group) { create(:group) } + let_it_be_with_reload(:group) { create(:group) } + let(:usage_billing_dev_enabled) { true } + let(:display_gitlab_credits_user_data) { true } before do sign_in(user) @@ -40,6 +42,10 @@ paid_group.add_owner(user) end + before do + paid_group.namespace_settings.update!(display_gitlab_credits_user_data: display_gitlab_credits_user_data) + end + it 'returns 200' do request @@ -55,6 +61,16 @@ expect(response).to have_gitlab_http_status(:not_found) end end + + context 'when display_gitlab_credits_user_data is false' do + let(:display_gitlab_credits_user_data) { false } + + it 'renders 404' do + request + + expect(response).to have_gitlab_http_status(:not_found) + end + end end end @@ -77,6 +93,7 @@ end before do + paid_group.namespace_settings.update!(display_gitlab_credits_user_data: display_gitlab_credits_user_data) stub_ee_application_setting(should_check_namespace_plan: true) end diff --git a/ee/spec/requests/groups/settings/gitlab_duo/usage_controller_spec.rb b/ee/spec/requests/groups/settings/gitlab_duo/usage_controller_spec.rb index 615e4a97d2d299bd5896df2e447b763e7155ea15..b3608c8e3b68bc09342bda5a9927a372b46804b3 100644 --- a/ee/spec/requests/groups/settings/gitlab_duo/usage_controller_spec.rb +++ b/ee/spec/requests/groups/settings/gitlab_duo/usage_controller_spec.rb @@ -55,6 +55,18 @@ expect(response).to have_gitlab_http_status(:not_found) end end + + context 'for display_gitlab_credits_user_data' do + before do + paid_group.namespace_settings.update!(display_gitlab_credits_user_data: true) + end + + it 'pushes the setting to the frontend' do + request + + expect(response.body).to include('gon.display_gitlab_credits_user_data=true') + end + end end end diff --git a/lib/gitlab/gon_helper.rb b/lib/gitlab/gon_helper.rb index 8aba0213ad359e0737ab1ea74d1935a5c59649d9..ff4b60d2c0a13b0755e87ac082ae1f6d55885256 100644 --- a/lib/gitlab/gon_helper.rb +++ b/lib/gitlab/gon_helper.rb @@ -145,6 +145,12 @@ def push_force_frontend_feature_flag(name, enabled) push_to_gon_attributes(:features, name, enabled) end + def push_application_setting(key) + return unless Gitlab::CurrentSettings.respond_to?(key) + + gon.push({ key => Gitlab::CurrentSettings.public_send(key) }) # rubocop:disable GitlabSecurity/PublicSend + end + def push_namespace_setting(key, object) return unless object&.namespace_settings.respond_to?(key) diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 9d1307399db059629fd600526bb829a6d04f0fe9..433daac93669459e6342ef2fe9dbdbd9b1885195 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -72052,6 +72052,9 @@ msgstr "" msgid "UsageBilling|Date/Time" msgstr "" +msgid "UsageBilling|Displaying user data is disabled." +msgstr "" + msgid "UsageBilling|Enable on-demand billing to keep GitLab Duo features active when monthly GitLab Credits run out. Without these terms, users lose GitLab Duo access after exhausting their included GitLab Credits. Learn about %{linkStart}overage billing%{linkEnd}." msgstr "" diff --git a/spec/lib/gitlab/gon_helper_spec.rb b/spec/lib/gitlab/gon_helper_spec.rb index 57b471b05c49f01c4ce2f482617b01f026c2042b..d2cad7dc2eaba1aa495c557f7f3451a610ba3fa6 100644 --- a/spec/lib/gitlab/gon_helper_spec.rb +++ b/spec/lib/gitlab/gon_helper_spec.rb @@ -237,6 +237,36 @@ end end + describe '#push_application_setting' do + it 'pushes an application setting to the frontend' do + stub_application_setting(signup_enabled: true) + + gon = class_double('Gon') + allow(helper) + .to receive(:gon) + .and_return(gon) + + expect(gon) + .to receive(:push) + .with({ signup_enabled: true }) + + helper.push_application_setting(:signup_enabled) + end + + it 'does not push if missing application setting entry' do + gon = class_double('Gon') + allow(helper) + .to receive(:gon) + .and_return(gon) + + expect(gon) + .not_to receive(:push) + .with({ non_existent_application_setting: true }) + + helper.push_application_setting(:non_existent_application_setting) + end + end + describe '#default_avatar_url' do it 'returns an absolute URL' do url = helper.default_avatar_url