You are here

protected function UserRNGSelection::buildEntityQuery in RNG - Events and Registrations 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/EntityReferenceSelection/UserRNGSelection.php \Drupal\rng\Plugin\EntityReferenceSelection\UserRNGSelection::buildEntityQuery()
  2. 3.x src/Plugin/EntityReferenceSelection/UserRNGSelection.php \Drupal\rng\Plugin\EntityReferenceSelection\UserRNGSelection::buildEntityQuery()

Builds an EntityQuery to get referenceable entities.

Parameters

string|null $match: (Optional) Text to match the label against. Defaults to NULL.

string $match_operator: (Optional) The operation the matching should be done with. Defaults to "CONTAINS".

Return value

\Drupal\Core\Entity\Query\QueryInterface The EntityQuery object with the basic conditions and sorting applied to it.

Overrides RNGSelectionBase::buildEntityQuery

File

src/Plugin/EntityReferenceSelection/UserRNGSelection.php, line 26

Class

UserRNGSelection
Provides selection for user entities when registering.

Namespace

Drupal\rng\Plugin\EntityReferenceSelection

Code

protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
  $query = parent::buildEntityQuery($match, $match_operator);

  // User entity.
  if (isset($match)) {
    $query
      ->condition('name', $match, $match_operator);
  }

  // Only show un-blocked users.
  $query
    ->condition('status', 1);

  // Remove anonymous user:
  $query
    ->condition($this->entityType
    ->getKey('id'), '0', '<>');

  // Event access rules.
  $condition_count = 0;
  $rules = $this->eventMeta
    ->getRules('rng_event.register', TRUE);
  foreach ($rules as $rule) {
    if ($this
      ->ruleGrantsOperation($rule, 'create')) {
      foreach ($rule
        ->getConditions() as $condition_storage) {

        // Do not use condition if it cannot alter query.
        $condition = $condition_storage
          ->createInstance();
        if ($condition instanceof RNGConditionInterface) {
          $condition_count++;
          $condition
            ->alterQuery($query);
        }
      }
    }
  }

  // Cancel the query if there are no conditions.
  if (!$condition_count) {
    $query
      ->condition($this->entityType
      ->getKey('id'), NULL, 'IS NULL');
    return $query;
  }

  // Apply proxy registration permissions for the current user.
  $proxy_count = 0;

  // If user can register `authenticated` users:
  $all_users = FALSE;
  $group = $query
    ->orConditionGroup();

  // Self.
  if ($this->currentUser
    ->isAuthenticated()) {
    if ($this->currentUser
      ->hasPermission('rng register self')) {
      $proxy_count++;
      $group
        ->condition($this->entityType
        ->getKey('id'), $this->currentUser
        ->id(), '=');
    }
  }
  foreach (user_roles(TRUE) as $role) {
    $role_id = $role
      ->id();
    if ($this->currentUser
      ->hasPermission("rng register role {$role_id}")) {
      if ($role_id == 'authenticated') {
        $all_users = TRUE;
        break;
      }
      else {
        $proxy_count++;
        $group
          ->condition('roles', $role_id, '=');
      }
    }
  }
  if ($all_users) {

    // Do not add any conditions.
  }
  elseif ($proxy_count) {
    $query
      ->condition($group);
  }
  else {

    // Cancel the query:
    $query
      ->condition($this->entityType
      ->getKey('id'), NULL, 'IS NULL');
  }
  return $query;
}