You are here

class WebformEntityAccess in Webform 6.x

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

Defines the custom access control handler for the webform entities.

Hierarchy

Expanded class hierarchy of WebformEntityAccess

2 files declare their use of WebformEntityAccess
WebformNodeAccess.php in modules/webform_node/src/Access/WebformNodeAccess.php
WebformOptionsLimitAccess.php in modules/webform_options_limit/src/Access/WebformOptionsLimitAccess.php

File

src/Access/WebformEntityAccess.php, line 12

Namespace

Drupal\webform\Access
View source
class WebformEntityAccess {

  /**
   * Check whether the webform has drafts.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   A webform.
   * @param \Drupal\Core\Entity\EntityInterface|null $source_entity
   *   The source entity.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public static function checkDraftsAccess(WebformInterface $webform, EntityInterface $source_entity = NULL) {
    $draft = $webform
      ->getSetting('draft');
    switch ($draft) {
      case WebformInterface::DRAFT_AUTHENTICATED:
        $access_result = AccessResult::allowedIf(\Drupal::currentUser()
          ->isAuthenticated());
        break;
      case WebformInterface::DRAFT_ALL:
        $access_result = AccessResult::allowed();
        break;
      case WebformInterface::DRAFT_NONE:
      default:
        $access_result = AccessResult::forbidden();
        break;
    }
    return $access_result
      ->addCacheableDependency($webform);
  }

  /**
   * Check whether the webform has results.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   A webform.
   * @param \Drupal\Core\Entity\EntityInterface|null $source_entity
   *   The source entity.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public static function checkResultsAccess(WebformInterface $webform, EntityInterface $source_entity = NULL) {

    // If results are not disabled return neutral.
    if (!$webform
      ->getSetting('results_disabled')) {
      $access_result = AccessResult::allowed();
    }
    elseif (\Drupal::entityTypeManager()
      ->getStorage('webform_submission')
      ->getTotal($webform, $source_entity)) {
      $access_result = AccessResult::allowed();
    }
    else {
      $access_result = AccessResult::forbidden();
    }
    return $access_result
      ->addCacheableDependency($webform);
  }

  /**
   * Check whether the webform has log.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   A webform.
   * @param \Drupal\Core\Entity\EntityInterface|null $source_entity
   *   The source entity.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public static function checkLogAccess(WebformInterface $webform = NULL, EntityInterface $source_entity = NULL) {

    // ISSUE:
    // Devel routes do not use 'webform' parameter which throws the below error.
    // Some mandatory parameters are missing ("webform") to generate a URL for
    // route "entity.webform_submission.canonical"
    //
    // WORKAROUND:
    // Make sure webform parameter is set for all routes.
    // @see webform_menu_local_tasks_alter()
    if (!$webform && ($webform_submission = \Drupal::routeMatch()
      ->getParameter('webform_submission'))) {
      $webform = $webform_submission
        ->getWebform();
    }
    if (!$webform
      ->hasSubmissionLog()) {
      $access_result = AccessResult::forbidden()
        ->addCacheableDependency($webform);
    }
    else {
      $access_result = static::checkResultsAccess($webform, $source_entity);
    }
    return $access_result
      ->addCacheTags([
      'config:webform.settings',
    ]);
  }

  /**
   * Check whether a webform setting is set to specified value.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   A webform.
   * @param string $setting
   *   A webform setting.
   * @param string $value
   *   The setting value used to determine access.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public static function checkWebformSettingValue(WebformInterface $webform = NULL, $setting = NULL, $value = NULL) {
    return AccessResult::allowedIf($webform
      ->getSetting($setting) === $value)
      ->addCacheableDependency($webform);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformEntityAccess::checkDraftsAccess public static function Check whether the webform has drafts.
WebformEntityAccess::checkLogAccess public static function Check whether the webform has log.
WebformEntityAccess::checkResultsAccess public static function Check whether the webform has results.
WebformEntityAccess::checkWebformSettingValue public static function Check whether a webform setting is set to specified value.