You are here

function node_node_access in Drupal 8

Same name and namespace in other branches
  1. 7 modules/node/node.module \node_node_access()
  2. 9 core/modules/node/node.module \node_node_access()
  3. 10 core/modules/node/node.module \node_node_access()

Implements hook_ENTITY_TYPE_access().

Related topics

File

core/modules/node/node.module, line 1007
The core module that allows content to be submitted to the site.

Code

function node_node_access(NodeInterface $node, $op, AccountInterface $account) {
  $type = $node
    ->bundle();

  // Note create access is handled by hook_ENTITY_TYPE_create_access().
  switch ($op) {
    case 'update':
      $access = AccessResult::allowedIfHasPermission($account, 'edit any ' . $type . ' content');
      if (!$access
        ->isAllowed() && $account
        ->hasPermission('edit own ' . $type . ' content')) {
        $access = $access
          ->orIf(AccessResult::allowedIf($account
          ->id() == $node
          ->getOwnerId())
          ->cachePerUser()
          ->addCacheableDependency($node));
      }
      break;
    case 'delete':
      $access = AccessResult::allowedIfHasPermission($account, 'delete any ' . $type . ' content');
      if (!$access
        ->isAllowed() && $account
        ->hasPermission('delete own ' . $type . ' content')) {
        $access = $access
          ->orIf(AccessResult::allowedIf($account
          ->id() == $node
          ->getOwnerId()))
          ->cachePerUser()
          ->addCacheableDependency($node);
      }
      break;
    default:
      $access = AccessResult::neutral();
  }
  return $access;
}