Commit be98ba80 authored by heddn's avatar heddn Committed by heddn
Browse files

Issue #3072800 by heddn: Backport email notifications

parent 9d66fbfb
<?php
/**
* Class EmailNotify.
*/
class Notify {
/**
* {@inheritdoc}
*/
public static function send() {
// Don't send mail if notifications are disabled.
if (!variable_get('automatic_updates_notify', TRUE)) {
return;
}
$messages = AutomaticUpdatesPsa::getPublicServiceMessages();
if (!$messages) {
return;
}
$notify_list = variable_get('update_notify_emails', '');
if (!empty($notify_list)) {
$frequency = variable_get('automatic_updates_check_frequency', 43200);
$last_check = variable_get('automatic_updates_last_check', 0);
if ((REQUEST_TIME - $last_check) > $frequency) {
variable_set('automatic_updates_last_check', REQUEST_TIME);
$default_language = language_default();
foreach ($notify_list as $target) {
if ($target_user = user_load_by_mail($target)) {
$target_language = user_preferred_language($target_user);
}
else {
$target_language = $default_language;
}
$params['subject'] = format_plural(
count($messages),
'@count urgent Drupal announcement requires your attention for @site_name',
'@count urgent Drupal announcements require your attention for @site_name',
['@site_name' => variable_get('site_name', 'Drupal')],
['langcode' => $target_language->language]
);
$params['body'] = [
'#theme' => 'automatic_updates_psa_notify',
'#messages' => $messages,
];
$params['langcode'] = $target_language->language;
drupal_mail('automatic_updates', 'notify', $target, $params['langcode'], $params);
}
}
}
}
}
<?php
/**
* @file
* Template for the public service announcements email notification.
*
* Available variables:
* - $messages: The messages array.
*
* @ingroup themeable
*/
?>
<p>
<?php print t('A security update will be made available soon for your Drupal site. To ensure the security of the site, you should prepare the site to immediately apply the update once it is released!'); ?>
</p>
<p>
<?php $status_report = url('admin/reports/status', ['absolute' => TRUE])?>
<?php print t('See the <a href="!status_report">site status report page</a> for more information.', ['!status_report' => $status_report]); ?>
</p>
<p><?php print t('Public service announcements:'); ?></p>
<ul>
<?php foreach ($messages as $message): ?>
<li><?php print $message; ?></li>
<?php endforeach; ?>
</ul>
<p><?php print t('To see all PSAs, visit <a href="!url">!url</a>.', ['!url' => 'https://www.drupal.org/security/psa']); ?></p>
<p>
<?php $settings_link = url('admin/config/system/automatic_updates', ['absolute' => TRUE]); ?>
<?php print t('Your site is currently configured to send these emails when a security update will be made available soon. To change how you are notified, you may <a href="!settings_link">configure email notifications</a>.', ['!settings_link' => $settings_link]); ?>
</p>
......@@ -17,6 +17,12 @@ function automatic_updates_admin_form() {
'#title' => t('Show Public service announcements on administrative pages.'),
'#default_value' => variable_get('automatic_updates_enable_psa', TRUE),
];
$form['automatic_updates_notify'] = [
'#type' => 'checkbox',
'#title' => t('Send email notifications for Public service announcements.'),
'#default_value' => variable_get('automatic_updates_notify', TRUE),
'#description' => t('The email addresses listed in <a href="@update_manager">update manager settings</a> will be notified.', ['@update_manager' => url('admin/reports/updates/settings')]),
];
$last_check_timestamp = ReadinessCheckerManager::timestamp();
$form['automatic_updates_enable_readiness_checks'] = [
'#type' => 'checkbox',
......
......@@ -8,6 +8,7 @@ package = Security
files[] = tests/automatic_updates.test
files[] = AutomaticUpdatesPsa.php
files[] = ModifiedFilesService.php
files[] = Notify.php
files[] = ReadinessCheckers/ReadinessCheckerManager.php
files[] = ReadinessCheckers/ReadinessCheckerInterface.php
files[] = ReadinessCheckers/BlacklistPhp72Versions.php
......
......@@ -11,11 +11,13 @@
function automatic_updates_uninstall() {
variable_del('automatic_updates_psa_endpoint');
variable_del('automatic_updates_enable_psa');
variable_del('automatic_updates_notify');
variable_del('automatic_updates_enable_readiness_checks');
variable_del('automatic_updates.readiness_check_results.error');
variable_del('automatic_updates.readiness_check_results.warning');
variable_del('automatic_updates.readiness_check_timestamp');
variable_del('automatic_updates.php_sapi');
variable_del('automatic_updates_check_frequency');
variable_del('automatic_updates_last_check');
}
/**
......
......@@ -86,7 +86,30 @@ function automatic_updates_menu() {
* Implements hook_cron().
*/
function automatic_updates_cron() {
Notify::send();
foreach (ReadinessCheckerManager::getCategories() as $category) {
ReadinessCheckerManager::run($category);
}
}
/**
* Implements hook_theme().
*/
function automatic_updates_theme(array $existing, $type, $theme, $path) {
return [
'automatic_updates_psa_notify' => [
'variables' => [
'messages' => [],
],
'template' => 'automatic-updates-psa-notify',
],
];
}
/**
* Implements hook_mail().
*/
function automatic_updates_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'][] = drupal_render($params['body']);
}
......@@ -14,28 +14,29 @@ class AutomaticUpdatesTestCase extends DrupalWebTestCase {
* {@inheritdoc}
*/
public static function getInfo() {
return array(
return [
'name' => 'Automatic Updates',
'description' => 'Tests automatic updates.',
'group' => 'Automatic Updates',
);
];
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp(array('automatic_updates', 'automatic_updates_test'));
parent::setUp(['automatic_updates', 'automatic_updates_test']);
$psa_endpoint = $this->getAbsoluteUrl('automatic_updates/test-json');
variable_set('automatic_updates_psa_endpoint', $psa_endpoint);
cache_clear_all(['automatic_updates_psa'], 'cache');
// Create a user with permission to view the actions administration pages.
$user = $this->drupalCreateUser(array(
$user = $this->drupalCreateUser([
'access administration pages',
'administer site configuration',
'administer software updates',
));
]);
$this->drupalLogin($user);
$psa_endpoint = $this->getAbsoluteUrl('automatic_updates/test-json');
variable_set('automatic_updates_psa_endpoint', $psa_endpoint);
variable_set('automatic_updates_enable_psa', TRUE);
}
/**
......@@ -45,8 +46,8 @@ class AutomaticUpdatesTestCase extends DrupalWebTestCase {
// Test PSAs.
$this->drupalGet('admin');
$this->assertText('Critical Release - SA-2019-02-19');
$this->assertText('Seven - Moderately critical - Access bypass - SA-CONTRIB-2019');
$this->assertText('Critical Release - PSA-Really Old');
$this->assertText('Seven - Moderately critical - Access bypass - SA-CONTRIB-2019');
$this->assertNoText('Node - Moderately critical - Access bypass - SA-CONTRIB-2019');
// Test disabling PSAs.
......@@ -66,7 +67,7 @@ class AutomaticUpdatesTestCase extends DrupalWebTestCase {
$this->assertText('Critical Release - SA-2019-02-19');
// Test transmit errors with JSON endpoint.
drupal_flush_all_caches();
cache_clear_all(['automatic_updates_psa'], 'cache');
$this->drupalGet('admin');
$this->assertText('automatic_updates/test-json-denied is unreachable.');
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment