new: [emailing] add custom templates to override existing ones

- currently implemented for event publish alerts and user enrollment (password_reset.ctp, alert.ctp)
- simply place the new templates in MISP/app/View/Emails/[text|html]/Custom
pull/8063/merge
iglocska 2022-04-15 16:28:36 +02:00
parent f81bb700c4
commit 3e706867e9
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
7 changed files with 56 additions and 26 deletions

5
.gitignore vendored
View File

@ -107,3 +107,8 @@ vagrant/.vagrant/
vagrant/*.log
/app/Lib/Dashboard/Custom/*
!/app/Lib/Dashboard/Custom/empty
/app/View/Emails/html/Custom/*
!/app/View/Emails/html/Custom/empty
/app/View/Emails/text/Custom/*
!/app/View/Emails/text/Custom/empty

View File

@ -1,5 +1,3 @@
http://download.geonames.org/export/dump/countryInfo.txt
<?php
class SupportShell extends AppShell {

View File

@ -71,14 +71,26 @@ class SendEmailTemplate
$View->viewPath = $View->layoutPath = 'Emails' . DS . 'html';
try {
$html = $View->render($this->viewName);
$View->viewPath = $View->layoutPath = 'Emails' . DS . 'html' . DS . 'Custom';
$html = $View->render($this->viewName); // Attempt to load a custom template if it exists
} catch (MissingViewException $e) {
$html = null; // HTMl template is optional
$View->viewPath = $View->layoutPath = 'Emails' . DS . 'html';
try {
$html = $View->render($this->viewName);
} catch (MissingViewException $e) {
$html = null; // HTMl template is optional
}
}
$View->viewPath = $View->layoutPath = 'Emails' . DS . 'text';
$View->hasRendered = false;
$text = $View->render($this->viewName);
try {
$View->viewPath = $View->layoutPath = 'Emails' . DS . 'text' . DS . 'Custom';
$text = $View->render($this->viewName); // Attempt to load a custom template if it exists
} catch (MissingViewException $e) {
$View->viewPath = $View->layoutPath = 'Emails' . DS . 'text';
$text = $View->render($this->viewName);
}
// Template can change default subject.
if ($View->get('subject')) {

View File

@ -4,6 +4,7 @@ App::uses('AuthComponent', 'Controller/Component');
App::uses('RandomTool', 'Tools');
App::uses('GpgTool', 'Tools');
App::uses('SendEmail', 'Tools');
App::uses('SendEmailTemplate', 'Tools');
App::uses('BlowfishConstantPasswordHasher', 'Controller/Component/Auth');
/**
@ -832,6 +833,7 @@ class User extends AppModel
$gpg = $this->initializeGpg();
$sendEmail = new SendEmail($gpg);
$result = $sendEmail->sendToUser($user, $subject, $body, $bodyNoEnc,$replyToUser ?: []);
try {
$result = $sendEmail->sendToUser($user, $subject, $body, $bodyNoEnc,$replyToUser ?: []);
@ -867,16 +869,6 @@ class User extends AppModel
return true;
}
public function adminMessageResolve($message)
{
$resolveVars = array('$contact' => 'MISP.contact', '$org' => 'MISP.org', '$misp' => 'MISP.baseurl');
foreach ($resolveVars as $k => $v) {
$v = Configure::read($v);
$message = str_replace($k, $v, $message);
}
return $message;
}
/**
* @param string $email
* @return array
@ -955,24 +947,15 @@ class User extends AppModel
public function initiatePasswordReset($user, $firstTime = false, $simpleReturn = false, $fixedPassword = false)
{
$org = Configure::read('MISP.org');
$options = array('newUserText', 'passwordResetText');
$subjects = array('[' . $org . ' MISP] New user registration', '[' . $org . ' MISP] Password reset');
$textToFetch = $options[($firstTime ? 0 : 1)];
$subject = $subjects[($firstTime ? 0 : 1)];
$this->Server = ClassRegistry::init('Server');
$body = Configure::read('MISP.' . $textToFetch);
if (!$body) {
$body = $this->Server->serverSettings['MISP'][$textToFetch]['value'];
}
$body = $this->adminMessageResolve($body);
if ($fixedPassword) {
$password = $fixedPassword;
} else {
$password = $this->generateRandomPassword();
}
$body = str_replace('$password', $password, $body);
$body = str_replace('$username', $user['User']['email'], $body);
$body = str_replace('\n', PHP_EOL, $body);
$body = $this->preparePasswordResetEmail($user, $password, $firstTime, $subject);
$result = $this->sendEmail($user, $body, false, $subject);
if ($result) {
$this->id = $user['User']['id'];
@ -991,6 +974,22 @@ class User extends AppModel
}
}
private function preparePasswordResetEmail($user, $password, $firstTime, $subject)
{
$textToFetch = $firstTime ? 'newUserText': 'passwordResetText';
$this->Server = ClassRegistry::init('Server');
$bodyTemplate = Configure::read('MISP.' . $textToFetch);
if (!$bodyTemplate) {
$bodyTemplate = $this->Server->serverSettings['MISP'][$textToFetch]['value'];
}
$template = new SendEmailTemplate('password_reset');
$template->set('body', $bodyTemplate);
$template->set('user', $user);
$template->set('password', $password);
$template->subject($subject);
return $template;
}
public function getOrgAdminsForOrg($org_id, $excludeUserId = false)
{
$adminRoles = $this->Role->find('column', array(

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,14 @@
<?php
$body = str_replace('$password', $password, $body);
$body = str_replace('$username', $user['User']['email'], $body);
$body = str_replace('\n', PHP_EOL, $body);
$resolveVars = [
'$contact' => 'MISP.contact',
'$org' => 'MISP.org',
'$misp' => 'MISP.baseurl'
];
foreach ($resolveVars as $k => $v) {
$v = Configure::read($v);
$body= str_replace($k, $v, $body);
}
echo $body;