From 1cf5c606a16642241b9d3a108f4d1c63bf69b86e Mon Sep 17 00:00:00 2001 From: Aron Novak Date: Wed, 10 Jul 2024 14:01:37 +0200 Subject: [PATCH] header link settings --- config/install/islandora.settings.yml | 1 + config/schema/islandora.schema.yml | 3 +++ src/EventSubscriber/LinkHeaderSubscriber.php | 14 +++++++++++++- src/EventSubscriber/MediaLinkHeaderSubscriber.php | 3 +++ src/EventSubscriber/NodeLinkHeaderSubscriber.php | 3 +++ src/Form/IslandoraSettingsForm.php | 9 +++++++++ 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/config/install/islandora.settings.yml b/config/install/islandora.settings.yml index 1497c291e..929188483 100644 --- a/config/install/islandora.settings.yml +++ b/config/install/islandora.settings.yml @@ -2,3 +2,4 @@ broker_url: 'tcp://localhost:61613' jwt_expiry: '+2 hour' delete_media_and_files: TRUE gemini_pseudo_bundles: [] +allow_header_links: TRUE diff --git a/config/schema/islandora.schema.yml b/config/schema/islandora.schema.yml index 282f7b3c5..93fbb3aae 100644 --- a/config/schema/islandora.schema.yml +++ b/config/schema/islandora.schema.yml @@ -14,6 +14,9 @@ islandora.settings: jwt_expiry: type: string label: 'How long JWTs should last before expiring.' + allow_header_links: + type: boolean + label: 'Allow generating links in the HTTP header' delete_media_and_files: type: boolean label: 'Node Delete with Media and Files' diff --git a/src/EventSubscriber/LinkHeaderSubscriber.php b/src/EventSubscriber/LinkHeaderSubscriber.php index f7e5725b8..9ba1029ac 100644 --- a/src/EventSubscriber/LinkHeaderSubscriber.php +++ b/src/EventSubscriber/LinkHeaderSubscriber.php @@ -2,6 +2,7 @@ namespace Drupal\islandora\EventSubscriber; +use Drupal\Core\Config\ConfigFactoryInterface; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Drupal\Core\Access\AccessManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; @@ -72,6 +73,13 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { */ protected $utils; + /** + * Module settings. + * + * @var \Drupal\Core\Config\ImmutableConfig + */ + protected $settings; + /** * Constructor. * @@ -89,6 +97,8 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { * Request stack (for current request). * @param \Drupal\islandora\IslandoraUtils $utils * Islandora utils. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * Config factory. */ public function __construct( EntityTypeManagerInterface $entity_type_manager, @@ -97,7 +107,8 @@ public function __construct( AccountInterface $account, RouteMatchInterface $route_match, RequestStack $request_stack, - IslandoraUtils $utils + IslandoraUtils $utils, + ConfigFactoryInterface $config_factory ) { $this->entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; @@ -108,6 +119,7 @@ public function __construct( $this->account = $account; $this->requestStack = $request_stack; $this->utils = $utils; + $this->settings = $config_factory->get('islandora.settings'); } /** diff --git a/src/EventSubscriber/MediaLinkHeaderSubscriber.php b/src/EventSubscriber/MediaLinkHeaderSubscriber.php index 0f406cf57..28a61fa02 100644 --- a/src/EventSubscriber/MediaLinkHeaderSubscriber.php +++ b/src/EventSubscriber/MediaLinkHeaderSubscriber.php @@ -18,6 +18,9 @@ class MediaLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSub * {@inheritdoc} */ public function onResponse(ResponseEvent $event) { + if (!$this->settings->get('allow_header_links')) { + return; + } $response = $event->getResponse(); $media = $this->getObject($response, 'media'); diff --git a/src/EventSubscriber/NodeLinkHeaderSubscriber.php b/src/EventSubscriber/NodeLinkHeaderSubscriber.php index c4cdaea8c..ff6029586 100644 --- a/src/EventSubscriber/NodeLinkHeaderSubscriber.php +++ b/src/EventSubscriber/NodeLinkHeaderSubscriber.php @@ -21,6 +21,9 @@ class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubs * Event containing the response. */ public function onResponse(ResponseEvent $event) { + if (!$this->settings->get('allow_header_links')) { + return; + } $response = $event->getResponse(); $node = $this->getObject($response, 'node'); diff --git a/src/Form/IslandoraSettingsForm.php b/src/Form/IslandoraSettingsForm.php index ad4c83b3e..14ccb9244 100644 --- a/src/Form/IslandoraSettingsForm.php +++ b/src/Form/IslandoraSettingsForm.php @@ -44,6 +44,7 @@ class IslandoraSettingsForm extends ConfigFormBase { const GEMINI_PSEUDO_FIELD = 'field_gemini_uri'; const NODE_DELETE_MEDIA_AND_FILES = 'delete_media_and_files'; const REDIRECT_AFTER_MEDIA_SAVE = 'redirect_after_media_save'; + const ALLOW_HEADER_LINKS = 'allow_header_links'; /** * To list the available bundle types. @@ -271,6 +272,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#url' => Url::fromRoute('system.jsonld_settings'), ]; + $form[self::ALLOW_HEADER_LINKS] = [ + '#type' => 'checkbox', + '#title' => $this->t('Allow header links'), + '#description' => $this->t('If checked, links in the HTTP header will be added'), + '#default_value' => (bool) $config->get(self::ALLOW_HEADER_LINKS), + ]; + return parent::buildForm($form, $form_state); } @@ -384,6 +392,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ->set(self::GEMINI_PSEUDO, $new_pseudo_types) ->set(self::NODE_DELETE_MEDIA_AND_FILES, $form_state->getValue(self::NODE_DELETE_MEDIA_AND_FILES)) ->set(self::REDIRECT_AFTER_MEDIA_SAVE, $form_state->getValue(self::REDIRECT_AFTER_MEDIA_SAVE)) + ->set(self::ALLOW_HEADER_LINKS, $form_state->getValue(self::ALLOW_HEADER_LINKS)) ->save(); parent::submitForm($form, $form_state);