Unverified Commit eff12d63 authored by ekes's avatar ekes Committed by GitHub
Browse files

Disable per-domain access control while indexing. (#545)

This populates the index with all items and their referenced items.
The items are filtered on output.

See https://github.com/localgovdrupal/localgov_microsites/issues/524
parent d0cc8ea9
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -73,3 +73,9 @@ services:
    tags:
      - { name: 'context_provider' }
    deprecated: 'The "%service_id%" service is deprecated. You should use the group sites context directly instead.'

  localgov_microsites_group.event_subscriber:
    class: Drupal\localgov_microsites_group\EventSubscriber\SearchApiSubscriber
    arguments: ['@group_sites.admin_mode']
    tags:
      - { name: event_subscriber }
+53 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\localgov_microsites_group\EventSubscriber;

use Drupal\group_sites\GroupSitesAdminModeInterface;
use Drupal\search_api\Event\IndexingItemsEvent;
use Drupal\search_api\Event\ItemsIndexedEvent;
use Drupal\search_api\Event\SearchApiEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Switch into Group Sites admin mode when indexing items.
 *
 * There is one index. All items go into it so while indexing per-site access
 * control (including on referenced items) shouldn't kick in.
 * The results are returned on searches with a group site filter.
 */
final class SearchApiSubscriber implements EventSubscriberInterface {

  /**
   * Constructs a SearchApiSubscriber object.
   */
  public function __construct(
    private readonly GroupSitesAdminModeInterface $groupSitesAdminMode,
  ) {}

  /**
   * Preparing items to be indexed.
   */
  public function startIndexing(IndexingItemsEvent $event): void {
    $this->groupSitesAdminMode->setAdminModeOverride(TRUE);
  }

  /**
   * Items have been indexed.
   */
  public function stopIndexing(ItemsIndexedEvent $event): void {
    $this->groupSitesAdminMode->setAdminModeOverride(FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    return [
      SearchApiEvents::INDEXING_ITEMS => ['startIndexing'],
      SearchApiEvents::ITEMS_INDEXED => ['stopIndexing'],
    ];
  }

}