function NodeGrantDatabaseStorage::alterQuery

Same name in other branches
  1. 9 core/modules/node/src/NodeGrantDatabaseStorage.php \Drupal\node\NodeGrantDatabaseStorage::alterQuery()
  2. 8.9.x core/modules/node/src/NodeGrantDatabaseStorage.php \Drupal\node\NodeGrantDatabaseStorage::alterQuery()
  3. 11.x core/modules/node/src/NodeGrantDatabaseStorage.php \Drupal\node\NodeGrantDatabaseStorage::alterQuery()

File

core/modules/node/src/NodeGrantDatabaseStorage.php, line 155

Class

NodeGrantDatabaseStorage
Defines a storage handler class that handles the node grants system.

Namespace

Drupal\node

Code

public function alterQuery($query, array $tables, $operation, AccountInterface $account, $base_table) {
    if (!($langcode = $query->getMetaData('langcode'))) {
        $langcode = FALSE;
    }
    // Find all instances of the base table being joined which could appear
    // more than once in the query, and could be aliased. Join each one to
    // the node_access table.
    $grants = node_access_grants($operation, $account);
    // If any grant exists for the specified user, then user has access to the
    // node for the specified operation.
    $grant_conditions = $this->buildGrantsQueryCondition($grants);
    $grants_exist = count($grant_conditions->conditions()) > 0;
    $is_multilingual = \Drupal::languageManager()->isMultilingual();
    foreach ($tables as $table_alias => $tableinfo) {
        $table = $tableinfo['table'];
        if (!$table instanceof SelectInterface && $table == $base_table) {
            // Set the subquery.
            $subquery = $this->database
                ->select('node_access', 'na')
                ->fields('na', [
                'nid',
            ]);
            // Attach conditions to the sub-query for nodes.
            if ($grants_exist) {
                $subquery->condition($grant_conditions);
            }
            $subquery->condition('na.grant_' . $operation, 1, '>=');
            // Add langcode-based filtering if this is a multilingual site.
            if ($is_multilingual) {
                // If no specific langcode to check for is given, use the grant entry
                // which is set as a fallback.
                // If a specific langcode is given, use the grant entry for it.
                if ($langcode === FALSE) {
                    $subquery->condition('na.fallback', 1, '=');
                }
                else {
                    $subquery->condition('na.langcode', $langcode, '=');
                }
            }
            $field = 'nid';
            // Now handle entities.
            $subquery->where("[{$table_alias}].[{$field}] = [na].[nid]");
            if (empty($tableinfo['join type'])) {
                $query->exists($subquery);
            }
            else {
                // If this is a join, add the node access check to the join condition.
                // This requires using $query->getTables() to alter the table
                // information.
                $join_cond = $query->andConditionGroup()
                    ->exists($subquery);
                $join_cond->where($tableinfo['condition'], $query->getTables()[$table_alias]['arguments']);
                $query->getTables()[$table_alias]['arguments'] = [];
                $query->getTables()[$table_alias]['condition'] = $join_cond;
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.