Problem/Motivation

Part of #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset() effort to remove drupal_static() & drupal_static_reset() from node_access_view_all_nodes().

node_access_view_all_nodes can be merged into existing OO API.

Proposed resolution

  • Move node_access_view_all_nodes() functionality to \Drupal\node\NodeGrantDatabaseStorage::checkAll()
  • Deprecate node_access_view_all_nodes() in favour of \Drupal\node\NodeAccessControlHandler::checkAllGrants()

Remaining tasks

Review
[PP-1]

API changes

  • node_access_view_all_nodes() is deprecated.

Issue fork drupal-3038908

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Status: Active » Needs review
Issue tags: +@deprecated
Parent issue: » #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset()
StatusFileSize
new15.42 KB

Patch.

claudiu.cristea’s picture

Issue summary: View changes
andypost’s picture

Looks nice, but how this internal cache will be cleared? I guess it needs extra tests

Status: Needs review » Needs work

The last submitted patch, 2: 3038908-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

claudiu.cristea’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new15.44 KB
new1.65 KB

(...) but how this internal cache will be cleared?

@andypost, We already have the method on the interface: EntityAccessControlHandlerInterface::resetCache(). We only need to extend it.

claudiu.cristea’s picture

StatusFileSize
new17.41 KB
new3.65 KB

Cover also the case when 3rd party code tries to reset the static cache by doing:

drupal_static_reset('node_access_view_all_nodes');

Updated also the CR to account that.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/node/src/NodeAccessControlHandler.php
    @@ -192,4 +199,29 @@ public function checkAllGrants(AccountInterface $account) {
    +  /**
    +   * {@inheritdoc}
    +   */
    +  public function viewAllNodes(AccountInterface $account) {
    +    $account_id = $account->id();
    

    I think adding extra API's to EntityAccessControlHandlers is a bit problematic, but we have quite a few grant related methods there already, so it makes sense I suppose.

  2. +++ b/core/modules/node/tests/src/Unit/Access/NodeAccessTest.php
    @@ -0,0 +1,54 @@
    +   * @expectedDeprecation node_access_view_all_nodes() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getAccessControlHandler("node")->viewAllNodes($account). See https://www.drupal.org/node/3038909.
    +   * @see node_access_view_all_nodes();
    +   */
    +  public function testNodeAccessViewAllNodesDeprecation() {
    +    $container = new ContainerBuilder();
    +    $current_user = $this->prophesize(AccountProxyInterface::class);
    +    $container->set('current_user', $current_user->reveal());
    +    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    +    $node_access_control_handler = $this->prophesize(NodeAccessControlHandlerInterface::class);
    +    $entity_type_manager->getAccessControlHandler('node')->willReturn($node_access_control_handler->reveal());
    +    $container->set('entity_type.manager', $entity_type_manager->reveal());
    +    \Drupal::setContainer($container);
    +
    +    require_once $this->root . '/core/modules/node/node.module';
    +    node_access_view_all_nodes();
    +  }
    

    We don't do unit tests for functions, as loading stuff then pollutes other unit tests.

    I'd suggest to simply make this a kernel test, should be much simpler?

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.

ridhimaabrol24’s picture

Status: Needs work » Needs review
StatusFileSize
new16.74 KB

Rerolled patch for 9.1.x
Moved test methods to core/modules/node/tests/src/Kernel/NodeAccessTest.php
Please review!

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.

longwave’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Needs another reroll, also needs the deprecation updating.

vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new16.73 KB

Re-roll patch created, Also updated deprecations. Please have a look.

vsujeetkumar’s picture

StatusFileSize
new16.71 KB
new5.05 KB

Fixed format issue, Please have a look.

Status: Needs review » Needs work

The last submitted patch, 16: 3038908-16.patch, failed testing. View results

vsujeetkumar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new16.65 KB
new2.01 KB

Fixed fail deprecation tests, Please have a look.

claudiu.cristea’s picture

The following issues we're fixed in MR.

  1. +++ b/core/includes/bootstrap.inc
    @@ -625,6 +625,12 @@ function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
    +      @trigger_error("Using drupal_static_reset() with 'node_access_view_all_nodes' as parameter is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal::entityTypeManager()->getAccessControlHandler('node')->resetCache() instead. See https://www.drupal.org/node/3038909.", E_USER_DEPRECATED);
    
    +++ b/core/modules/node/node.module
    @@ -868,37 +868,31 @@ function node_access_grants($op, AccountInterface $account) {
    +  @trigger_error('node_access_view_all_nodes() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal::entityTypeManager()->getAccessControlHandler("node")->viewAllNodes($account). See https://www.drupal.org/node/3038909.', E_USER_DEPRECATED);
    
    +++ b/core/modules/node/src/Cache/NodeAccessGrantsCacheContext.php
    @@ -20,6 +22,30 @@
    +      @trigger_error('The $entity_type_manager parameter will be mandatory before Drupal 9.0.0. See https://www.drupal.org/node/3038909.', E_USER_DEPRECATED);
    

    Should avoid dot at the end of message.

  2. +++ b/core/modules/node/src/NodeAccessControlHandler.php
    @@ -194,4 +201,29 @@ public function checkAllGrants(AccountInterface $account) {
    +  public function viewAllNodes(AccountInterface $account) {
    
    +++ b/core/modules/node/src/NodeAccessControlHandlerInterface.php
    @@ -58,4 +58,27 @@ public function countGrants();
    +  public function viewAllNodes(AccountInterface $account);
    

    New code, let's strict type the return.

  3. +++ b/core/modules/node/tests/src/Kernel/NodeAccessTest.php
    @@ -123,4 +129,39 @@ public function testUnsupportedOperation() {
    +  public function testNodeAccessViewAllNodesDeprecation() {
    ...
    +  public function testNodeAccessViewAllNodesCacheResetDeprecation() {
    

    #8.2, not fully addressed. We should drop container stuff and file inclusion. Kernel tests offers all.

berdir’s picture

Added some comments.

daffie’s picture

daffie’s picture

Status: Needs review » Needs work

For the unresolved threads on the MR.

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.

acbramley made their first commit to this issue’s fork.

acbramley’s picture

acbramley’s picture

Title: Deprecate node_access_view_all_nodes(). Move its functionality in NodeAccessControlHandlerInterface » Deprecate node_access_view_all_nodes()
Issue summary: View changes
Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new89 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

acbramley’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record updates

Can the CR also be updated that drupal_static_reset() with the node_grant key is deprecated.

acbramley’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record updates

Done

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

LGTM

catch’s picture

Status: Reviewed & tested by the community » Needs work

One comment on the MR.

acbramley’s picture

Status: Needs work » Needs review

Converted to use a memory cache

smustgrave’s picture

Status: Needs review » Needs work

Think we missed the 11.2 boat can those be updated to 11.3, reviewed again and closed the rest of the threads as answered.

If you are another contributor eager to jump in, please allow the previous poster(s) at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!

acbramley’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! All feedback appears to be addressed

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Related issues: +#3532197: [meta] Deprecate node.module functions

Adding issue credit.

As per @acbramley, I'm concerned that the focus of this issue is about the use of drupal_static and not the node access API. I think it is great to refactor out usages of drupal static - but this needs to be done with an eye to the resulting API. We have the following node access related functions left in node.module and we should have a plan for them:

  • node_access_grants
  • node_access_view_all_nodes
  • node_access_needs_rebuild
  • node_access_rebuild
  • _node_access_rebuild_batch_operation
  • _node_access_rebuild_batch_finished

Are there issues for the other methods? It feels as though they split down into two groups:

  • node_access_grants and node_access_view_all_nodes
  • The *rebuild* ones

Are there existing issues? I think not looking at #3532197: [meta] Deprecate node.module functions.

I think we need to agree the API shape before doing this issue and once we've done that we should focus the issues on doing that and as a by product remove drupal_static usage where we can.

I think that converting node_access_grants and node_access_view_all_nodes to a single service makes sense. I also think the service should be marked as @internal because it is API glue and if you want to affect this API you sure be implementing the hooks and not doing anything to this service.

acbramley’s picture

Status: Needs work » Needs review

Added #3533299: Deprecate node access rebuild functions and updated #2473041: Restructure node access grant behavior into the node access handler

I think node_access_grants could live on the service that this provides, maybe with a new method name as per the IS in that issue to more accurately describe what it does (and so it's not so confusing with hook_node_grants)

What do you think @alexpott?

alexpott’s picture

Status: Needs review » Needs work

@acbramley yes I think this issue could create a service for both of these methods in node.module. I've been trying to think if any of the existing node access services could be locations and I think I agree with the earlier posts - a separate service is a good idea. I considered node.grant_storage but I think this is not about storage and the new service needs to be injected into that one. I also considered cache_context.user.node_grants - or maybe making a new service that is also a cache context but that felt overly complex to. Therefore a new service with two public methods for each function we're replacing and marked @internal because it is not meant to be extended - the extension points are the node grant hooks.

acbramley’s picture

Status: Needs work » Needs review

How's that?

The other method should be moved in the other issue

alexpott’s picture

Status: Needs review » Needs work

@acbramley I think we should widen the scope to include the other function here. Doing it piecemeal results in a less complete API for no real gain.

acbramley’s picture

Issue summary: View changes
acbramley’s picture

Title: Deprecate node_access_view_all_nodes() » Deprecate node_access_view_all_nodes and node_access_grants
Issue summary: View changes
acbramley’s picture

Status: Needs work » Needs review

Deprecated node_access_grants as well and added the note to the @internal tag.

I think a good next step would be to flesh out #2473041: Restructure node access grant behavior into the node access handler. The first step should probably be to remove all/most of the grants functions from NodeAccessControlHandler, especially the ones that just proxy to the grant storage service. Like there's no reason hasViewAllNodesGrant or NodeRequirements::runtime needs to call a function on the access control handler...

I also found some interesting stuff like NodeHooks1::nodeAccess which implements hook_ENTITY_TYPE_access for its own entity type 🤷‍♂️ There's no wonder why barely anyone understands this system fully (including myself)

alexpott’s picture

So after refactoring node_access_view_all_nodes() into \Drupal\node\NodeGrantDatabaseStorage::checkAll() - see https://git.drupalcode.org/project/drupal/-/merge_requests/12586 - I think this issue can return to it's original intent - because there is no real API change. And then we can use #2473041: Restructure node access grant behavior into the node access handler to decide how to deal with node_access_grants() as part of an issue that is completely API focussed.

@acbramley sorry it is taken a while to see through the API trees here but I think that the new MR points to a better place :)

alexpott’s picture

One thing that is "interesting" is that node_access_view_all_nodes() is documented to return a bool. ATM in 11.x it only does this if there are no hook_node_grants implementations. Otherwise it returns a 0 or 1 integer because that's what Drupal::entityTypeManager()->getAccessControlHandler('node')->checkAllGrants($account) returns. With the new MR it will always return a 0 or 1. Boo to us and our lax return types. I think we should do a separate issue and CR to change \Drupal\node\NodeAccessControlHandlerInterface::checkAllGrants() and \Drupal\node\NodeGrantDatabaseStorageInterface::checkAll() to return a bool as this is a TRUE / FALSE access check and an integer is confusing.

acbramley’s picture

Status: Needs review » Needs work

Added some notes on both MRs, I don't really think I like either now lol

alexpott’s picture

@acbramley I've responded to your points on the new MR.

acbramley’s picture

Status: Needs work » Needs review

I guess we need someone else to weigh in, I don't feel this is much cleaner than my MR but it's hard when this whole system is a bowl of spaghetti in the first place.

I'd appreciate if you could add any notes to #2473041: Restructure node access grant behavior into the node access handler on what you'd like to achieve there too since we've flip flopped a couple of times.

xjm’s picture

Node access subsystem maintainer here.

I haven't had a chance to review the MR in detail yet (will try to do so soon) but I strongly, strongly advise against making any API or behavior changes in this issue. The functionality of this and all the procedural functions in the node access system has a lot of edgecase behaviors that are fiddly and changing them even slightly can break access control behavior on sites with complicated implementations. We don't only have to worry about individual contrib modules; we have to worry about sites with multiple access control modules, or custom code that implements custom access control and/or integrates multiple access control modules, which is very common.

The refactoring and eventual deprecation of the whole API is much more feasible now that D7 is EOL, but for scope management's sake and for the sake of not having surprise behavior changes or access bypasses on some weird edgecase sites, none of it should happen in this issue. This issue should only deprecate and move the code so we can get it out of the .module file. We have an opportunity for many followups!

Thanks!

xjm’s picture

Issue tags: +Node access
xjm’s picture

Title: Deprecate node_access_view_all_nodes and node_access_grants » Deprecate node_access_view_all_nodes() and node_access_grants()
acbramley’s picture

Title: Deprecate node_access_view_all_nodes() and node_access_grants() » Deprecate node_access_view_all_nodes()

This is back to just node_access_view_all_nodes

catch’s picture

Adding #2199001: Statically cache node access grants as related issue, don't think it's relevant to this issue but it would be relevant to further refactoring to try to get rid of the circular/semi-circular dependencies.

xjm’s picture

Issue tags: +Needs followup

Followups followups ➡️ 😇

alexpott changed the visibility of the branch 3038908-viewAllNodes to hidden.

alexpott’s picture

Issue summary: View changes

Updated issue summary reflect the approach.

alexpott’s picture

@xjm I think we have all the follow-ups in place already. I found an existing issue for the return types - #2863737: Change the return type of NodeGrantDatabaseStorageInterface::checkAll() and NodeAccessControlHandler::checkAllGrants() to a bool and the deprecation / moving about of node.module code has plenty of issues #2473041: Restructure node access grant behavior into the node access handler and #3532197: [meta] Deprecate node.module functions for example. Is there something else we need to put in place?

alexpott’s picture

acbramley’s picture

Issue tags: -Needs followup

Rebased, fixed a minor issue in the deprecation message and updated the CR a bit. I think this is good to go now. Agree that the follow ups we have in #63 cover plenty.

The main API change is going from returning TRUE when there are no grant hook implementations to returning 1 (to match the return signature of checkAll). That will then change in 2863737 so I think that's fine for now. The deprecated function still returns what it used to.

Leaving in NR since this is tagged for subsystem maintainer review which is not me.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

acbramley’s picture

Status: Needs work » Needs review

Rebased again.

xjm’s picture

Re:

Leaving in NR since this is tagged for subsystem maintainer review which is not me.

If it's blocked on subsystem maintainers for an extended period in general it can also be escalated to framework managers, although in this case since a subsytem maintainer is also a release manager someone RTBCing it with the tag on is fine as well. Meanwhile though I'm also going to ping the other active maintainer for the subsystem since I still don't have time to look at this this week.

agentrickard’s picture

From the node_access subsystem perspective, this seems fine.

From a module developer perspective, it's also fine, as I consider node_access_view_all_nodes() to be an internal function and don't call it directly. Even if I did, there is an OO replacement I can use, and this code directs me to that.

xjm’s picture

Untagging from #70; thanks Ken!

Note for committers, as I mentioned in Slack: The one thing here that needs very careful attention from a reviewer is the test coverage, to ensure the coverage is exactly identical with the new API. The fact that the test passes is not sufficient by itself.

xjm’s picture

Oh, belatedly confirming #63 -- @alexpott and I already discussed that in Slack last week but it looks like our consensus that the followups are covered did not make it back onto the issue. So we are all good. Thanks!

xjm’s picture

And updated credits.

acbramley’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Great, this is RTBC then

  • catch committed 5703ce35 on 11.x
    Issue #3038908 by acbramley, claudiu.cristea, alexpott, xjm, smustgrave...
catch’s picture

Status: Reviewed & tested by the community » Fixed

This looks good to me. Committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

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