/** * {@inheritdoc} */ public function andIf(AccessResultInterface $other) { // The other access result's cacheability metadata is merged if $merge_other // gets set to TRUE. It gets set to TRUE in one case: if the other access // result is used. $merge_other = FALSE; if ($this->isForbidden() || $other->isForbidden()) { $result = static::forbidden(); if (!$this->isForbidden()) { $merge_other = TRUE; } } elseif ($this->isAllowed() && $other->isAllowed()) { $result = static::allowed(); $merge_other = TRUE; } else { $result = static::neutral(); if (!$this->isNeutral()) { $merge_other = TRUE; } } $result->inheritCacheability($this); if ($merge_other) { $result->inheritCacheability($other); // If this access result is not cacheable, then an AND with another access // result must also not be cacheable, except if the other access result // has isForbidden() === TRUE. isForbidden() access results are contagious // in that they propagate regardless of the other value. if ($this->getCacheMaxAge() === 0 && !$result->isForbidden()) { $result->setCacheMaxAge(0); } } return $result; }
/** * {@inheritdoc} */ public function andIf(AccessResultInterface $other) { if ($this->isForbidden() || $other->isForbidden()) { return new static('FORBIDDEN'); } elseif ($this->isAllowed() && $other->isAllowed()) { return new static('ALLOWED'); } else { return new static('NEUTRAL'); } }
/** * Inherits the cacheability of the other access result, if any. * * @param \Drupal\Core\Access\AccessResultInterface $other * The other access result, whose cacheability (if any) to inherit. * * @return $this */ public function inheritCacheability(AccessResultInterface $other) { if ($other instanceof CacheableDependencyInterface) { if ($this->getCacheMaxAge() !== 0 && $other->getCacheMaxAge() !== 0) { $this->setCacheMaxAge(Cache::mergeMaxAges($this->getCacheMaxAge(), $other->getCacheMaxAge())); } else { $this->setCacheMaxAge($other->getCacheMaxAge()); } $this->addCacheContexts($other->getCacheContexts()); $this->addCacheTags($other->getCacheTags()); } else { $this->setCacheMaxAge(0); } return $this; }
/** * Inherits the cacheability of the other access result, if any. * * @param \Drupal\Core\Access\AccessResultInterface $other * The other access result, whose cacheability (if any) to inherit. * * @return $this */ public function inheritCacheability(AccessResultInterface $other) { if ($other instanceof CacheableInterface) { $this->setCacheable($other->isCacheable()); $this->addCacheContexts($other->getCacheKeys()); $this->addCacheTags($other->getCacheTags()); // Use the lowest max-age. if ($this->getCacheMaxAge() === Cache::PERMANENT) { // The other max-age is either lower or equal. $this->setCacheMaxAge($other->getCacheMaxAge()); } else { $this->setCacheMaxAge(min($this->getCacheMaxAge(), $other->getCacheMaxAge())); } } else { $this->setCacheable(FALSE); } return $this; }
/** * Sets an access check result. * * @param \Drupal\Core\Access\AccessResultInterface * * @return $this */ public function setAccessResult(AccessResultInterface $access_result) { $this->accessResult = $this->accessResult->orIf($access_result); return $this; }
/** * Inherits the cacheability of the other access result, if any. * * @param \Drupal\Core\Access\AccessResultInterface $other * The other access result, whose cacheability (if any) to inherit. * * @return $this */ public function inheritCacheability(AccessResultInterface $other) { if ($other instanceof CacheableInterface) { $this->setCacheable($other->isCacheable()); $this->addCacheContexts($other->getCacheContexts()); $this->addCacheTags($other->getCacheTags()); $this->setCacheMaxAge(Cache::mergeMaxAges($this->getCacheMaxAge(), $other->getCacheMaxAge())); } else { $this->setCacheable(FALSE); } return $this; }