View source
<?php
namespace Drupal\node;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
class NodeGrantDatabaseStorage implements NodeGrantDatabaseStorageInterface {
protected $database;
protected $moduleHandler;
protected $languageManager;
public function __construct(Connection $database, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
$this->database = $database;
$this->moduleHandler = $module_handler;
$this->languageManager = $language_manager;
}
public function access(NodeInterface $node, $operation, AccountInterface $account) {
if (!in_array($operation, [
'view',
'update',
'delete',
])) {
return AccessResult::neutral();
}
if (!$this->moduleHandler
->getImplementations('node_grants') || !$node
->id()) {
if ($operation === 'view') {
return AccessResult::allowedIf($node
->isPublished());
}
else {
return AccessResult::neutral();
}
}
$query = $this->database
->select('node_access');
$query
->addExpression('1');
$query
->condition('grant_' . $operation, 1, '>=');
$nids = $query
->andConditionGroup()
->condition('nid', $node
->id())
->condition('langcode', $node
->language()
->getId());
$status = $node
->isPublished();
if ($status) {
$nids = $query
->orConditionGroup()
->condition($nids)
->condition('nid', 0);
}
$query
->condition($nids);
$query
->range(0, 1);
$grants = static::buildGrantsQueryCondition(node_access_grants($operation, $account));
if (count($grants) > 0) {
$query
->condition($grants);
}
$set_cacheability = function (AccessResult $access_result) use ($operation) {
$access_result
->addCacheContexts([
'user.node_grants:' . $operation,
]);
if ($operation !== 'view') {
$access_result
->setCacheMaxAge(0);
}
return $access_result;
};
if ($query
->execute()
->fetchField()) {
return $set_cacheability(AccessResult::allowed());
}
else {
return $set_cacheability(AccessResult::neutral());
}
}
public function checkAll(AccountInterface $account) {
$query = $this->database
->select('node_access');
$query
->addExpression('COUNT(*)');
$query
->condition('nid', 0)
->condition('grant_view', 1, '>=');
$grants = static::buildGrantsQueryCondition(node_access_grants('view', $account));
if (count($grants) > 0) {
$query
->condition($grants);
}
return $query
->execute()
->fetchField();
}
public function alterQuery($query, array $tables, $op, AccountInterface $account, $base_table) {
if (!($langcode = $query
->getMetaData('langcode'))) {
$langcode = FALSE;
}
$grants = node_access_grants($op, $account);
$grant_conditions = static::buildGrantsQueryCondition($grants);
$grants_exist = count($grant_conditions
->conditions()) > 0;
$is_multilingual = \Drupal::languageManager()
->isMultilingual();
foreach ($tables as $nalias => $tableinfo) {
$table = $tableinfo['table'];
if (!$table instanceof SelectInterface && $table == $base_table) {
$subquery = $this->database
->select('node_access', 'na')
->fields('na', [
'nid',
]);
if ($grants_exist) {
$subquery
->condition($grant_conditions);
}
$subquery
->condition('na.grant_' . $op, 1, '>=');
if ($is_multilingual) {
if ($langcode === FALSE) {
$subquery
->condition('na.fallback', 1, '=');
}
else {
$subquery
->condition('na.langcode', $langcode, '=');
}
}
$field = 'nid';
$subquery
->where("{$nalias}.{$field} = na.nid");
$query
->exists($subquery);
}
}
}
public function write(NodeInterface $node, array $grants, $realm = NULL, $delete = TRUE) {
if ($delete) {
$query = $this->database
->delete('node_access')
->condition('nid', $node
->id());
if ($realm) {
$query
->condition('realm', [
$realm,
'all',
], 'IN');
}
$query
->execute();
}
if (!empty($grants) && count($this->moduleHandler
->getImplementations('node_grants'))) {
$query = $this->database
->insert('node_access')
->fields([
'nid',
'langcode',
'fallback',
'realm',
'gid',
'grant_view',
'grant_update',
'grant_delete',
]);
$fallback_langcode = $node
->getUntranslated()
->language()
->getId();
foreach ($grants as $grant) {
if ($realm && $realm != $grant['realm']) {
continue;
}
if (isset($grant['langcode'])) {
$grant_languages = [
$grant['langcode'] => $this->languageManager
->getLanguage($grant['langcode']),
];
}
else {
$grant_languages = $node
->getTranslationLanguages(TRUE);
}
foreach ($grant_languages as $grant_langcode => $grant_language) {
if ($grant['grant_view'] || $grant['grant_update'] || $grant['grant_delete']) {
$grant['nid'] = $node
->id();
$grant['langcode'] = $grant_langcode;
if ($grant['langcode'] == $fallback_langcode) {
$grant['fallback'] = 1;
}
else {
$grant['fallback'] = 0;
}
$query
->values($grant);
}
}
}
$query
->execute();
}
}
public function delete() {
$this->database
->truncate('node_access')
->execute();
}
public function writeDefault() {
$this->database
->insert('node_access')
->fields([
'nid' => 0,
'realm' => 'all',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
])
->execute();
}
public function count() {
return $this->database
->query('SELECT COUNT(*) FROM {node_access}')
->fetchField();
}
public function deleteNodeRecords(array $nids) {
$this->database
->delete('node_access')
->condition('nid', $nids, 'IN')
->execute();
}
protected static function buildGrantsQueryCondition(array $node_access_grants) {
$grants = new Condition("OR");
foreach ($node_access_grants as $realm => $gids) {
if (!empty($gids)) {
$and = new Condition('AND');
$grants
->condition($and
->condition('gid', $gids, 'IN')
->condition('realm', $realm));
}
}
return $grants;
}
}