You are here

class WebformAccessResult in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Access/WebformAccessResult.php \Drupal\webform\Access\WebformAccessResult

Value object indicating an allowed access result, with cacheability metadata.

Hierarchy

Expanded class hierarchy of WebformAccessResult

3 files declare their use of WebformAccessResult
WebformAccessRulesManager.php in src/WebformAccessRulesManager.php
WebformEntityAccessControlHandler.php in src/WebformEntityAccessControlHandler.php
WebformSubmissionAccessControlHandler.php in src/WebformSubmissionAccessControlHandler.php

File

src/Access/WebformAccessResult.php, line 12

Namespace

Drupal\webform\Access
View source
class WebformAccessResult {

  /**
   * Creates an allowed or neutral access result.
   *
   * @param bool $condition
   *   The condition to evaluate.
   * @param \Drupal\Core\Entity\EntityInterface|null $webform_entity
   *   A webform or webform submission.
   * @param bool $cache_per_user
   *   Cache per user.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   If $condition is TRUE, isAllowed() will be TRUE, otherwise isNeutral()
   *   will be TRUE.
   */
  public static function allowedIf($condition, EntityInterface $webform_entity = NULL, $cache_per_user = FALSE) {
    return $condition ? static::allowed($webform_entity, $cache_per_user) : static::neutral($webform_entity, $cache_per_user);
  }

  /**
   * Creates an AccessResultInterface object with isAllowed() === TRUE.
   *
   * @param \Drupal\Core\Entity\EntityInterface|null $webform_entity
   *   A webform or webform submission.
   * @param bool $cache_per_user
   *   Cache per user.
   *
   * @return \Drupal\Core\Access\AccessResultAllowed
   *   isAllowed() will be TRUE.
   */
  public static function allowed(EntityInterface $webform_entity = NULL, $cache_per_user = FALSE) {
    return static::addDependencies(AccessResult::allowed(), $webform_entity, $cache_per_user);
  }

  /**
   * Creates an AccessResultInterface object with isNeutral() === TRUE.
   *
   * @param \Drupal\Core\Entity\EntityInterface|null $webform_entity
   *   A webform or webform submission.
   * @param bool $cache_per_user
   *   Cache per user.
   *
   * @return \Drupal\Core\Access\AccessResultForbidden
   *   isNeutral() will be TRUE.
   */
  public static function neutral(EntityInterface $webform_entity = NULL, $cache_per_user = FALSE) {
    return static::addDependencies(AccessResult::neutral(), $webform_entity, $cache_per_user);
  }

  /**
   * Creates an AccessResultInterface object with isForbidden() === TRUE.
   *
   * @param \Drupal\Core\Entity\EntityInterface|null $webform_entity
   *   A webform or webform submission.
   * @param bool $cache_per_user
   *   Cache per user.
   *
   * @return \Drupal\Core\Access\AccessResultForbidden
   *   isForbidden() will be TRUE.
   */
  public static function forbidden(EntityInterface $webform_entity = NULL, $cache_per_user = FALSE) {
    return static::addDependencies(AccessResult::forbidden(), $webform_entity, $cache_per_user);
  }

  /**
   * Adds dependencies to an access result.
   *
   * @param \Drupal\Core\Access\AccessResult $access_result
   *   The access result.
   * @param \Drupal\Core\Entity\EntityInterface|null $webform_entity
   *   A webform or webform submission.
   * @param bool $cache_per_user
   *   Cache per user.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result with dependencies.
   */
  public static function addDependencies(AccessResult $access_result, EntityInterface $webform_entity = NULL, $cache_per_user = FALSE) {
    $access_result
      ->cachePerPermissions();
    if ($cache_per_user) {
      $access_result
        ->cachePerUser();
    }
    if ($webform_entity) {
      if ($webform_entity instanceof WebformSubmissionInterface) {
        $access_result
          ->addCacheableDependency($webform_entity
          ->getWebform());
      }
      $access_result
        ->addCacheableDependency($webform_entity);
    }
    return $access_result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformAccessResult::addDependencies public static function Adds dependencies to an access result.
WebformAccessResult::allowed public static function Creates an AccessResultInterface object with isAllowed() === TRUE.
WebformAccessResult::allowedIf public static function Creates an allowed or neutral access result.
WebformAccessResult::forbidden public static function Creates an AccessResultInterface object with isForbidden() === TRUE.
WebformAccessResult::neutral public static function Creates an AccessResultInterface object with isNeutral() === TRUE.