Commit 805df365 authored by Marcin Grabias's avatar Marcin Grabias
Browse files

Issue #3310973: Allow access to groups being subgroup content of the current domain group

parent addcf1ec
Loading
Loading
Loading
Loading
+37 −19
Original line number Diff line number Diff line
@@ -10,9 +10,9 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings;
use Drupal\group\Entity\GroupContentInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\Storage\GroupContentStorageInterface;
use Drupal\group_domain\GroupDomainInfo;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -25,7 +25,7 @@ final class PathRequestSubscriber implements EventSubscriberInterface {
  /**
   * The ETM.
   */
  private EntityTypeManagerInterface $entityTypeManager;
  private GroupContentStorageInterface $groupContentStorage;

  /**
   * The RM.
@@ -51,7 +51,7 @@ final class PathRequestSubscriber implements EventSubscriberInterface {
    ConfigFactoryInterface $config_factory,
    GroupDomainInfo $domain_info
  ) {
    $this->entityTypeManager = $entity_type_manager;
    $this->groupContentStorage = $entity_type_manager->getStorage('group_content');
    $this->routeMatch = $route_match;
    $this->domainInfo = $domain_info;

@@ -83,25 +83,43 @@ final class PathRequestSubscriber implements EventSubscriberInterface {
      return;
    }

    $group = $this->routeMatch->getParameter('group');
    $group_content = $this->routeMatch->getParameter('group_content');
    if ($group instanceof GroupInterface) {
      $this->throwIfNotGroupNestedContent($group, $request);
    $domain_group = $this->domainInfo->getRequestDomainGroup($request);
    $domain_group_id = $domain_group === NULL ? '0' : $domain_group->id();

    $parameters = $this->routeMatch->getParameters()->all();
    if (\count($parameters) === 0) {
      return;
    }

    $throw = FALSE;
    foreach ($parameters as $parameter) {
      if ($parameter instanceof GroupInterface) {
        if ($parameter->id() === $domain_group_id) {
          return;
        }
        // Group as group content case.
        $group_contents = $this->groupContentStorage->loadByEntity($parameter);
        foreach ($group_contents as $group_content) {
          if ($group_content->getGroup()->id() === $domain_group_id) {
            return;
          }
    elseif ($group_content instanceof GroupContentInterface) {
      $this->throwIfNotGroupNestedContent($group_content->getGroup(), $request);
        }
      }
      elseif ($parameter instanceof GroupContentInterface) {
        if ($parameter->getGroup()->id() === $domain_group_id) {
          return;
        }
      }

  /**
   * Throw exception.
   *
   * Throw if request is for content which is not nested under the right
   * custom domain.
   */
  private function throwIfNotGroupNestedContent(GroupInterface $group, Request $request): void {
    $domain_group = $this->domainInfo->getRequestDomainGroup($request);
    if ($domain_group === NULL || $domain_group->id() !== $group->id()) {
      // Don't throw if we don't have group or group content in parameters.
      else {
        continue;
      }

      $throw = TRUE;
    }

    if ($throw) {
      throw new NotFoundHttpException();
    }
  }