Skip to content
Snippets Groups Projects

Fix page access check with contexts, #2837833

Files
21
<?php
namespace Drupal\page_manager\Entity\CoreImprovement;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
/**
* Resolves a set of conditions.
*
* Copy of core trait with added cacheability.
*
* @todo Upstream this.
*/
trait ConditionAccessResolverTrait {
/**
* Resolves the given conditions based on the condition logic ('and'/'or').
*
* @param \Drupal\Core\Condition\ConditionInterface[] $conditions
* A set of conditions.
* @param string $condition_logic
* The logic used to compute access, either 'and' or 'or'.
* @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface|null $cacheabilityCollector
* A cacheability collector if wanted.
*
* @return bool
* Whether these conditions grant or deny access.
*
* @see \Drupal\Core\Condition\ConditionAccessResolverTrait
*/
protected function resolveConditions($conditions, $condition_logic, ?RefinableCacheableDependencyInterface $cacheabilityCollector = NULL) {
foreach ($conditions as $condition) {
try {
$pass = $condition->execute();
}
catch (ContextException $e) {
// If a condition is missing context and is not negated, consider that a
// fail.
$pass = $condition->isNegated();
}
if ($cacheabilityCollector) {
$cacheabilityCollector->addCacheableDependency($condition);
}
// If a condition fails and all conditions were needed, deny access.
if (!$pass && $condition_logic == 'and') {
return FALSE;
}
// If a condition passes and only one condition was needed, grant access.
elseif ($pass && $condition_logic == 'or') {
return TRUE;
}
}
// Return TRUE if logic was 'and', meaning all rules passed.
// Return FALSE if logic was 'or', meaning no rule passed.
return $condition_logic == 'and';
}
}
Loading