Skip to content
Snippets Groups Projects
Unverified Commit 494cc031 authored by Rodrigo Nascimento's avatar Rodrigo Nascimento Committed by Diego Sampaio
Browse files

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

parent 8bc295e0
No related branches found
No related tags found
No related merge requests found
......@@ -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');
......
......@@ -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',
......
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 }`, {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment