Skip to content

Commit

Permalink
[FIX] Email configs not updating after setting changes (#17578)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok authored and sampaiodiego committed May 11, 2020
1 parent 15ac767 commit cbebe5a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 72 deletions.
46 changes: 34 additions & 12 deletions server/lib/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -63,28 +68,45 @@ Accounts.emailTemplates.userActivated = {
},
};


// const verifyEmailHtml = Accounts.emailTemplates.verifyEmail.html;
let verifyEmailTemplate = '';
let enrollAccountTemplate = '';
let resetPasswordTemplate = '';
Meteor.startup(() => {
Mailer.getTemplateWrapped('Verification_Email', (value) => {
verifyEmailTemplate = value;
});
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');
Expand Down
27 changes: 2 additions & 25 deletions server/methods/sendConfirmationEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
37 changes: 2 additions & 35 deletions server/methods/sendForgotPasswordEmail.js
Original file line number Diff line number Diff line change
@@ -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 }`, {
Expand Down

0 comments on commit cbebe5a

Please sign in to comment.