From 7d3bfc1963e47f4f916a51772c069a7413d5bd88 Mon Sep 17 00:00:00 2001
From: Ahmad Farhat <ahmad.af.farhat@gmail.com>
Date: Wed, 22 Nov 2023 13:00:46 -0500
Subject: [PATCH] Add email notification to external users (#5565)

---
 .rubocop.yml                                             | 4 ++--
 app/controllers/api/v1/migrations/external_controller.rb | 2 --
 app/controllers/api/v1/users_controller.rb               | 3 +--
 app/controllers/external_controller.rb                   | 3 +++
 app/mailers/user_mailer.rb                               | 5 ++---
 app/views/user_mailer/new_user_signup_email.html.erb     | 2 +-
 app/views/user_mailer/new_user_signup_email.text.erb     | 4 ++--
 7 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/.rubocop.yml b/.rubocop.yml
index db58bc26..1fed7150 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -70,7 +70,7 @@ Metrics/ClassLength:
 # A calculated magnitude based on number of assignments,
 # branches, and conditions.
 Metrics/AbcSize:
-  Max: 75
+  Max: 80
 
 Metrics/ParameterLists:
   CountKeywordArgs: false
@@ -79,7 +79,7 @@ RSpec/AnyInstance:
   Enabled: false
 
 Metrics/CyclomaticComplexity:
-  Max: 16
+  Max: 17
 
 Metrics/PerceivedComplexity:
   Max: 17
diff --git a/app/controllers/api/v1/migrations/external_controller.rb b/app/controllers/api/v1/migrations/external_controller.rb
index 717af969..d15956d3 100644
--- a/app/controllers/api/v1/migrations/external_controller.rb
+++ b/app/controllers/api/v1/migrations/external_controller.rb
@@ -115,7 +115,6 @@ module Api
         #                    shared_users_emails: [ <list of shared users emails> ] }}
         # Returns: { data: Array[serializable objects] , errors: Array[String] }
         # Does: Creates a Room and its RoomMeetingOptions.
-        # rubocop:disable Metrics/CyclomaticComplexity
         def create_room
           room_hash = room_params.to_h
 
@@ -170,7 +169,6 @@ module Api
 
           render_data status: :created
         end
-        # rubocop:enable Metrics/CyclomaticComplexity
 
         # POST /api/v1/migrations/site_settings.json
         # Expects: { settings: { site_settings: { :PrimaryColor, :PrimaryColorLight, :PrimaryColorDark,
diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb
index a99129cc..558dda4a 100644
--- a/app/controllers/api/v1/users_controller.rb
+++ b/app/controllers/api/v1/users_controller.rb
@@ -77,8 +77,7 @@ module Api
                             activation_url: activate_account_url(token), base_url: request.base_url,
                             provider: current_provider).activate_account_email.deliver_later
 
-            UserMailer.with(user:, admin_panel_url:, provider: current_provider)
-                      .new_user_signup_email.deliver_later
+            UserMailer.with(user:, admin_panel_url:, provider: current_provider).new_user_signup_email.deliver_later
           end
 
           create_default_room(user)
diff --git a/app/controllers/external_controller.rb b/app/controllers/external_controller.rb
index a4b87b6a..0c6e2bbe 100644
--- a/app/controllers/external_controller.rb
+++ b/app/controllers/external_controller.rb
@@ -53,6 +53,9 @@ class ExternalController < ApplicationController
       user = UserCreator.new(user_params: user_info, provider: current_provider, role: default_role).call
       user.save!
       create_default_room(user)
+
+      # Send admins an email if smtp is enabled
+      UserMailer.with(user:, admin_panel_url:, provider: current_provider).new_user_signup_email.deliver_later if ENV['SMTP_SERVER'].present?
     end
 
     if SettingGetter.new(setting_name: 'ResyncOnLogin', provider:).call
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index fb530613..e0ed21fb 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -71,9 +71,8 @@ class UserMailer < ApplicationMailer
 
   def admin_emails
     # Find all the roles that have EmailOnSignup enabled
-    role_ids = Role.joins(role_permissions: :permission)
-                   .with_provider(@provider)
-                   .where(role_permissions: { value: 'true' }, permission: { name: 'EmailOnSignup' })
+    role_ids = Role.joins(role_permissions: :permission).with_provider(@provider).where(role_permissions: { value: 'true' },
+                                                                                        permission: { name: 'EmailOnSignup' })
                    .pluck(:id)
 
     User.where(role_id: role_ids).pluck(:email)
diff --git a/app/views/user_mailer/new_user_signup_email.html.erb b/app/views/user_mailer/new_user_signup_email.html.erb
index acdff87f..8c17ace3 100644
--- a/app/views/user_mailer/new_user_signup_email.html.erb
+++ b/app/views/user_mailer/new_user_signup_email.html.erb
@@ -24,7 +24,7 @@
 
   <p style="font-size: 24px;"><%= t('email.new_user_signup.take_action') %></p>
 
-  <a href="<%= @signup_url %>" target="_blank" style="background-color: <%= @brand_color %>; border-radius: 8px; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; font-weight: 600; margin-top: 16px; margin-bottom: 64px;">
+  <a href="<%= @admin_panel_url %>" target="_blank" style="background-color: <%= @brand_color %>; border-radius: 8px; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; font-weight: 600; margin-top: 16px; margin-bottom: 64px;">
     <%= t('email.new_user_signup.admin_panel') %>
   </a>
 </div>
diff --git a/app/views/user_mailer/new_user_signup_email.text.erb b/app/views/user_mailer/new_user_signup_email.text.erb
index bf3f1237..1d3b468c 100644
--- a/app/views/user_mailer/new_user_signup_email.text.erb
+++ b/app/views/user_mailer/new_user_signup_email.text.erb
@@ -18,9 +18,9 @@
 
 ---
 <%= t('email.new_user_signup.new_user') %>
-<%= t('email.new_user_signup.new_user_description' %>
+<%= t('email.new_user_signup.new_user_description') %>
 <%= t('email.new_user_signup.name', name: @user.name) %>
 <%= t('email.new_user_signup.email', email: @user.email) %>
 <%= t('email.new_user_signup.take_action') %>
-<%= @signup_url %>
+<%= @admin_panel_url %>
 ---
-- 
GitLab