You are here

public function MongodbNodeGrantStorage::alterQuery in MongoDB 8

Alters a query when node access is required.

Parameters

mixed $query: Query that is being altered.

array $tables: A list of tables that need to be part of the alter.

string $op: The operation to be performed on the node. Possible values are:

  • "view"
  • "update"
  • "delete"
  • "create"

\Drupal\Core\Session\AccountInterface $account: A user object representing the user for whom the operation is to be performed.

string $base_table: The base table of the query.

Return value

int Status of the access check.

Overrides NodeGrantDatabaseStorageInterface::alterQuery

File

mongodb_node/src/MongodbNodeGrantStorage.php, line 121
Contains \Drupal\node\NodeGrantDatabaseStorage.

Class

MongodbNodeGrantStorage
Defines a controller class that handles the node grants system.

Namespace

Drupal\mongodb_node

Code

public function alterQuery($query, array $tables, $op, AccountInterface $account, $base_table) {
  if (!($langcode = $query
    ->getMetaData('langcode'))) {
    $langcode = FALSE;
  }

  // Find all instances of the base table being joined -- could appear
  // more than once in the query, and could be aliased. Join each one to
  // the node_access table.
  $grants = node_access_grants($op, $account);
  foreach ($tables as $nalias => $tableinfo) {
    $table = $tableinfo['table'];
    if (!$table instanceof SelectInterface && $table == $base_table) {

      // Set the subquery.
      $subquery = $this->database
        ->select('node_access', 'na')
        ->fields('na', array(
        'nid',
      ));

      // If any grant exists for the specified user, then user has access to the
      // node for the specified operation.
      $langcodes = $this->languageManager
        ->getLanguages();
      $fallbacks = [
        0,
        1,
      ];
      if ($this->languageManager
        ->isMultilingual()) {
        if ($langcode === FALSE) {
          $fallbacks = [
            1,
          ];
        }
        else {
          $langcodes = [
            $langcode,
          ];
        }
      }
      $grant_conditions = $this
        ->buildGrantsQueryCondition($grants, $langcodes, $fallbacks);

      // Attach conditions to the subquery for nodes.
      if (count($grant_conditions
        ->conditions())) {
        $subquery
          ->condition($grant_conditions);
      }
      $subquery
        ->condition('na.grant_' . $op, 1, '>=');

      // Add langcode-based filtering if this is a multilingual site.
      if (\Drupal::languageManager()
        ->isMultilingual()) {

        // 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("{$nalias}.{$field} = na.nid");
      $query
        ->exists($subquery);
    }
  }
}