new: [workflowModule:send_email] To allow sending an email to a list of users

The module requires the `jinja_template_rendering` module in misp-module to work correctly
pull/8568/head
Sami Mokaddem 2022-08-25 10:56:49 +02:00
parent e9e0d3d203
commit d22e426364
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,93 @@
<?php
include_once APP . 'Model/WorkflowModules/WorkflowBaseModule.php';
App::uses('JsonTool', 'Tools');
class Module_send_mail extends WorkflowBaseActionModule
{
public $id = 'send-mail';
public $name = 'Send Mail';
public $description = 'Allow to send a Mail to a list or recipients';
public $icon = 'envelope';
public $inputs = 1;
public $outputs = 1;
public $support_filters = false;
public $params = [];
private $User;
private $all_users;
public function __construct()
{
parent::__construct();
$this->User = ClassRegistry::init('User');
$this->all_users = $this->User->find('all', [
'conditions' => [],
'recursive' => -1,
]);
$users = array_merge(['All accounts'], array_column(array_column($this->all_users, 'User'), 'email'));
$this->params = [
[
'id' => 'recipients',
'label' => 'Recipients',
'type' => 'picker',
'multiple' => true,
'options' => $users,
'default' => ['All accounts'],
],
[
'id' => 'mail_template_subject',
'label' => 'Mail template subject',
'type' => 'textarea',
'placeholder' => __('The **template** will be rendered using *Jinja2*!'),
],
[
'id' => 'mail_template_body',
'label' => 'Mail template body',
'type' => 'textarea',
'placeholder' => __('The **template** will be rendered using *Jinja2*!'),
],
];
}
public function exec(array $node, WorkflowRoamingData $roamingData, array &$errors = []): bool
{
parent::exec($node, $roamingData, $errors);
$params = $this->getParamsWithValues($node);
if (empty($params['recipients']['value'])) {
$errors[] = __('No recipient set.');
return false;
}
if (empty($params['mail_template_subject']['value'])) {
$errors[] = __('The mail template is empty.');
return false;
}
$rData = $roamingData->getData();
$renderedBody = $this->render_jinja_template($params['mail_template_body']['value'], $rData);
$renderedSubject = $this->render_jinja_template($params['mail_template_subject']['value'], $rData);
$users = [];
if (in_array('All accounts', $params['recipients']['value'])) {
$users = $this->all_users;
} else {
$users = $this->User->find('all', [
'conditions' => [
'User.email' => $params['recipients']['value']
],
'recursive' => -1,
]);
}
foreach ($users as $user) {
$this->sendMail($user, $renderedBody, $renderedSubject);
}
return true;
}
protected function sendMail(array $user, string $content, string $subject): void
{
$res = $this->User->sendEmail($user, $content, false, $subject);
}
}