You are here

protected function WebformElementBase::checkAccessRule in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElementBase.php \Drupal\webform\Plugin\WebformElementBase::checkAccessRule()

Checks an access rule against a user account's roles and id.

Parameters

array $element: The element.

string $operation: The operation.

\Drupal\Core\Session\AccountInterface $account: The user session for which to check access.

Return value

bool The access result. Returns a TRUE if access is allowed.

See also

\Drupal\webform\Entity\Webform::checkAccessRule

1 call to WebformElementBase::checkAccessRule()
WebformElementBase::checkAccessRules in src/Plugin/WebformElementBase.php
Check element access (rules).

File

src/Plugin/WebformElementBase.php, line 994

Class

WebformElementBase
Provides a base class for a webform element.

Namespace

Drupal\webform\Plugin

Code

protected function checkAccessRule(array $element, $operation, AccountInterface $account) {

  // If no access rules are set return NULL (no opinion).
  // @see \Drupal\webform\Plugin\WebformElementBase::defaultBaseProperties
  if (!isset($element['#access_' . $operation . '_roles']) && !isset($element['#access_' . $operation . '_users']) && !isset($element['#access_' . $operation . '_permissions'])) {
    return TRUE;
  }

  // If access roles are not set then use the anonymous and authenticated
  // roles from the element's default properties.
  // @see \Drupal\webform\Plugin\WebformElementBase::defaultBaseProperties
  if (!isset($element['#access_' . $operation . '_roles'])) {
    $element['#access_' . $operation . '_roles'] = $this
      ->getDefaultProperty('access_' . $operation . '_roles') ?: [];
  }
  if (array_intersect($element['#access_' . $operation . '_roles'], $account
    ->getRoles())) {
    return TRUE;
  }
  if (isset($element['#access_' . $operation . '_users']) && in_array($account
    ->id(), $element['#access_' . $operation . '_users'])) {
    return TRUE;
  }
  if (isset($element['#access_' . $operation . '_permissions'])) {
    foreach ($element['#access_' . $operation . '_permissions'] as $permission) {
      if ($account
        ->hasPermission($permission)) {
        return TRUE;
      }
    }
  }
  return FALSE;
}