You are here

public function WebformAjax::autocompleteAccess in Webform CiviCRM Integration 8.5

Access callback. Check if user has permission to view autocomplete results.

Parameters

Webform $webform:

string $fid: Webform component id

Return value

bool

Overrides WebformAjaxInterface::autocompleteAccess

1 call to WebformAjax::autocompleteAccess()
WebformAjax::contactAjax in src/WebformAjax.php
Load one or more contacts via ajax

File

src/WebformAjax.php, line 190
Front-end form ajax handler.

Class

WebformAjax
Class WebformAjax

Namespace

Drupal\webform_civicrm

Code

public function autocompleteAccess($webform, $fid) {
  $user = \Drupal::currentUser();
  if (!$fid || empty($webform
    ->getHandler('webform_civicrm'))) {
    return FALSE;
  }
  $element = $webform
    ->getElement($fid);
  if (empty($element) || !$webform
    ->access('submission_create')) {
    return FALSE;
  }
  if ($user
    ->id() === 1 || $user
    ->hasPermission('access all webform results') || $user
    ->hasPermission('access own webform results') && $webform
    ->uuid() == $user
    ->id()) {
    return TRUE;
  }

  /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
  $element_manager = \Drupal::service('plugin.manager.webform.element');
  $element_instance = $element_manager
    ->getElementInstance($element);

  // @todo test after moving to getElementProperty.
  if (!empty($element_instance
    ->getElementProperty($element, 'private'))) {
    return FALSE;
  }

  /* TODO figure out what this means in Drupal 8
     if (\Drupal::state()->get('webform_submission_access_control', 1)) {
       $allowed_roles = array();
       foreach ($node->webform['roles'] as $rid) {
         $allowed_roles[$rid] = isset($user->roles[$rid]) ? TRUE : FALSE;
       }
       if (array_search(TRUE, $allowed_roles) === FALSE) {
         return FALSE;
       }
     }*/

  // ToDo - to be refactored -> it would be safer to return FALSE by default.
  return TRUE;
}