Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Email configs not updating after setting changes #17578

Merged
merged 1 commit into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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