We're still suffering from a 10 year old confusion here...

#2473021: Deprecate NodeAccessControlHandlerInterface::writeGrants() for removal in Drupal 9.0.x has raised the confusion level again significantly — we currently have the following (merging NodeAccessControlHandlerInterface and NodeAccessControlHandler):

  /**
   * Gets the list of node access grants.
   *
   * This function is called to check the access grants for a node. It collects
   * all node access grants for the node from hook_node_access_records()
   * implementations, allows these grants to be altered via
   * hook_node_access_records_alter() implementations, and returns the grants to
   * the caller.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The $node to acquire grants for.
   *
   * @return array $grants
   *   The access rules for the node.
   */
  public function acquireGrants(NodeInterface $node) {
    $grants = $this->moduleHandler->invokeAll('node_access_records', array($node));
    // Let modules alter the grants.
    $this->moduleHandler->alter('node_access_records', $grants, $node);
    // If no grants are set and the node is published, then use the default grant.
    if (empty($grants) && $node->isPublished()) {
      $grants[] = array('realm' => 'all', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
    }
    return $grants;
  }

  /**
   * Writes a list of grants to the database, deleting any previously saved ones.
   *
   * Modules that use node access can use this function when doing mass updates
   * due to widespread permission changes.
   *
   * Note: Don't call this function directly from a contributed module. Call
   * \Drupal\node\NodeAccessControlHandlerInterface::acquireGrants() instead.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node whose grants are being written.
   * @param $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.
   *
   * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
   *   Use \Drupal\node\NodeAccessControlHandlerInterface::acquireGrants().
   */
  public function writeGrants(NodeInterface $node, $delete = TRUE) {
    $grants = $this->acquireGrants($node);
    $this->grantStorage->write($node, $grants, NULL, $delete);
  }

acquireGrants() just returns the grant records, but it doesn't do anything in the database. Calling it "instead" does not work!

writeGrants() calls acquireGrants() and then writes them to the database. It is the one that everyone needs to call, even though it's deprecated and its docblock says "Don't call this function". Core (and probably everyone else) works around this contradiction by just copying the two lines in writeGrants(), which is obviously stupid.

acquireGrants(), OTOH, is only useful if you're going to write those grant records, which is precisely what writeGrants() does. There's one exception: Devel Node Access would like to call acquireGrants() in order to verify that what's in the node_access table is actually correct.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

salvis created an issue. See original summary.

salvis’s picture

Status: Active » Needs review
FileSize
5.02 KB

Much of the confusion comes from less than ideal naming of those methods which goes back to D5. I propose to keep the deprecated status of writeGrants() and introduce a new method called acquireAndWriteGrants().

And I would like to keep acquireGrants() as a separate method for the benefit of contribs that want to find out what records we should have in the NA table — without writing to the table!

markdorison’s picture

  1. Explanation and patch #2 make a ton of sense to me.
  2. An optional step further might be to also deprecate/replace acquireGrants() with a method name that is more clear in that it is just retrieving the grants. retrieveGrants()? Not sure of our level of tolerance for these types of changes.
  3. +++ b/core/modules/node/src/NodeAccessControlHandlerInterface.php
    @@ -29,13 +29,21 @@
    +   *   (optional) If false, does not delete records. This is only for optimization
    

    'false' should be capitalized (FALSE). Line also wraps beyond 80 characters.

salvis’s picture

Thank you for your review!

2. retrieveGrants() sounds an awful lot like readGrants() or loadGrants(), as if we were just "retrieving" them from the database or so. But there's a whole lot more going in during this call — I would vote for keeping the acquireGrants() name.

3. I just copied that comment as it was, but I'm attaching a new patch with fixed capitalization and line lengths. The same comment appears also in NodeGrantDatabaseStorageInterface.php and this patch fixes it there, too, plus another comment line that is too long.

markdorison’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/node/src/NodeGrantDatabaseStorageInterface.php
@@ -67,12 +67,12 @@ public function alterQuery($query, array $tables, $op, AccountInterface $account
-   *   (optional) If provided, read/write grants for that realm only. Defaults to
-   *   NULL.
+   *   (optional) If provided, read/write grants for that realm only. Defaults
+   *   to NULL.
    * @param 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.
+   *   (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.

Unrelated changes. Should be a separate issue.

+++ b/core/modules/node/src/NodeAccessControlHandlerInterface.php
@@ -29,23 +29,32 @@
-   * Note: Don't call this function directly from a contributed module. Call
-   * \Drupal\node\NodeAccessControlHandlerInterface::acquireGrants() instead.

We need to know why this is here. Just removing it without that consideration being noted on the issue doesn't make sense. The fact this is already deprecated means that someone has already considered that necessary....

It came from node_access_write_grants() in D7 but actually \Drupal\node\NodeAccessControlHandlerInterface::writeGrants is doing what node_access_acquire_grants() did in D7. Nice.

I think we should do this quite differently. We should undeprecate \Drupal\node\NodeAccessControlHandlerInterface::writeGrants and not introduce a new method. We should remove the warning not to use and revert much of #2473021: Deprecate NodeAccessControlHandlerInterface::writeGrants() for removal in Drupal 9.0.x. We should file a follow-up to rename \Drupal\node\NodeAccessControlHandlerInterface::acquireGrants and deprecate that method. And lastly we should file a followup to discuss the architecture of NodeAccessControlHandler / node.grant_storage. The equivalent to D7's node_access_write_grants() is actually on the node.grant_storage service and now we're recommending people call it all the time (precisely the opposite advice lol).

alexpott’s picture

tbh I'm not sure that the NodeAccessControlHandler should have all the methods to write and acquire grants. In my mind we should have a NodeGrants service that has all the logic pertaining to the effects of hook_node_access_records() and hook_node_grants().

salvis’s picture

Assigned: salvis » Unassigned
Status: Needs work » Needs review
FileSize
5.33 KB
1.87 KB

Thank you for reviewing. I'm attaching a revised patch without the "unrelated changes". I had put them there because that comment is an exact copy of the other one.

+++ b/core/modules/node/src/NodeAccessControlHandlerInterface.php
@@ -29,23 +29,32 @@
-   * Note: Don't call this function directly from a contributed module. Call
-   * \Drupal\node\NodeAccessControlHandlerInterface::acquireGrants() instead.

We need to know why this is here. Just removing it without that consideration being noted on the issue doesn't make sense. The fact this is already deprecated means that someone has already considered that necessary....

The reason why it's here is that I asked to have it put there in D7 in #237634: Rename node_access_write_grants() to _node_access_write_grants() and discourage its use. That issue also has the reasoning. In short, the corresponding function was called node_access_write_grants() at the time and it would just delete the existing grant records and write the provided set of new ones. If a node access module called that function, it was unable to coexist with any other node access modules, breaking them!

Unfortunately, what was committed five years ago was not perfect, which has perpetuated the confusion. I'm trying to clean it up now. Since the current writeGrants() method faithfully calls acquireGrants(), it does not have this problem anymore, and it is the method that everyone should call, rather than duplicating the two calls inside.

It came from node_access_write_grants() in D7 but actually \Drupal\node\NodeAccessControlHandlerInterface::writeGrants is doing what node_access_acquire_grants() did in D7. Nice.

Yes, that's why I propose to keep it deprecated and introduce the new acquireAndWriteGrants() method, kind of just renaming writeGrants(). This will leave us in a safe and comprehensible state.

With the introduction of hook_node_access_records_alter() the whole mechanism was changed anyway, and I don't see why we should have to carry over parts of the old procedural idiom into the D8 class.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kalistos’s picture

Status: Needs review » Needs work

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Lendude’s picture

Category: Bug report » Task
Issue tags: +Bug Smash Initiative

This seems aimed at making an improvement to DX, but nothing is broken as such. So reclassifying as a task. If somebody feels something is broken and it should be a bug, please add a test-only patch that demonstrates the failure.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.