You are here

trait ConditionAccessResolverTrait in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php \Drupal\Core\Condition\ConditionAccessResolverTrait

Resolves a set of conditions.

Hierarchy

1 file declares its use of ConditionAccessResolverTrait
BlockAccessControlHandler.php in core/modules/block/src/BlockAccessControlHandler.php

File

core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php, line 10

Namespace

Drupal\Core\Condition
View source
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'.
   *
   * @return bool
   *   Whether these conditions grant or deny access.
   */
  protected function resolveConditions($conditions, $condition_logic) {
    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 a condition fails and all conditions were needed, deny access.
      if (!$pass && $condition_logic == 'and') {
        return FALSE;
      }
      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';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionAccessResolverTrait::resolveConditions protected function Resolves the given conditions based on the condition logic ('and'/'or').