You are here

node_view_permissions.module in Node View Permissions 8

Same filename and directory in other branches
  1. 7 node_view_permissions.module

The node view permission module.

File

node_view_permissions.module
View source
<?php

/**
 * @file
 * The node view permission module.
 * @see \Drupal\node\Tests\NodeQueryAlterTest
 * @see \Drupal\node\Tests\NodeAccessBaseTableTest
 */
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;

/**
 * Implements hook_node_access_records().
 */
function node_view_permissions_node_access_records(NodeInterface $node) {
  $grants = [];

  // We don't want to override view published permissions.
  $languages = $node
    ->getTranslationLanguages();
  foreach ($languages as $langcode => $language) {
    if ($langcode === Language::LANGCODE_NOT_APPLICABLE || $langcode === Language::LANGCODE_NOT_SPECIFIED) {

      // If node is not translatable, check the publish status of it.
      if ($node
        ->isPublished()) {
        $grants[] = [
          'realm' => "view_any_{$node->getType()}_content",
          'gid' => 1,
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
        $grants[] = [
          'realm' => "view_own_{$node->getType()}_content",
          'gid' => $node
            ->getOwnerId(),
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
      }
      else {
        $grants[] = [
          'realm' => "view_any_unpublished_{$node->getType()}_content",
          'gid' => 1,
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
        $grants[] = [
          'realm' => "view_own_unpublished_{$node->getType()}_content",
          'gid' => $node
            ->getOwnerId(),
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
      }
    }
    else {

      // If node is translated, check the publish status of the translation
      // and create separate realm for it.
      $translation = $node
        ->getTranslation($langcode);
      if ($translation
        ->isPublished()) {
        $grants[] = [
          'realm' => "view_any_{$node->getType()}_{$langcode}_content",
          'langcode' => $langcode,
          'gid' => 1,
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
        $grants[] = [
          'realm' => "view_own_{$node->getType()}_{$langcode}_content",
          'langcode' => $langcode,
          'gid' => $node
            ->getOwnerId(),
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
      }
      else {
        $grants[] = [
          'realm' => "view_any_unpublished_{$node->getType()}_{$langcode}_content",
          'langcode' => $langcode,
          'gid' => 1,
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
        $grants[] = [
          'realm' => "view_own_unpublished_{$node->getType()}_{$langcode}_content",
          'langcode' => $langcode,
          'gid' => $node
            ->getOwnerId(),
          'grant_view' => 1,
          'grant_update' => 0,
          'grant_delete' => 0,
          'priority' => 0,
        ];
      }
    }
  }
  return $grants;
}

/**
 * Implements hook_node_grants().
 */
function node_view_permissions_node_grants(AccountInterface $account, $op) {
  $grants = [];
  if ($op == 'view') {

    // Get current langcode.
    $node = \Drupal::routeMatch()
      ->getParameter('node');
    $language_manager = \Drupal::languageManager();
    $languages = \Drupal::languageManager()
      ->getLanguages();
    foreach ($languages as $langcode => $language) {
      foreach (NodeType::loadMultiple() as $type) {
        $type_id = $type
          ->id();
        if ($account
          ->hasPermission("view any {$type_id} content")) {
          $grants["view_any_{$type_id}_content"] = [
            1,
          ];

          // Also set grants for the current language.
          $grants["view_any_{$type_id}_{$langcode}_content"] = [
            1,
          ];
        }
        if ($account
          ->hasPermission("view own {$type_id} content")) {
          $grants["view_own_{$type_id}_content"] = [
            $account
              ->id(),
          ];

          // Also set grants for the current language.
          $grants["view_own_{$type_id}_{$langcode}_content"] = [
            $account
              ->id(),
          ];
        }

        // Add integration for Content Moderation.
        if ($account
          ->hasPermission("view any unpublished content") && $account
          ->hasPermission("view any {$type_id} content")) {
          $grants["view_any_unpublished_{$type_id}_content"] = [
            1,
          ];

          // Also set grants for the current language.
          $grants["view_any_unpublished_{$type_id}_{$langcode}_content"] = [
            1,
          ];
        }
        if ($account
          ->hasPermission("view own unpublished content") && $account
          ->hasPermission("view own {$type_id} content")) {
          $grants["view_own_unpublished_{$type_id}_content"] = [
            $account
              ->id(),
          ];

          // Also set grants for the current language.
          $grants["view_own_unpublished_{$type_id}_{$langcode}_content"] = [
            $account
              ->id(),
          ];
        }
      }
    }
  }
  return $grants;
}