diff --git a/automatic_updates.info.yml b/automatic_updates.info.yml index 3ba06a9eb1774016f6316ad6b73b1442a946f657..3bcad2cfaceb27286de765c670645541b035d7bb 100644 --- a/automatic_updates.info.yml +++ b/automatic_updates.info.yml @@ -3,3 +3,4 @@ type: module description: 'Drupal Automatic Updates' core: 8.x package: 'Security' +configure: automatic_updates.admin_form diff --git a/automatic_updates.links.menu.yml b/automatic_updates.links.menu.yml new file mode 100644 index 0000000000000000000000000000000000000000..572208f91e730a4b8fa20243ad331fb2cb0ec495 --- /dev/null +++ b/automatic_updates.links.menu.yml @@ -0,0 +1,5 @@ +automatic_updates.admin_form: + title: 'Automatic Updates' + route_name: automatic_updates.admin_form + description: 'Configure automatic updates' + parent: system.admin_config_system diff --git a/automatic_updates.routing.yml b/automatic_updates.routing.yml new file mode 100644 index 0000000000000000000000000000000000000000..18c08689d0f6a0b086273fcc65769e535a2dc121 --- /dev/null +++ b/automatic_updates.routing.yml @@ -0,0 +1,9 @@ +automatic_updates.admin_form: + path: '/admin/config/automatic_updates' + defaults: + _form: '\Drupal\automatic_updates\Form\AdminForm' + _title: 'Automatic Updates' + requirements: + _permission: 'administer software updates' + options: + _admin_route: TRUE diff --git a/config/install/automatic_updates.settings.yml b/config/install/automatic_updates.settings.yml index 2dc7865e6537becafe131b5f5c341e1e45a62671..085f065b0fc5d65c35a4eec635f6ca89cab69fd4 100644 --- a/config/install/automatic_updates.settings.yml +++ b/config/install/automatic_updates.settings.yml @@ -2,3 +2,4 @@ # TODO: Update to correct end point once it is available. See # https://www.drupal.org/project/automatic_updates/issues/3045273 psa_endpoint: 'http://localhost/automatic_updates/test-json' +enable_psa: true diff --git a/config/schema/automatic_updates.schema.yml b/config/schema/automatic_updates.schema.yml index 467d316c33677bb767d3a127a075132a44c603ba..ca294031e24d9d8350c77d967c1eb41ac11471fd 100644 --- a/config/schema/automatic_updates.schema.yml +++ b/config/schema/automatic_updates.schema.yml @@ -5,3 +5,6 @@ automatic_updates.settings: psa_endpoint: type: string label: 'Endpoint URI for PSAs' + enable_psa: + type: boolean + label: 'Enable PSA notices' diff --git a/src/Form/AdminForm.php b/src/Form/AdminForm.php new file mode 100644 index 0000000000000000000000000000000000000000..78217e8bc78230de5b469bfcbd9117ea6cc607c4 --- /dev/null +++ b/src/Form/AdminForm.php @@ -0,0 +1,55 @@ +<?php + +namespace Drupal\automatic_updates\Form; + +use Drupal\Core\Form\ConfigFormBase; +use Drupal\Core\Form\FormStateInterface; + +/** + * Administration form for automatic updates. + */ +class AdminForm extends ConfigFormBase { + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return [ + 'automatic_updates.settings', + ]; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'automatic_updates_admin_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $config = $this->config('automatic_updates.settings'); + $form['enable_psa'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Enable messaging of public service alerts (PSAs)'), + '#default_value' => $config->get('enable_psa'), + ]; + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + $form_state->cleanValues(); + $config = $this->config('automatic_updates.settings'); + foreach ($form_state->getValues() as $key => $value) { + $config->set($key, $value); + } + $config->save(); + } + +} diff --git a/src/Services/AutomaticUpdatesPsa.php b/src/Services/AutomaticUpdatesPsa.php index 9b31b744ad882af50789fd97d8dbcf8a818427b4..bc33939f525f49a095186d0b1f3f867dc75bc36e 100644 --- a/src/Services/AutomaticUpdatesPsa.php +++ b/src/Services/AutomaticUpdatesPsa.php @@ -114,6 +114,10 @@ class AutomaticUpdatesPsa implements AutomaticUpdatesPsaInterface { public function getPublicServiceMessages() { $messages = []; + if (!$this->config->get('enable_psa')) { + return $messages; + } + if ($cache = $this->cache->get('automatic_updates_psa')) { $response = $cache->data; } diff --git a/tests/src/Functional/AutomaticUpdatesTest.php b/tests/src/Functional/AutomaticUpdatesTest.php index 5cfca1d9812b54e731594b0a109d29f9dbb52a0f..807851dfccc8d53a366fed823df36801ce00aff7 100644 --- a/tests/src/Functional/AutomaticUpdatesTest.php +++ b/tests/src/Functional/AutomaticUpdatesTest.php @@ -68,6 +68,16 @@ class AutomaticUpdatesTest extends BrowserTestBase { drupal_flush_all_caches(); $this->drupalGet(Url::fromRoute('system.admin')); $this->assertSession()->pageTextContains('Drupal PSA endpoint http://localhost/automatic_updates/test-json-denied is unreachable.'); + + // Test disabling PSAs. + $end_point = $this->buildUrl(Url::fromRoute('test_automatic_updates.json_test_controller')); + $this->config('automatic_updates.settings') + ->set('psa_endpoint', $end_point) + ->set('enable_psa', FALSE) + ->save(); + drupal_flush_all_caches(); + $this->drupalGet(Url::fromRoute('system.admin')); + $this->assertSession()->pageTextNotContains('Drupal Core PSA: Critical Release - PSA-2019-02-19'); } }