예제 #1
0
 /**
  * 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);
         }
     }
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function getRuntimeContexts(array $unqualified_context_ids)
 {
     // Load the current domain.
     $current_domain = $this->negotiator->getActiveDomain();
     // Set the context.
     $context = new Context(new ContextDefinition('entity:domain', $this->t('Active domain')), $current_domain);
     // Allow caching.
     $cacheability = new CacheableMetadata();
     $cacheability->setCacheContexts(['url.site']);
     $context->addCacheableDependency($cacheability);
     // Prepare the result.
     $result = ['current_domain' => $context];
     return $result;
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function access(AccountInterface $account)
 {
     $domain = $this->domainNegotiator->getActiveDomain();
     // Is the domain allowed?
     // No domain, let it pass.
     if (empty($domain)) {
         return AccessResult::allowed()->setCacheMaxAge(0);
     }
     // Active domain, let it pass.
     if ($domain->status()) {
         return AccessResult::allowed()->setCacheMaxAge(0);
     } else {
         $permissions = array('administer domains', 'access inactive domains');
         $operator = 'OR';
         return AccessResult::allowedIfHasPermissions($account, $permissions, $operator)->setCacheMaxAge(0);
     }
 }
예제 #4
0
 /**
  * Sets domain and language contexts for the request.
  *
  * We wait to do this in order to avoid circular dependencies
  * with the locale module.
  */
 protected function initiateContext()
 {
     // @TODO: Is this cacheable?
     // Get the language context. Note that injecting the language manager
     // into the service created a circular dependency error, so we load from
     // the core service manager.
     $this->languageManager = \Drupal::languageManager();
     $this->language = $this->languageManager->getCurrentLanguage();
     // Get the domain context.
     $this->domain = $this->domainNegotiator->getActiveDomain();
 }
예제 #5
0
 /**
  * @inheritdoc
  */
 public static function getDefaultValue(FieldableEntityInterface $entity, FieldDefinitionInterface $definition)
 {
     $item = array();
     switch ($entity->getEntityType()) {
         case 'user':
         case 'node':
             if ($active = $this->negotiator->getActiveDomain()) {
                 $item[0]['target_uuid'] = $active->uuid();
             }
             break;
         default:
             break;
     }
     return $item;
 }
예제 #6
0
 /**
  * {@inheritdoc}
  */
 public function createHostname()
 {
     return $this->negotiator->negotiateActiveHostname();
 }