Allow altering of the request header

Created on 18 July 2023, over 1 year ago
Updated 28 August 2023, over 1 year ago

Problem/Motivation

We need to alter the request header in \Drupal\linkchecker\LinkCheckerService::check as we have to check links in an intranet where content is only accessible for authenticated users.

Proposed resolution

When using the user_hash module, you can add X-USER-NAME and X-USER-HASH to the request header to make an otherwise anonymous request behave like from an authenticated user without having to mess with cookies.

This can be made possible by calling a hook so that other modules can add new values to the request header.

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇩🇪Germany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jurgenhaas
  • @jurgenhaas opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • Status changed to Needs work over 1 year ago
  • 🇳🇴Norway eiriksm Norway

    Great addition, makes sense!

    Could we maybe get the module handler injected here? And if we could get it into an api.php file that would be great. Would of course be extra nice with a test, but not a requirement :)

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thanks @eiriksm for your feedback. I'm late with my response since I've been at DevDays in Vienna last week.

    Could we maybe get the module handler injected here?

    Absolutely, I've just proposed a quick solution initially to see if you would be willing to go for the general idea. Will of course clean up the code with DI and maybe more.

    And if we could get it into an api.php file that would be great.

    Yes, if we stick with an alter hook, then I would be adding this. However, the future proof approach would be to dispatch an event instead. What would be your preference?

    Would of course be extra nice with a test, but not a requirement :)

    Sure, will see what I can do.

  • Assigned to jurgenhaas
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • 🇩🇪Germany jurgenhaas Gottmadingen

    @eiriksm any feedback on whether we should implement an event rather than an alter hook?

  • 🇩🇪Germany jurgenhaas Gottmadingen

    @eiriksm any feedback on whether we should implement an event rather than an alter hook?

  • 🇳🇴Norway eiriksm Norway

    Ah yeah.

    I don't have any strong preferences, so do whatever suits your project requirements best?

  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    This is now implemented with an event because that's the future when hooks will be gone eventually ;-)

    Making use of this could be done with an event subscriber like e.g. this:

    
    namespace Drupal\example\EventSubscriber;
    
    use Drupal\linkchecker\Event\BuildHeader;
    use Drupal\linkchecker\Event\LinkcheckerEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    /**
     * Example event subscriber.
     */
    class SomeSubscriber implements EventSubscriberInterface {
    
      /**
       * Linkchecker build header event handler.
       *
       * @param \Drupal\linkchecker\Event\BuildHeader $event
       *   BuildHeader event.
       */
      public function onBuildLinkcheckerHeader(BuildHeader $event): void {
        $headers = &$event->getHeaders();
        $headers['X-SOMETHING'] = 'Test value';
      }
    
      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents(): array {
        return [
          LinkcheckerEvents::BUILD_HEADER => ['onBuildLinkcheckerHeader'],
        ];
      }
    
    }
    
    
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    86 pass
  • 🇳🇴Norway eiriksm Norway

    Sorry about the random commit to the MR, had to trigger the tests some way 🤓

  • Status changed to Needs work over 1 year ago
  • 🇳🇴Norway eiriksm Norway

    2 comments:

    - could you fix up the coding standards in the added code?
    - just a personal preference, but would feel nicer if we used a setter on the event, and then retrieve the headers from the event after we dispatch it?

    Otherwise this is great stuff and looks great 👍

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    86 pass
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Have addressed your comments in #12. THe last remaining code style issue is unrelated to this MR.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    86 pass
  • 🇳🇴Norway eiriksm Norway

    Awesome! Thanks!

  • Status changed to Fixed over 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024