From 6cfb448c1f367cdd29bf944491971334abbe6db0 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Fri, 8 May 2020 18:35:06 -0300 Subject: [PATCH] [FIX] Email configs not updating after setting changes (#17578) --- server/lib/accounts.js | 46 +++++++++++++++++------ server/methods/sendConfirmationEmail.js | 27 +------------ server/methods/sendForgotPasswordEmail.js | 37 +----------------- 3 files changed, 38 insertions(+), 72 deletions(-) diff --git a/server/lib/accounts.js b/server/lib/accounts.js index 95e3aa4bec90..951e360254aa 100644 --- a/server/lib/accounts.js +++ b/server/lib/accounts.js @@ -13,16 +13,21 @@ import { Users as UsersRaw } from '../../app/models/server/raw'; import { addUserRoles } from '../../app/authorization'; import { getAvatarSuggestionForUser } from '../../app/lib/server/functions'; -const accountsConfig = { +Accounts.config({ forbidClientAccountCreation: true, - loginExpirationInDays: settings.get('Accounts_LoginExpiration'), -}; +}); -Accounts.config(accountsConfig); +const updateMailConfig = _.debounce(() => { + Accounts._options.loginExpirationInDays = settings.get('Accounts_LoginExpiration'); -Accounts.emailTemplates.siteName = settings.get('Site_Name'); + Accounts.emailTemplates.siteName = settings.get('Site_Name'); -Accounts.emailTemplates.from = `${ settings.get('Site_Name') } <${ settings.get('From_Email') }>`; + Accounts.emailTemplates.from = `${ settings.get('Site_Name') } <${ settings.get('From_Email') }>`; +}, 1000); + +Meteor.startup(() => { + settings.get(/^(Accounts_LoginExpiration|Site_Name|From_Email)$/, updateMailConfig); +}); Accounts.emailTemplates.userToActivate = { subject() { @@ -63,10 +68,9 @@ Accounts.emailTemplates.userActivated = { }, }; - -// const verifyEmailHtml = Accounts.emailTemplates.verifyEmail.html; let verifyEmailTemplate = ''; let enrollAccountTemplate = ''; +let resetPasswordTemplate = ''; Meteor.startup(() => { Mailer.getTemplateWrapped('Verification_Email', (value) => { verifyEmailTemplate = value; @@ -74,17 +78,35 @@ Meteor.startup(() => { Mailer.getTemplateWrapped('Accounts_Enrollment_Email', (value) => { enrollAccountTemplate = value; }); + Mailer.getTemplateWrapped('Forgot_Password_Email', (value) => { + resetPasswordTemplate = value; + }); }); -Accounts.emailTemplates.verifyEmail.html = function(user, url) { - url = url.replace(Meteor.absoluteUrl(), `${ Meteor.absoluteUrl() }login/`); - return Mailer.replace(verifyEmailTemplate, { Verification_Url: url }); + +Accounts.emailTemplates.verifyEmail.html = function(userModel, url) { + return Mailer.replace(verifyEmailTemplate, { Verification_Url: url, name: userModel.name }); +}; + +Accounts.emailTemplates.verifyEmail.subject = function() { + const subject = settings.get('Verification_Email_Subject'); + return Mailer.replace(subject || ''); }; Accounts.urls.resetPassword = function(token) { return Meteor.absoluteUrl(`reset-password/${ token }`); }; -Accounts.emailTemplates.resetPassword.html = Accounts.emailTemplates.resetPassword.text; +Accounts.emailTemplates.resetPassword.subject = function(userModel) { + return Mailer.replace(settings.get('Forgot_Password_Email_Subject') || '', { + name: userModel.name, + }); +}; + +Accounts.emailTemplates.resetPassword.html = function(userModel, url) { + return Mailer.replacekey(Mailer.replace(resetPasswordTemplate, { + name: userModel.name, + }), 'Forgot_Password_Url', url); +}; Accounts.emailTemplates.enrollAccount.subject = function(user) { const subject = settings.get('Accounts_Enrollment_Email_Subject'); diff --git a/server/methods/sendConfirmationEmail.js b/server/methods/sendConfirmationEmail.js index 5749d0e58690..4bcfbad7c454 100644 --- a/server/methods/sendConfirmationEmail.js +++ b/server/methods/sendConfirmationEmail.js @@ -2,44 +2,21 @@ import { Meteor } from 'meteor/meteor'; import { check } from 'meteor/check'; import { Accounts } from 'meteor/accounts-base'; -import * as Mailer from '../../app/mailer'; import { Users } from '../../app/models'; -import { settings } from '../../app/settings'; - -let subject = ''; -let html = ''; - -Meteor.startup(() => { - settings.get('Verification_Email_Subject', function(key, value) { - subject = Mailer.replace(value || ''); - }); - - Mailer.getTemplateWrapped('Verification_Email', function(value) { - html = value; - }); -}); Meteor.methods({ sendConfirmationEmail(to) { check(to, String); const email = to.trim(); - const user = Users.findOneByEmailAddress(email); + const user = Users.findOneByEmailAddress(email, { fields: { _id: 1 } }); if (!user) { return false; } - Accounts.emailTemplates.verifyEmail.subject = function(/* userModel*/) { - return subject; - }; - - Accounts.emailTemplates.verifyEmail.html = function(userModel, url) { - return Mailer.replace(html, { Verification_Url: url, name: user.name }); - }; - try { - return Accounts.sendVerificationEmail(user._id, email); + return !!Accounts.sendVerificationEmail(user._id, email); } catch (error) { throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${ error.message }`, { method: 'registerUser', diff --git a/server/methods/sendForgotPasswordEmail.js b/server/methods/sendForgotPasswordEmail.js index c8ba6c8c3403..99b66b532c1c 100644 --- a/server/methods/sendForgotPasswordEmail.js +++ b/server/methods/sendForgotPasswordEmail.js @@ -1,55 +1,22 @@ import { Meteor } from 'meteor/meteor'; import { check } from 'meteor/check'; import { Accounts } from 'meteor/accounts-base'; -import s from 'underscore.string'; -import * as Mailer from '../../app/mailer'; import { Users } from '../../app/models'; -import { settings } from '../../app/settings'; - -let template = ''; - -Meteor.startup(() => { - Mailer.getTemplateWrapped('Forgot_Password_Email', (value) => { - template = value; - }); -}); Meteor.methods({ sendForgotPasswordEmail(to) { check(to, String); - let email = to.trim(); + const email = to.trim(); - const user = Users.findOneByEmailAddress(email); + const user = Users.findOneByEmailAddress(email, { fields: { _id: 1 } }); if (!user) { return false; } - const regex = new RegExp(`^${ s.escapeRegExp(email) }$`, 'i'); - email = (user.emails || []).map((item) => item.address).find((userEmail) => regex.test(userEmail)); - - const subject = Mailer.replace(settings.get('Forgot_Password_Email_Subject') || '', { - name: user.name, - email, - }); - - const html = Mailer.replace(template, { - name: user.name, - email, - }); - - Accounts.emailTemplates.from = `${ settings.get('Site_Name') } <${ settings.get('From_Email') }>`; - try { - Accounts.emailTemplates.resetPassword.subject = function(/* userModel*/) { - return subject; // TODO check a better way to do this - }; - - Accounts.emailTemplates.resetPassword.html = function(userModel, url) { - return Mailer.replacekey(html, 'Forgot_Password_Url', url); - }; return !!Accounts.sendResetPasswordEmail(user._id, email); } catch (error) { throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${ error.message }`, {