Skip to content

Commit eb65310

Browse files
alikonrichard67
andauthored
[5.3] fine grain task notification mail (#44604)
Co-authored-by: Richard Fath <[email protected]>
1 parent 1bc1e8f commit eb65310

File tree

3 files changed

+86
-32
lines changed

3 files changed

+86
-32
lines changed

administrator/language/en-GB/plg_system_tasknotification.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
PLG_SYSTEM_TASK_NOTIFICATION="System - Task Notification"
77
PLG_SYSTEM_TASK_NOTIFICATION_FAILURE_MAIL_BODY="Hello,\n\n\nPlanned execution of Scheduled Task#{TASK_ID}, {TASK_TITLE}, has failed with exit code {EXIT_CODE} at {EXEC_DATE_TIME}.\n\nPlease visit the Joomla! backend for more information.\n\n{TASK_OUTPUT}"
88
PLG_SYSTEM_TASK_NOTIFICATION_FAILURE_MAIL_SUBJECT="Task Failure"
9+
PLG_SYSTEM_TASK_NOTIFICATION_FAILURE_USERGROUP_LABEL="User Groups to Notify"
910
PLG_SYSTEM_TASK_NOTIFICATION_FATAL_MAIL_BODY="Hello,\n\nPlanned execution of Scheduler Task#{TASK_ID}, {TASK_TITLE}, recovered from a fatal failure.\n\nThis could mean that the task execution exhausted the system resources or the restrictions from the PHP INI.\n\nPlease visit the Joomla! backend for more information."
1011
PLG_SYSTEM_TASK_NOTIFICATION_FATAL_MAIL_SUBJECT="Task Recover from Fatal Failure"
12+
PLG_SYSTEM_TASK_NOTIFICATION_FATAL_USERGROUP_LABEL="User Groups to Notify"
1113
PLG_SYSTEM_TASK_NOTIFICATION_LABEL_FAILURE_MAIL_TOGGLE="Task Failure"
1214
PLG_SYSTEM_TASK_NOTIFICATION_LABEL_FATAL_FAILURE_MAIL_TOGGLE="Fatal Failures/Crashes (Recommended)"
1315
PLG_SYSTEM_TASK_NOTIFICATION_LABEL_ORPHANED_TASK_MAIL_TOGGLE="Orphaned Tasks (Recommended)"
1416
PLG_SYSTEM_TASK_NOTIFICATION_LABEL_SUCCESS_MAIL_TOGGLE="Task Success"
1517
PLG_SYSTEM_TASK_NOTIFICATION_NO_MAIL_SENT="Could not send task notification to any user. This either means that mailer is not set up properly or no user with system emails enabled, com_scheduler `core.manage` privilege exists."
1618
PLG_SYSTEM_TASK_NOTIFICATION_ORPHAN_MAIL_BODY="Hello,\n\nScheduled Task#{TASK_ID}, {TASK_TITLE}, has been orphaned. This likely means that the provider plugin was removed or disabled from your Joomla! installation.\n\nPlease visit the Joomla! backend to investigate."
1719
PLG_SYSTEM_TASK_NOTIFICATION_ORPHAN_MAIL_SUBJECT="New Orphaned Task"
20+
PLG_SYSTEM_TASK_NOTIFICATION_ORPHAN_USERGROUP_LABEL="User Groups to Notify"
1821
PLG_SYSTEM_TASK_NOTIFICATION_SUCCESS_MAIL_BODY="Hello,\n\nScheduled Task#{TASK_ID}, {TASK_TITLE}, has been successfully executed at {EXEC_DATE_TIME}.\n\n{TASK_OUTPUT}"
1922
PLG_SYSTEM_TASK_NOTIFICATION_SUCCESS_MAIL_SUBJECT="Task Successful"
23+
PLG_SYSTEM_TASK_NOTIFICATION_SUCCESS_USERGROUP_LABEL="User Groups to Notify"
2024
PLG_SYSTEM_TASK_NOTIFICATION_USER_FETCH_FAIL="Failed to fetch users to send notifications to."
2125
PLG_SYSTEM_TASK_NOTIFICATION_XML_DESCRIPTION="Responsible for email notifications for execution of Scheduled tasks."

plugins/system/tasknotification/forms/task_notification.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
<option value="0">JDISABLED</option>
1414
<option value="1">JENABLED</option>
1515
</field>
16+
<field
17+
name="notification_success_groups"
18+
type="usergrouplist"
19+
label="PLG_SYSTEM_TASK_NOTIFICATION_SUCCESS_USERGROUP_LABEL"
20+
multiple="true"
21+
layout="joomla.form.field.list-fancy-select"
22+
checksuperusergroup="0"
23+
default="8"
24+
showon="success_mail:1"
25+
>
26+
</field>
1627
<field
1728
name="failure_mail"
1829
type="radio"
@@ -23,6 +34,17 @@
2334
<option value="0">JDISABLED</option>
2435
<option value="1">JENABLED</option>
2536
</field>
37+
<field
38+
name="notification_failure_groups"
39+
type="usergrouplist"
40+
label="PLG_SYSTEM_TASK_NOTIFICATION_FAILURE_USERGROUP_LABEL"
41+
multiple="true"
42+
layout="joomla.form.field.list-fancy-select"
43+
checksuperusergroup="0"
44+
default="8"
45+
showon="failure_mail:1"
46+
>
47+
</field>
2648
<field
2749
name="fatal_failure_mail"
2850
type="radio"
@@ -33,6 +55,17 @@
3355
<option value="0">JDISABLED</option>
3456
<option value="1">JENABLED</option>
3557
</field>
58+
<field
59+
name="notification_fatal_groups"
60+
type="usergrouplist"
61+
label="PLG_SYSTEM_TASK_NOTIFICATION_FATAL_USERGROUP_LABEL"
62+
multiple="true"
63+
layout="joomla.form.field.list-fancy-select"
64+
checksuperusergroup="0"
65+
default="8"
66+
showon="fatal_failure_mail:1"
67+
>
68+
</field>
3669
<field
3770
name="orphan_mail"
3871
type="radio"
@@ -43,6 +76,17 @@
4376
<option value="0">JDISABLED</option>
4477
<option value="1">JENABLED</option>
4578
</field>
79+
<field
80+
name="notification_orphan_groups"
81+
type="usergrouplist"
82+
label="PLG_SYSTEM_TASK_NOTIFICATION_ORPHAN_USERGROUP_LABEL"
83+
multiple="true"
84+
layout="joomla.form.field.list-fancy-select"
85+
checksuperusergroup="0"
86+
default="8"
87+
showon="orphan_mail:1"
88+
>
89+
</field>
4690
</fieldset>
4791
</fields>
4892
</fields>

plugins/system/tasknotification/src/Extension/TaskNotification.php

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,11 @@ public function notifyFailure(Event $event): void
134134

135135
// Load translations
136136
$this->loadLanguage();
137+
$groups = $task->get('params.notifications.notification_failure_groups');
137138

138139
// @todo safety checks, multiple files [?]
139140
$outFile = $event->getArgument('subject')->snapshot['output_file'] ?? '';
140-
$this->sendMail('plg_system_tasknotification.failure_mail', $data, $outFile);
141+
$this->sendMail('plg_system_tasknotification.failure_mail', $data, $outFile, $groups);
141142
}
142143

143144
/**
@@ -163,9 +164,10 @@ public function notifyOrphan(Event $event): void
163164

164165
// Load translations
165166
$this->loadLanguage();
167+
$groups = $task->get('params.notifications.notification_orphan_groups');
166168

167169
$data = $this->getDataFromTask($event->getArgument('subject'));
168-
$this->sendMail('plg_system_tasknotification.orphan_mail', $data);
170+
$this->sendMail('plg_system_tasknotification.orphan_mail', $data, '', $groups);
169171
}
170172

171173
/**
@@ -191,10 +193,11 @@ public function notifySuccess(Event $event): void
191193

192194
// Load translations
193195
$this->loadLanguage();
196+
$groups = $task->get('params.notifications.notification_success_groups');
194197

195198
// @todo safety checks, multiple files [?]
196199
$outFile = $event->getArgument('subject')->snapshot['output_file'] ?? '';
197-
$this->sendMail('plg_system_tasknotification.success_mail', $data, $outFile);
200+
$this->sendMail('plg_system_tasknotification.success_mail', $data, $outFile, $groups);
198201
}
199202

200203
/**
@@ -204,7 +207,7 @@ public function notifySuccess(Event $event): void
204207
*
205208
* @return void
206209
*
207-
* @since 5.3.0
210+
* @since __DEPLOY_VERSION__
208211
* @throws \Exception
209212
*/
210213
public function notifyWillResume(Event $event): void
@@ -236,8 +239,12 @@ public function notifyFatalRecovery(Event $event): void
236239
return;
237240
}
238241

242+
// Load translations
243+
$this->loadLanguage();
244+
$groups = $task->get('params.notifications.notification_fatal_groups');
245+
239246
$data = $this->getDataFromTask($event->getArgument('subject'));
240-
$this->sendMail('plg_system_tasknotification.fatal_recovery_mail', $data);
247+
$this->sendMail('plg_system_tasknotification.fatal_recovery_mail', $data, '', $groups);
241248
}
242249

243250
/**
@@ -267,24 +274,27 @@ private function getDataFromTask(Task $task): array
267274
* @param string $template The mail template.
268275
* @param array $data The data to bind to the mail template.
269276
* @param string $attachment The attachment to send with the mail (@todo multiple)
277+
* @param array $groups The user groups to notify.
270278
*
271279
* @return void
272280
*
273281
* @since 4.1.0
274282
* @throws \Exception
275283
*/
276-
private function sendMail(string $template, array $data, string $attachment = ''): void
284+
private function sendMail(string $template, array $data, string $attachment = '', array $groups = []): void
277285
{
278286
$app = $this->getApplication();
279287
$db = $this->getDatabase();
280288

281289
// Get all users who are not blocked and have opted in for system mails.
282290
$query = $db->getQuery(true);
283291

284-
$query->select($db->quoteName(['name', 'email', 'sendEmail', 'id']))
285-
->from($db->quoteName('#__users'))
286-
->where($db->quoteName('sendEmail') . ' = 1')
287-
->where($db->quoteName('block') . ' = 0');
292+
$query->select('DISTINCT ' . $db->quoteName('u.id') . ', ' . $db->quoteName('u.email'))
293+
->from($db->quoteName('#__users', 'u'))
294+
->join('LEFT', $db->quoteName('#__user_usergroup_map', 'g') . ' ON ' . $db->quoteName('g.user_id') . ' = ' . $db->quoteName('u.id'))
295+
->where($db->quoteName('u.sendEmail') . ' = 1')
296+
->where($db->quoteName('u.block') . ' = 0')
297+
->whereIn($db->quoteName('g.group_id'), $groups);
288298

289299
$db->setQuery($query);
290300

@@ -302,30 +312,26 @@ private function sendMail(string $template, array $data, string $attachment = ''
302312

303313
$mailSent = false;
304314

305-
// Mail all matching users who also have the `core.manage` privilege for com_scheduler.
315+
// Mail all matching users.
306316
foreach ($users as $user) {
307-
$user = $this->getUserFactory()->loadUserById($user->id);
308-
309-
if ($user->authorise('core.manage', 'com_scheduler')) {
310-
try {
311-
$mailer = new MailTemplate($template, $app->getLanguage()->getTag());
312-
$mailer->addTemplateData($data);
313-
$mailer->addRecipient($user->email);
314-
315-
if (
316-
!empty($attachment)
317-
&& is_file($attachment)
318-
) {
319-
// @todo we allow multiple files [?]
320-
$attachName = pathinfo($attachment, PATHINFO_BASENAME);
321-
$mailer->addAttachment($attachName, $attachment);
322-
}
323-
324-
$mailer->send();
325-
$mailSent = true;
326-
} catch (MailerException) {
327-
Log::add($this->getApplication()->getLanguage()->_('PLG_SYSTEM_TASK_NOTIFICATION_NOTIFY_SEND_EMAIL_FAIL'), Log::ERROR);
317+
try {
318+
$mailer = new MailTemplate($template, $app->getLanguage()->getTag());
319+
$mailer->addTemplateData($data);
320+
$mailer->addRecipient($user->email);
321+
322+
if (
323+
!empty($attachment)
324+
&& is_file($attachment)
325+
) {
326+
// @todo we allow multiple files [?]
327+
$attachName = pathinfo($attachment, PATHINFO_BASENAME);
328+
$mailer->addAttachment($attachName, $attachment);
328329
}
330+
331+
$mailer->send();
332+
$mailSent = true;
333+
} catch (MailerException) {
334+
Log::add($this->getApplication()->getLanguage()->_('PLG_SYSTEM_TASK_NOTIFICATION_NOTIFY_SEND_EMAIL_FAIL'), Log::ERROR);
329335
}
330336
}
331337

0 commit comments

Comments
 (0)