/** * Sets the domain context of the request. * * This method also determines the redirect status for the http request. * * Specifically, here we determine if a redirect is required. That happens * in one of two cases: an unauthorized request to an inactive domain is made; * a domain alias is set to redirect to its primary domain record. * * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event * The Event to process. */ public function onKernelRequestDomain(GetResponseEvent $event) { $redirect = FALSE; // Negotiate the request and set domain context. if ($domain = $this->domainNegotiator->getActiveDomain(TRUE)) { $domain_url = $domain->getUrl(); if ($domain_url) { $redirect_type = $domain->getRedirect(); $path = trim($event->getRequest()->getPathInfo(), '/'); // If domain negotiation asked for a redirect, issue it. if (!is_null($redirect_type)) { $redirect = TRUE; } elseif ($apply = $this->accessCheck->checkPath($path)) { $access = $this->accessCheck->access($this->account); // If the access check fails, reroute to the default domain. // Note that Allowed, Neutral, and Failed are the options here. // We insist on Allowed. if (!$access->isAllowed()) { $default = $this->domainLoader->loadDefaultDomain(); $domain_url = $default->getUrl(); $redirect = TRUE; $redirect_type = 302; } } } if ($redirect) { // Pass a redirect if necessary. $response = new TrustedRedirectResponse($domain_url, $redirect_type); $event->setResponse($response); } } }
/** * {@inheritdoc} */ public function setRequestDomain($httpHost, $reset = FALSE) { // @TODO: Investigate caching methods. $this->setHttpHost($httpHost); if ($domain = $this->domainLoader->loadByHostname($httpHost)) { // If the load worked, set an exact match flag for the hook. $domain->setMatchType(DOMAIN_MATCH_EXACT); } elseif ($domain = $this->domainLoader->loadDefaultDomain()) { $domain->setMatchType(DOMAIN_MATCH_NONE); } else { $values = array('hostname' => $httpHost); $domain = \Drupal::entityManager()->getStorage('domain')->create($values); $domain->setMatchType(DOMAIN_MATCH_NONE); } // Now check with modules (like Domain Alias) that register alternate // lookup systems with the main module. $this->moduleHandler->alter('domain_request', $domain); // We must have registered a valid id, else the request made no match. $id = $domain->id(); if (!empty($id)) { $this->setActiveDomain($domain); } }