Problem/Motivation

NodeAccessControlHandlerInterface::writeGrants() has the following documentation note:

Note: Don't call this function directly from a contributed module. Call node_access_acquire_grants() instead.

There are two problems with this:

  1. node_access_acquire_grants() no longer exists; it has been replaced with NodeAccessControlHandlerInterface::acquireGrants().
  2. We shouldn't have a public method for something that, essentially, doesn't seem to be intended as public.

The method is simply a wrapper for:

    $grants = $this->acquireGrants($node);
    $this->grantStorage->write($node, $grants, NULL, $delete);

The only usages are in node_access_rebuild() (which logically should be a method on the handler or storage anyway):

    $grants = $this->acquireGrants($node);
    $this->grantStorage->write($node, $grants, NULL, $delete);

And in Node::postSave():

    if ($this->isDefaultRevision()) {
      \Drupal::entityManager()->getAccessControlHandler('node')->writeGrants($this, $update);
    }

Proposed resolution

Is there any reason not to just remove the method? It seems like a mostly-dead misdirection that is only tested implicitly and that explicitly says it's not supposed to be used. Since it would be a narrowing of the API, but a BC break, I think it would be okay to deprecate the method in 8.0.x for removal in 9.x.

Remaining tasks

  1. Discuss.
  2. Patch.

User interface changes

None.

API changes

NodeAccessControlHandlerInterface::writeGrants() is deprecated.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

xjm’s picture

Issue summary: View changes
xjm’s picture

There is also a bogus reference to node_access_write_grants() in the hook_node_access_records() documentation.

xjm’s picture

Issue tags: +D8 Accelerate Dev Days
jcnventura’s picture

Status: Active » Needs review
FileSize
4.07 KB

I agree, and it certainly doesn't hurt to deprecate it. At least it will serve to tell anyone that uses it that they shouldn't (which is already the case, if the documentation is right).

jcnventura’s picture

FileSize
2.32 KB
6.14 KB

Slightly improved version with some documentation clean-up.

Now the question seems to be if the same warning to not use NodeGrantStorageInterface::write() directly also means that we should deprecate it or remove the warning. That function isn't a simple wrapper like this one, so I'd be strongly against deprecating it (and besides, what's the benefit of a storage interface if you can't write to it??).

Mile23’s picture

Issue tags: +@deprecated

#2473041: Deprecate node_access_grants() and move its functionality to NodeAccessControlHandler is basically postponed on this issue.

Retesting to see if it still doesn't explode.

+++ b/core/modules/node/src/NodeGrantDatabaseStorageInterface.php
@@ -59,7 +59,7 @@ public function alterQuery($query, array $tables, $op, AccountInterface $account
    * Note: Don't call this method directly from a contributed module. Call
-   * node_access_write_grants() instead.
+   * \Drupal\node\NodeAccessControlHandlerInterface::acquireGrants() instead.

Not really in scope here, but if contrib shouldn't call this, then it shouldn't be part of the interface.

+++ b/core/modules/node/src/Entity/Node.php
@@ -111,7 +111,10 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
     if ($this->isDefaultRevision()) {
-      \Drupal::entityManager()->getAccessControlHandler('node')->writeGrants($this, $update);
+      /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
+      $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
+      $grants = $access_control_handler->acquireGrants($this);
+      \Drupal::service('node.grant_storage')->write($this, $grants, NULL, $update);
     }

Is there really no place these services are injected into the entity?

Mile23 queued 5: deprecate-2473021-5.patch for re-testing.

Mile23’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
$ git apply deprecate-2473021-5.patch 
error: patch failed: core/modules/node/src/NodeAccessControlHandlerInterface.php:34
error: core/modules/node/src/NodeAccessControlHandlerInterface.php: patch does not apply
amitgoyal’s picture

Status: Needs work » Needs review
FileSize
5.93 KB

Reroll of #5.

amitgoyal’s picture

Issue tags: -Needs reroll

Status: Needs review » Needs work

The last submitted patch, 9: deprecate-2473021-9.patch, failed testing.

jcnventura’s picture

Status: Needs work » Needs review

Retesting, as #9 is correct.

As to the comments in #6:
1. It's only correcting the comment to point to the right place. Moving this method out of the interface is as referred, out of scope.
2. No. These services are not injected. There's one use each of these services and they both use the same code. The only thing I found, two steps above in the class hierarchy, is that we could replace \Drupal::entityManager() with $this->entityManager() (which in turn would call \Drupal::entityManager(), thus adding one extra stack call).

jcnventura’s picture

Requesting some triage on this. It's mostly documentation, but it also contains some preparation for removing the code in writeGrants, by copying it's logic to the places where it's used.

jcnventura’s picture

Issue tags: +rc target triage

And the tag.

catch’s picture

Should it be deprecated or @internal?

xjm’s picture

I think deprecating it is correct.

Mile23’s picture

Title: Deprecate NodeAccessControlHandlerInterface::writeGrants() » Deprecate NodeAccessControlHandlerInterface::writeGrants() for removal in Drupal 9.0.x
Version: 8.0.x-dev » 8.1.x-dev
Issue tags: -rc target triage
naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community

Patch looks good to be in.

  • catch committed 758a81e on 8.1.x
    Issue #2473021 by jcnventura, amitgoyal: Deprecate...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.