From 6936d94e587bbe3d729356061164fc256a800c4d Mon Sep 17 00:00:00 2001
From: eugielimpin <elimpin@gitlab.com>
Date: Thu, 30 Jun 2022 12:07:05 +0800
Subject: [PATCH] Update user_auto_banned_email to scope the email to a group

For SaaS, we auto-ban a user in a group when they exceed unique
project download limit set for that namespace. This version of
user_auto_banned_email (with group passed in) will be sent to group
owners when that happens.
---
 app/mailers/emails/admin_notification.rb         |  7 ++++++-
 app/mailers/previews/notify_preview.rb           | 10 +++++++++-
 .../notify/user_auto_banned_email.html.haml      |  2 +-
 app/views/notify/user_auto_banned_email.text.erb |  2 +-
 locale/gitlab.pot                                |  8 +++++++-
 spec/mailers/emails/admin_notification_spec.rb   | 16 +++++++++++++++-
 6 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/app/mailers/emails/admin_notification.rb b/app/mailers/emails/admin_notification.rb
index f44dd448a3556..9d02d4132a140 100644
--- a/app/mailers/emails/admin_notification.rb
+++ b/app/mailers/emails/admin_notification.rb
@@ -16,11 +16,16 @@ def send_unsubscribed_notification(user_id)
       mail to: email, subject: "Unsubscribed from GitLab administrator notifications"
     end
 
-    def user_auto_banned_email(admin_id, user_id, max_project_downloads:, within_seconds:)
+    def user_auto_banned_email(admin_id, user_id, max_project_downloads:, within_seconds:, group: nil)
       admin = User.find(admin_id)
       @user = User.find(user_id)
       @max_project_downloads = max_project_downloads
       @within_minutes = within_seconds / 60
+      @ban_scope = if group.present?
+                     _('your group (%{group_name})' % { group_name: group.name })
+                   else
+                     _('your GitLab instance')
+                   end
 
       Gitlab::I18n.with_locale(admin.preferred_language) do
         email_with_layout(
diff --git a/app/mailers/previews/notify_preview.rb b/app/mailers/previews/notify_preview.rb
index 61456ef79c82b..074aec54b1057 100644
--- a/app/mailers/previews/notify_preview.rb
+++ b/app/mailers/previews/notify_preview.rb
@@ -205,10 +205,14 @@ def inactive_project_deletion_warning
     Notify.inactive_project_deletion_warning_email(project, user, '2022-04-22').message
   end
 
-  def user_auto_banned_email
+  def user_auto_banned_instance_email
     ::Notify.user_auto_banned_email(user.id, user.id, max_project_downloads: 5, within_seconds: 600).message
   end
 
+  def user_auto_banned_namespace_email
+    ::Notify.user_auto_banned_email(user.id, user.id, max_project_downloads: 5, within_seconds: 600, group: group).message
+  end
+
   private
 
   def project
@@ -239,6 +243,10 @@ def user
     @user ||= User.last
   end
 
+  def group
+    @group ||= Group.last
+  end
+
   def member
     @member ||= Member.last
   end
diff --git a/app/views/notify/user_auto_banned_email.html.haml b/app/views/notify/user_auto_banned_email.html.haml
index d88c06526eb91..8c33cd7299db9 100644
--- a/app/views/notify/user_auto_banned_email.html.haml
+++ b/app/views/notify/user_auto_banned_email.html.haml
@@ -2,7 +2,7 @@
 - link_end = '</a>'.html_safe
 = email_default_heading(_("We've detected some unusual activity"))
 %p
-  = _('We want to let you know %{username} has been banned from your GitLab instance due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes.') % { username: sanitize_name(@user.name), max_project_downloads: @max_project_downloads, within_minutes: @within_minutes }
+  = _('We want to let you know %{username} has been banned from %{scope} due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes.') % { username: sanitize_name(@user.name), max_project_downloads: @max_project_downloads, within_minutes: @within_minutes, scope: @ban_scope }
 %p
   = _('If this is a mistake, you can %{link_start}unban them%{link_end}.').html_safe % { link_start: link_start % { url: admin_users_url(filter: 'banned') }, link_end: link_end }
 %p
diff --git a/app/views/notify/user_auto_banned_email.text.erb b/app/views/notify/user_auto_banned_email.text.erb
index 0469ee9788ce0..336973c2e42e0 100644
--- a/app/views/notify/user_auto_banned_email.text.erb
+++ b/app/views/notify/user_auto_banned_email.text.erb
@@ -1,6 +1,6 @@
 <%= _("We've detected some unusual activity") %>
 
-<%= _('We want to let you know %{username} has been banned from your GitLab instance due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes.') % { username: sanitize_name(@user.name), max_project_downloads: @max_project_downloads, within_minutes: @within_minutes } %>
+<%= _('We want to let you know %{username} has been banned from %{scope} due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes.') % { username: sanitize_name(@user.name), max_project_downloads: @max_project_downloads, within_minutes: @within_minutes, scope: @ban_scope } %>
 
 <%= _('If this is a mistake, you can unban them: %{url}.') % { url: admin_users_url(filter: 'banned') } %>
 
diff --git a/locale/gitlab.pot b/locale/gitlab.pot
index 1e1307276fed2..0c0a2e06dd495 100644
--- a/locale/gitlab.pot
+++ b/locale/gitlab.pot
@@ -42964,7 +42964,7 @@ msgstr ""
 msgid "We want to be sure it is you, please confirm you are not a robot."
 msgstr ""
 
-msgid "We want to let you know %{username} has been banned from your GitLab instance due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes."
+msgid "We want to let you know %{username} has been banned from %{scope} due to them downloading more than %{max_project_downloads} project repositories within %{within_minutes} minutes."
 msgstr ""
 
 msgid "We will notify %{inviter} that you declined their invitation to join GitLab. You will stop receiving reminders."
@@ -46710,6 +46710,12 @@ msgstr ""
 msgid "yaml invalid"
 msgstr ""
 
+msgid "your GitLab instance"
+msgstr ""
+
+msgid "your group (%{group_name})"
+msgstr ""
+
 msgid "your settings"
 msgstr ""
 
diff --git a/spec/mailers/emails/admin_notification_spec.rb b/spec/mailers/emails/admin_notification_spec.rb
index a233be86a83dc..bfd07f1d4389f 100644
--- a/spec/mailers/emails/admin_notification_spec.rb
+++ b/spec/mailers/emails/admin_notification_spec.rb
@@ -18,12 +18,14 @@
 
     let(:max_project_downloads) { 5 }
     let(:time_period) { 600 }
+    let(:group) { nil }
 
     subject do
       Notify.user_auto_banned_email(
         admin.id, user.id,
         max_project_downloads: max_project_downloads,
-        within_seconds: time_period
+        within_seconds: time_period,
+        group: group
       )
     end
 
@@ -45,6 +47,10 @@
       is_expected.to have_body_text user.name
     end
 
+    it 'includes the scope of the ban' do
+      is_expected.to have_body_text "banned from your GitLab instance"
+    end
+
     it 'includes the reason' do
       is_expected.to have_body_text "due to them downloading more than 5 project repositories within 10 minutes"
     end
@@ -60,5 +66,13 @@
     it 'includes the email reason' do
       is_expected.to have_body_text "You're receiving this email because of your account on localhost"
     end
+
+    context 'when scoped to a group' do
+      let(:group) { create(:group) }
+
+      it 'includes the scope of the ban' do
+        is_expected.to have_body_text "banned from your group (#{group.name})"
+      end
+    end
   end
 end
-- 
GitLab