Commit 89234f31 authored by Corinna Wiesner's avatar Corinna Wiesner Committed by James Lopez

Send email when user cap is reached

When the user cap is reached a banner in the UI already notifies the
admins about it. Now the admins will also receive an email as soon as
the user cap is reached.
parent 4c64d3fc
...@@ -12,6 +12,7 @@ module EE ...@@ -12,6 +12,7 @@ module EE
include ::Emails::AdminNotification include ::Emails::AdminNotification
include ::Emails::Epics include ::Emails::Epics
include ::Emails::Requirements include ::Emails::Requirements
include ::Emails::UserCap
end end
attr_reader :group attr_reader :group
......
# frozen_string_literal: true
module Emails
module UserCap
def user_cap_reached(user_id)
user = User.find(user_id)
email = user.notification_email
@url_to_user_cap = 'https://docs.gitlab.com/ee/user/admin_area/settings/sign_up_restrictions.html#user-cap'
@url_to_pending_users = 'https://docs.gitlab.com/ee/user/admin_area/approving_users.html#view-user-sign-ups-pending-approval'
@url_to_manage_pending_users = 'https://docs.gitlab.com/ee/user/admin_area/approving_users.html#approve-or-reject-a-user-sign-up'
@url_to_adjust_user_cap = 'https://docs.gitlab.com/ee/user/admin_area/settings/sign_up_restrictions.html#set-the-user-cap-number'
@url_to_docs = 'https://docs.gitlab.com/'
@url_to_support = 'https://about.gitlab.com/support/'
mail to: email, subject: s_('AdminUsers|Important information about usage on your GitLab instance')
end
end
end
%p
= s_('AdminUsers|Your GitLab instance has reached the maximum allowed %{user_doc_link} set by an instance admin.').html_safe % { user_doc_link: link_to(s_('AdminUsers|user cap'), @url_to_user_cap) }
%p
%b= s_('AdminUsers|What does this mean?')
= s_('AdminUsers|Users can still be invited to your instance and/or add themselves if permitted based on your settings. They will not have access to your instance, nor count towards your subscribed seat count until you %{approve_link}.').html_safe % { approve_link: link_to(s_('AdminUsers|approve them'), @url_to_manage_pending_users) }
%p
%b= s_('AdminUsers|What can I do?')
= s_('AdminUsers|Here are some helpful links to help you manage your instance:')
%ul
%li= link_to s_('AdminUsers|View pending member requests'), @url_to_pending_users
%li= link_to s_('AdminUsers|Manage (accept/reject) pending user sign ups'), @url_to_manage_pending_users
%li= link_to s_('AdminUsers|Adjust the user cap setting on your instance'), @url_to_adjust_user_cap
%p
= s_('AdminUsers|If you have any questions about this process please consult our %{doc_link} or %{support_link}.').html_safe % { doc_link: link_to(s_('AdminUsers|docs'), @url_to_docs), support_link: link_to(s_('AdminUsers|contact our support team'), @url_to_support) }
<%= s_('AdminUsers|Your GitLab instance has reached the maximum allowed %{user_doc_link} set by an instance admin.') % { user_doc_link: "#{s_('AdminUsers|user cap')} #{@url_to_user_cap}" } %>
<%= s_('AdminUsers|What does this mean?') %>
<%= s_('AdminUsers|Users can still be invited to your instance and/or add themselves if permitted based on your settings. They will not have access to your instance, nor count towards your subscribed seat count until you %{approve_link}.') % { approve_link: "#{s_('AdminUsers|approve them')} #{@url_to_manage_pending_users}" } %>
<%= s_('AdminUsers|What can I do?') %>
<%= s_('AdminUsers|Here are some helpful links to help you manage your instance:') %>
<%= s_('AdminUsers|View pending member requests') %> <%= @url_to_pending_users %>
<%= s_('AdminUsers|Manage (accept/reject) pending user sign ups') %> <%= @url_to_manage_pending_users %>
<%= s_('AdminUsers|Adjust the user cap setting on your instance') %> <%= @url_to_adjust_user_cap %>
<%= s_('AdminUsers|If you have any questions about this process please consult our %{doc_link} or %{support_link}.') % { doc_link: "#{s_('AdminUsers|docs')} #{@url_to_docs}", support_link: "#{s_('AdminUsers|contact our support team')} #{@url_to_support}" } %>
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
class SetUserStatusBasedOnUserCapSettingWorker class SetUserStatusBasedOnUserCapSettingWorker
include ApplicationWorker include ApplicationWorker
include ::Gitlab::Utils::StrongMemoize
feature_category :users feature_category :users
...@@ -12,7 +13,11 @@ class SetUserStatusBasedOnUserCapSettingWorker ...@@ -12,7 +13,11 @@ class SetUserStatusBasedOnUserCapSettingWorker
return unless user.blocked_pending_approval? return unless user.blocked_pending_approval?
return if user_cap_max.nil? return if user_cap_max.nil?
return if current_billable_users_count >= user_cap_max
if user_cap_reached?
send_user_cap_reached_email
return
end
if user.activate if user.activate
# Resends confirmation email if the user isn't confirmed yet. # Resends confirmation email if the user isn't confirmed yet.
...@@ -28,10 +33,22 @@ class SetUserStatusBasedOnUserCapSettingWorker ...@@ -28,10 +33,22 @@ class SetUserStatusBasedOnUserCapSettingWorker
private private
def user_cap_max def user_cap_max
::Gitlab::CurrentSettings.new_user_signups_cap strong_memoize(:user_cap_max) do
::Gitlab::CurrentSettings.new_user_signups_cap
end
end end
def current_billable_users_count def current_billable_users_count
User.billable.count User.billable.count
end end
def user_cap_reached?
current_billable_users_count >= user_cap_max
end
def send_user_cap_reached_email
User.admins.active.each do |user|
::Notify.user_cap_reached(user.id).deliver_later
end
end
end end
---
title: Send email when user cap is reached
merge_request: 53489
author:
type: changed
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Emails::UserCap do
include EmailSpec::Matchers
let_it_be(:user) { create(:user) }
describe "#user_cap_reached" do
subject { Notify.user_cap_reached(user.id) }
it { is_expected.to have_subject('Important information about usage on your GitLab instance') }
it { is_expected.to be_delivered_to([user.notification_email]) }
it { is_expected.to have_body_text('Your GitLab instance has reached the maximum allowed') }
it { is_expected.to have_body_text('user cap') }
end
end
...@@ -15,6 +15,25 @@ RSpec.describe SetUserStatusBasedOnUserCapSettingWorker, type: :worker do ...@@ -15,6 +15,25 @@ RSpec.describe SetUserStatusBasedOnUserCapSettingWorker, type: :worker do
allow(Gitlab::CurrentSettings).to receive(:new_user_signups_cap).and_return(new_user_signups_cap) allow(Gitlab::CurrentSettings).to receive(:new_user_signups_cap).and_return(new_user_signups_cap)
end end
shared_examples 'keeps user in blocked_pending_approval state' do
it 'keeps the user in blocked_pending_approval state' do
subject
expect(user.reload).to be_blocked_pending_approval
end
end
shared_examples 'sends email to every active admin' do
let_it_be(:active_admin) { create(:user, :admin, state: 'active') }
let_it_be(:inactive_admin) { create(:user, :admin, :deactivated) }
it 'sends an email to every active admin' do
expect(::Notify).to receive(:user_cap_reached).with(active_admin.id).once.and_call_original
subject
end
end
context 'when user is not blocked_pending_approval' do context 'when user is not blocked_pending_approval' do
let(:user) { active_user } let(:user) { active_user }
...@@ -98,13 +117,19 @@ RSpec.describe SetUserStatusBasedOnUserCapSettingWorker, type: :worker do ...@@ -98,13 +117,19 @@ RSpec.describe SetUserStatusBasedOnUserCapSettingWorker, type: :worker do
end end
context 'when current billable user count is equal to user cap' do context 'when current billable user count is equal to user cap' do
let(:new_user_signups_cap) { 1 } let(:new_user_signups_cap) { 2 }
it 'keeps the user in blocked_pending_approval state' do include_examples 'keeps user in blocked_pending_approval state'
subject include_examples 'sends email to every active admin'
end
expect(user.reload).to be_blocked_pending_approval context 'when current billable user count is greater than user cap' do
end let_it_be(:another_active_user) { create(:user, state: 'active') }
let(:new_user_signups_cap) { 1 }
include_examples 'keeps user in blocked_pending_approval state'
include_examples 'sends email to every active admin'
end end
end end
end end
...@@ -2297,6 +2297,9 @@ msgstr "" ...@@ -2297,6 +2297,9 @@ msgstr ""
msgid "AdminUsers|Active" msgid "AdminUsers|Active"
msgstr "" msgstr ""
msgid "AdminUsers|Adjust the user cap setting on your instance"
msgstr ""
msgid "AdminUsers|Admin" msgid "AdminUsers|Admin"
msgstr "" msgstr ""
...@@ -2390,6 +2393,15 @@ msgstr "" ...@@ -2390,6 +2393,15 @@ msgstr ""
msgid "AdminUsers|For more information, please refer to the %{link_start}user account deletion documentation.%{link_end}" msgid "AdminUsers|For more information, please refer to the %{link_start}user account deletion documentation.%{link_end}"
msgstr "" msgstr ""
msgid "AdminUsers|Here are some helpful links to help you manage your instance:"
msgstr ""
msgid "AdminUsers|If you have any questions about this process please consult our %{doc_link} or %{support_link}."
msgstr ""
msgid "AdminUsers|Important information about usage on your GitLab instance"
msgstr ""
msgid "AdminUsers|Is using seat" msgid "AdminUsers|Is using seat"
msgstr "" msgstr ""
...@@ -2399,6 +2411,9 @@ msgstr "" ...@@ -2399,6 +2411,9 @@ msgstr ""
msgid "AdminUsers|Log in" msgid "AdminUsers|Log in"
msgstr "" msgstr ""
msgid "AdminUsers|Manage (accept/reject) pending user sign ups"
msgstr ""
msgid "AdminUsers|New user" msgid "AdminUsers|New user"
msgstr "" msgstr ""
...@@ -2498,6 +2513,18 @@ msgstr "" ...@@ -2498,6 +2513,18 @@ msgstr ""
msgid "AdminUsers|Users" msgid "AdminUsers|Users"
msgstr "" msgstr ""
msgid "AdminUsers|Users can still be invited to your instance and/or add themselves if permitted based on your settings. They will not have access to your instance, nor count towards your subscribed seat count until you %{approve_link}."
msgstr ""
msgid "AdminUsers|View pending member requests"
msgstr ""
msgid "AdminUsers|What can I do?"
msgstr ""
msgid "AdminUsers|What does this mean?"
msgstr ""
msgid "AdminUsers|When the user logs back in, their account will reactivate as a fully active account" msgid "AdminUsers|When the user logs back in, their account will reactivate as a fully active account"
msgstr "" msgstr ""
...@@ -2531,6 +2558,21 @@ msgstr "" ...@@ -2531,6 +2558,21 @@ msgstr ""
msgid "AdminUsers|You must transfer ownership or delete the groups owned by this user before you can delete their account" msgid "AdminUsers|You must transfer ownership or delete the groups owned by this user before you can delete their account"
msgstr "" msgstr ""
msgid "AdminUsers|Your GitLab instance has reached the maximum allowed %{user_doc_link} set by an instance admin."
msgstr ""
msgid "AdminUsers|approve them"
msgstr ""
msgid "AdminUsers|contact our support team"
msgstr ""
msgid "AdminUsers|docs"
msgstr ""
msgid "AdminUsers|user cap"
msgstr ""
msgid "Administration" msgid "Administration"
msgstr "" msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment