From 265ce2d8a5dae6e79e60dd330d1491689b2936a7 Mon Sep 17 00:00:00 2001
From: Ahmad Farhat <ahmad.af.farhat@gmail.com>
Date: Mon, 20 Nov 2023 11:26:58 -0500
Subject: [PATCH] Add a new role permission for receiving an email on user
 signup (#5529)

* Add a new role permission for receiving an email on user signup

* Fix text email

* rubo
---
 app/assets/locales/en.json                    |  3 +-
 app/controllers/api/v1/users_controller.rb    |  3 ++
 app/controllers/concerns/client_routable.rb   |  4 +++
 .../admin/roles/forms/EditRoleForm.jsx        |  7 ++++
 app/mailers/user_mailer.rb                    | 21 ++++++++++-
 .../new_user_signup_email.html.erb            | 30 ++++++++++++++++
 .../new_user_signup_email.text.erb            | 26 ++++++++++++++
 config/locales/en.yml                         |  7 ++++
 ...7151542_add_email_on_sign_up_permission.rb | 36 +++++++++++++++++++
 db/data_schema.rb                             |  2 +-
 spec/controllers/users_controller_spec.rb     | 10 ++++++
 test/mailers/previews/user_mailer_preview.rb  |  6 ++++
 12 files changed, 152 insertions(+), 3 deletions(-)
 create mode 100644 app/views/user_mailer/new_user_signup_email.html.erb
 create mode 100644 app/views/user_mailer/new_user_signup_email.text.erb
 create mode 100644 db/data/20231117151542_add_email_on_sign_up_permission.rb

diff --git a/app/assets/locales/en.json b/app/assets/locales/en.json
index 162b628a..bc52d396 100644
--- a/app/assets/locales/en.json
+++ b/app/assets/locales/en.json
@@ -350,7 +350,8 @@
         "manage_site_settings": "Allow users with this role to manage site settings",
         "manage_roles": "Allow users with this role to edit other roles",
         "shared_list": "Include users with this role in the dropdown for sharing rooms",
-        "room_limit": "Room Limit"
+        "room_limit": "Room Limit",
+        "email_on_signup": "Receive an email when a new user signs up"
       }
     }
   },
diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb
index 98b52024..a99129cc 100644
--- a/app/controllers/api/v1/users_controller.rb
+++ b/app/controllers/api/v1/users_controller.rb
@@ -76,6 +76,9 @@ module Api
             UserMailer.with(user:,
                             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
           end
 
           create_default_room(user)
diff --git a/app/controllers/concerns/client_routable.rb b/app/controllers/concerns/client_routable.rb
index 32321642..f9fc4f3b 100644
--- a/app/controllers/concerns/client_routable.rb
+++ b/app/controllers/concerns/client_routable.rb
@@ -33,4 +33,8 @@ module ClientRoutable
   def pending_path
     "#{root_path}pending"
   end
+
+  def admin_panel_url
+    "#{root_url}admin/users"
+  end
 end
diff --git a/app/javascript/components/admin/roles/forms/EditRoleForm.jsx b/app/javascript/components/admin/roles/forms/EditRoleForm.jsx
index 08101684..5e90df42 100644
--- a/app/javascript/components/admin/roles/forms/EditRoleForm.jsx
+++ b/app/javascript/components/admin/roles/forms/EditRoleForm.jsx
@@ -130,6 +130,13 @@ export default function EditRoleForm({ role }) {
                 defaultValue={rolePermissions?.SharedList === 'true'}
               />
 
+              <RolePermissionRow
+                permissionName="EmailOnSignup"
+                description={t('admin.roles.edit.email_on_signup')}
+                roleId={role?.id}
+                defaultValue={rolePermissions?.EmailOnSignup === 'true'}
+              />
+
               <Form methods={methodsLimit} onBlur={methodsLimit.handleSubmit(updatePermissionAPI.mutate)}>
                 <Stack direction="horizontal">
                   <div className="text-muted me-auto">
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 3e00b6fc..fb530613 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -42,11 +42,20 @@ class UserMailer < ApplicationMailer
     @email = params[:email]
     @name = params[:name]
     @signup_url = params[:signup_url]
-    @email = params[:email]
 
     mail(to: @email, subject: t('email.invitation.invitation_to_join'))
   end
 
+  def new_user_signup_email
+    @user = params[:user]
+    @admin_panel_url = params[:admin_panel_url]
+    emails = admin_emails
+
+    return if emails.blank? # Dont send anything if no-one has EmailOnSignup enabled
+
+    mail(to: emails, subject: t('email.new_user_signup.new_user'))
+  end
+
   private
 
   def preset
@@ -59,4 +68,14 @@ class UserMailer < ApplicationMailer
     @brand_image = ActionController::Base.helpers.image_url(branding_hash['BrandingImage'], host: @base_url)
     @brand_color = branding_hash['PrimaryColor']
   end
+
+  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' })
+                   .pluck(:id)
+
+    User.where(role_id: role_ids).pluck(:email)
+  end
 end
diff --git a/app/views/user_mailer/new_user_signup_email.html.erb b/app/views/user_mailer/new_user_signup_email.html.erb
new file mode 100644
index 00000000..acdff87f
--- /dev/null
+++ b/app/views/user_mailer/new_user_signup_email.html.erb
@@ -0,0 +1,30 @@
+<!--BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.-->
+
+<!--Copyright (c) 2022 BigBlueButton Inc. and by respective authors (see below).-->
+
+<!--This program is free software; you can redistribute it and/or modify it under the-->
+<!--terms of the GNU Lesser General Public License as published by the Free Software-->
+<!--Foundation; either version 3.0 of the License, or (at your option) any later-->
+<!--version.-->
+
+<!--Greenlight is distributed in the hope that it will be useful, but WITHOUT ANY-->
+<!--WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A-->
+<!--PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.-->
+
+<!--You should have received a copy of the GNU Lesser General Public License along-->
+<!--with Greenlight; if not, see <http://www.gnu.org/licenses/>.-->
+
+<div style="padding-left: 80px; padding-right: 80px;">
+  <p style="font-size: 40px; margin-bottom: 20px; font-weight: 600;"><%= t('email.new_user_signup.new_user') %></p>
+
+  <p style="font-size: 24px;"><%= t('email.new_user_signup.new_user_description') %></p>
+
+  <p style="font-size: 20px;"><%= t('email.new_user_signup.name', name: @user.name) %></p>
+  <p style="font-size: 20px;"><%= t('email.new_user_signup.email', email: @user.email) %></p>
+
+  <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;">
+    <%= 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
new file mode 100644
index 00000000..bf3f1237
--- /dev/null
+++ b/app/views/user_mailer/new_user_signup_email.text.erb
@@ -0,0 +1,26 @@
+<%#
+  BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
+
+  Copyright (c) 2022 BigBlueButton Inc. and by respective authors (see below).
+
+  This program is free software; you can redistribute it and/or modify it under the
+  terms of the GNU Lesser General Public License as published by the Free Software
+  Foundation; either version 3.0 of the License, or (at your option) any later
+  version.
+
+  Greenlight is distributed in the hope that it will be useful, but WITHOUT ANY
+  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+  PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License along
+  with Greenlight; if not, see http://www.gnu.org/licenses/.
+%>
+
+---
+<%= t('email.new_user_signup.new_user') %>
+<%= 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 %>
+---
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 98393108..ba04fe59 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -65,6 +65,13 @@ en:
       get_started: To sign up, please click the button below and follow the steps.
       valid_invitation: The invitation is valid for 24 hours.
       sign_up: Sign Up
+    new_user_signup:
+      new_user: New User Signup
+      new_user_description: A new user has signed up to use BigBlueButton.
+      name: "Name: %{name}"
+      email: "Email: %{email}"
+      admin_panel: "Administrator Panel"
+      take_action: "To view the new user or to take the necessary action, visit the Administrator Panel"
     reset:
       password_reset: Reset Password
       password_reset_requested: A password reset has been requested for %{email}.
diff --git a/db/data/20231117151542_add_email_on_sign_up_permission.rb b/db/data/20231117151542_add_email_on_sign_up_permission.rb
new file mode 100644
index 00000000..d2bd13e1
--- /dev/null
+++ b/db/data/20231117151542_add_email_on_sign_up_permission.rb
@@ -0,0 +1,36 @@
+# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
+#
+# Copyright (c) 2022 BigBlueButton Inc. and by respective authors (see below).
+#
+# This program is free software; you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free Software
+# Foundation; either version 3.0 of the License, or (at your option) any later
+# version.
+#
+# Greenlight is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with Greenlight; if not, see <http://www.gnu.org/licenses/>.
+
+# frozen_string_literal: true
+
+class AddEmailOnSignUpPermission < ActiveRecord::Migration[7.1]
+  def up
+    email_permission = Permission.create!(name: 'EmailOnSignup')
+    admin = Role.find_by(name: 'Administrator')
+
+    values = [{ role: admin, permission: email_permission, value: 'true' }]
+
+    Role.where.not(name: 'Administrator').each do |role|
+      values.push({ role:, permission: email_permission, value: 'false' })
+    end
+
+    RolePermission.create! values
+  end
+
+  def down
+    raise ActiveRecord::IrreversibleMigration
+  end
+end
diff --git a/db/data_schema.rb b/db/data_schema.rb
index 5e92e7bf..d9e08cdf 100644
--- a/db/data_schema.rb
+++ b/db/data_schema.rb
@@ -1 +1 @@
-DataMigrate::Data.define(version: 20231030185844)
+DataMigrate::Data.define(version: 20231117151542)
diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb
index e7f59cde..81a9d594 100644
--- a/spec/controllers/users_controller_spec.rb
+++ b/spec/controllers/users_controller_spec.rb
@@ -66,6 +66,16 @@ RSpec.describe Api::V1::UsersController, type: :controller do
         expect(User.find_by(email: user_params[:user][:email]).role.name).to eq('User')
       end
 
+      context 'EmailOnSignup' do
+        it 'emails all admins that a new user has signed up' do
+          post :create, params: user_params
+
+          expect(ActionMailer::MailDeliveryJob).to have_been_enqueued
+                                               .at(:no_wait).exactly(:once)
+            .with('UserMailer', 'new_user_signup_email', 'deliver_now', Hash)
+        end
+      end
+
       context 'User language' do
         it 'Persists the user language in the user record' do
           post :create, params: user_params
diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb
index 58726734..f7161ee0 100644
--- a/test/mailers/previews/user_mailer_preview.rb
+++ b/test/mailers/previews/user_mailer_preview.rb
@@ -38,4 +38,10 @@ class UserMailerPreview < ActionMailer::Preview
 
     UserMailer.with(user: fake_user.new('user', 'user@users'), invitation_url: 'https://example.com/invite').invitation_email
   end
+
+  def new_user_signup_email
+    fake_user = Struct.new(:name, :email)
+
+    UserMailer.with(user: fake_user.new('user', 'user@users')).new_user_signup_email
+  end
 end
-- 
GitLab