public function MongodbNodeGrantStorage::write in MongoDB 8
Writes a list of grants to the database, deleting previously saved ones.
If a realm is provided, it will only delete grants from that realm, but it will always delete a grant from the 'all' realm. Modules that use node access can use this method when doing mass updates due to widespread permission changes.
Note: Don't call this method directly from a contributed module. Call \Drupal\node\NodeAccessControlHandlerInterface::acquireGrants() instead.
Parameters
\Drupal\node\NodeInterface $node: The node whose grants are being written.
array $grants: A list of grants to write. Each grant is an array that must contain the following keys: realm, gid, grant_view, grant_update, grant_delete. The realm is specified by a particular module; the gid is as well, and is a module-defined id to define grant privileges. each grant_* field is a boolean value.
string $realm: (optional) If provided, read/write grants for that realm only. Defaults to NULL.
bool $delete: (optional) If false, does not delete records. This is only for optimization purposes, and assumes the caller has already performed a mass delete of some form. Defaults to TRUE.
Overrides NodeGrantDatabaseStorageInterface::write
File
- mongodb_node/
src/ MongodbNodeGrantStorage.php, line 182 - Contains \Drupal\node\NodeGrantDatabaseStorage.
Class
- MongodbNodeGrantStorage
- Defines a controller class that handles the node grants system.
Namespace
Drupal\mongodb_nodeCode
public function write(NodeInterface $node, array $grants, $realm = NULL, $delete = TRUE) {
$collection = $this->mongo
->get('entity.node');
$query = [
'nid' => $node
->id(),
];
$record = $collection
->findOne($query) ?: [
'grant' => [],
];
$stored_grants = $record['grant'];
$ops = [
'view',
'update',
'delete',
];
if ($delete) {
foreach ($ops as $op) {
if ($realm) {
$stored_grants[$op] = array_values(array_filter($stored_grants[$op], function ($grant) use ($realm) {
return $grant['realm'] != $realm && $grant['realm'] != 'all';
}));
}
else {
unset($stored_grants[$op]);
}
}
}
// Only perform work when node_access modules are active.
if (!empty($grants) && count($this->moduleHandler
->getImplementations('node_grants'))) {
// If we have defined a granted langcode, use it. But if not, add a grant
// for every language this node is translated to.
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) {
foreach ($ops as $op) {
if ($grant["grant_{$op}"] >= 1) {
$stored_grants[$op][] = [
'langcode' => $grant_langcode,
'fallback' => (int) ($grant_langcode == $node
->language()
->getId()),
'gid' => $grant['gid'],
'realm' => $realm,
];
}
}
}
}
}
if ($record['grant'] !== $stored_grants) {
$newobj['$set']['grant'] = $stored_grants;
$collection
->update($query, $newobj, [
'upsert' => !$node
->id(),
]);
}
}