Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Email users when a new associated account is linked (#44382)

Keegan George8dece8319b1e
diff --git a/app/controllers/admin/email_templates_controller.rb b/app/controllers/admin/email_templates_controller.rb
index f70c413fb15..d356fd20f2c 100644
--- a/app/controllers/admin/email_templates_controller.rb
+++ b/app/controllers/admin/email_templates_controller.rb
@@ -89,6 +89,7 @@ class Admin::EmailTemplatesController < Admin::AdminController
system_messages.welcome_user
test_mailer
unsubscribe_mailer
+ user_notifications.account_associated
user_notifications.account_created
user_notifications.account_deleted
user_notifications.account_exists
diff --git a/app/controllers/users/associate_accounts_controller.rb b/app/controllers/users/associate_accounts_controller.rb
index 2eff12e26b1..54b3341d782 100644
--- a/app/controllers/users/associate_accounts_controller.rb
+++ b/app/controllers/users/associate_accounts_controller.rb
@@ -27,6 +27,15 @@ class Users::AssociateAccountsController < ApplicationController
server_session.delete(self.class.key(params[:token]))
+ if !auth_result.failed?
+ Jobs.enqueue(
+ :critical_user_email,
+ type: "account_associated",
+ user_id: current_user.id,
+ provider_name: authenticator.display_name,
+ )
+ end
+
render json: success_json
end
diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rb
index d97d4f73019..290c81738f9 100644
--- a/app/jobs/regular/user_email.rb
+++ b/app/jobs/regular/user_email.rb
@@ -220,6 +220,8 @@ module Jobs
email_args[:user_agent] = args[:user_agent]
end
+ email_args[:provider_name] = args[:provider_name] if args[:provider_name].present?
+
if EmailLog.reached_max_emails?(user, type)
return skip_message(SkippedEmailLog.reason_types[:exceeded_emails_limit])
end
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 15006974bf3..1fd74aadbb1 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -247,6 +247,16 @@ class UserNotifications < ActionMailer::Base
)
end
+ def account_associated(user, opts = {})
+ build_email(
+ user.email,
+ template: "user_notifications.account_associated",
+ locale: user_locale(user),
+ provider_name: opts[:provider_name],
+ recipient_user: user,
+ )
+ end
+
def account_second_factor_disabled(user, opts = {})
build_email(
user.email,
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index e7d6eeffc0c..8e1f7a220ce 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -5127,6 +5127,17 @@ en:
If you have any questions, [contact our friendly staff](%{base_url}/about).
+ account_associated:
+ title: "New associated account"
+ subject_template: "[%{email_prefix}] New associated account"
+ subject_template_improved: "New associated account"
+ text_body_template: |
+ A %{provider_name} account was just linked to your account at %{site_name}. It can now be used to log in.
+
+ If this was you, there’s nothing else you need to do.
+
+ If this was not you, someone may have access to your account. [Review your associated accounts](%{base_url}/my/preferences/account), [review your existing sessions](%{base_url}/my/preferences/security), and consider changing your password.
+
digest:
why: "A brief summary of %{site_link} since %{since}"
since_last_visit: "Since your last visit"
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index 7e562399f47..dfa73f3f0f6 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -2528,6 +2528,18 @@ RSpec.describe UserNotifications do
end
end
+ describe ".account_associated" do
+ it "names the provider the account was linked to" do
+ mail = UserNotifications.account_associated(user, provider_name: "Google")
+
+ expect(mail.to).to contain_exactly(user.email)
+ expect(mail.subject).to include(
+ I18n.t("user_notifications.account_associated.title", locale: :en),
+ )
+ expect(mail.body.encoded).to include("A Google account was just linked")
+ end
+ end
+
describe ".account_suspended" do
fab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:suspend_user]) }
diff --git a/spec/requests/associate_accounts_controller_spec.rb b/spec/requests/associate_accounts_controller_spec.rb
index fc4c2f02182..be25af15ad6 100644
--- a/spec/requests/associate_accounts_controller_spec.rb
+++ b/spec/requests/associate_accounts_controller_spec.rb
@@ -70,6 +70,28 @@ RSpec.describe Users::AssociateAccountsController do
expect(response.status).to eq(404)
end
+ it "emails the user when an account is associated" do
+ sign_in(user)
+
+ post "/auth/google_oauth2?reconnect=true"
+ OmniAuth.config.mock_auth[:google_oauth2].uid = "123456"
+ get "/auth/google_oauth2/callback.json"
+
+ uri = URI.parse(response.redirect_url)
+ get "#{uri.path}.json"
+
+ expect_enqueued_with(
+ job: :critical_user_email,
+ args: {
+ type: "account_associated",
+ user_id: user.id,
+ provider_name: "Google",
+ },
+ ) { post "#{uri.path}.json" }
+
+ expect(response.status).to eq(200)
+ end
+
it "restricts association to the current session" do
sign_in(user)