Problem/Motivation

The Workbench moderation module implements hook_entity_storage_load() this is fired during a container rebuild before the container is rebuilt because AccountProxy::id() will unnecessarily load a user to preserve the current user ID across a container rebuild.

Note that the new content_moderation module for core no longer takes this approach but the account proxy should still not fire this hook.

Proposed resolution

Don't load the user in AccountProxy::id()

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott created an issue. See original summary.

alexpott’s picture

dawehner’s picture

Looks great in general I would argue!

+++ b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php
@@ -0,0 +1,55 @@
+
+namespace Drupal\Tests\Core\Session {
+
+  use Drupal\Core\Session\AccountInterface;
+  use Drupal\Tests\UnitTestCase;
+  use Drupal\Core\Session\AccountProxy;

You can get rid of the indentation by using this: https://3v4l.org/vWFKR

The last submitted patch, 2: 2753733-2.test-only.patch, failed testing.

alexpott’s picture

@dawehner good idea.

znerol’s picture

Good idea. I'm wondering whether there is any situation where the id property gets out of sync?

dawehner’s picture

I'm wondering whether there is any situation where the id property gets out of sync?

Well, its just set in two locations, but sure someone could change the session entry somehow directly, couldn't they?

alexpott’s picture

but sure someone could change the session entry somehow directly, couldn't they?

Isn't that TRUE in HEAD?

I think this patch is good to go.

Berdir’s picture

+1, what about the protected property rename. No problem for 8.2 I think, we exclude protected there, but not sure about 8.1, where we really try to keep changes as minimal as possible?

alexpott’s picture

@Berdir yeah I guess we can keep the property name the same and add a big comment. Seems the "nicer" thing to do if people are extending.

alexpott’s picture

Here's a patch preserving BC.

alexpott’s picture

neclimdul’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php
@@ -0,0 +1,53 @@
+    $account_proxy = new AccountProxy();
+    $this->assertSame(0, $account_proxy->id());
+    $account_proxy->setInitialAccountId(1);
+    $this->assertFalse(\Drupal::hasContainer());
+    // If the following call loaded the user entity it would call
+    // AccountProxy::loadUserEntity() which would fail because the container
+    // does not exist.
+    $this->assertSame(1, $account_proxy->id());

Got it. Couldn't figure out how this worked but this explains it and why id() is special.

+++ b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php
@@ -0,0 +1,53 @@
+namespace Drupal\Core\Session;
+
+if (!function_exists('drupal_get_user_timezone')) {
+  function drupal_get_user_timezone() {
+    return date_default_timezone_get();
+  }
+}

gugh, I hate when we have to do this...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: 2753733-12.patch, failed testing.

alexpott’s picture

Status: Needs work » Reviewed & tested by the community

This was a DrupalCI error

alexpott’s picture

Issue summary: View changes
apaderno’s picture

+++ b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php
@@ -0,0 +1,53 @@
+namespace Drupal\Core\Session ;

There is a space before the semicolon.

For the rest, it seems fine.

neclimdul’s picture

@kiamlaluno I think you reviewed #11not #12 where he fixed that. :)

apaderno’s picture

Yes, it seems so.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Nice find.

+++ b/core/lib/Drupal/Core/Session/AccountProxy.php
@@ -23,9 +23,19 @@ class AccountProxy implements AccountProxyInterface {
+   *
+   * @deprecated in Drupal 8.1.9 and to be removed in Drupal 9.
+   *   Use $this->id instead.
    */

This makes a lot of assumptions about what 8.1.9 will be - if it's a security release it won't include this patch for example.

Elsewhere we've been using "Scheduled for removal in 9.0.0, use foo instead" which is a bit simpler.
https://api.drupal.org/api/drupal/core%21tests%21Drupal%21FunctionalTest...

Also protected properties are @internal, so I think we could remove this in 8.3.x not?

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

klausi’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Needs work » Needs review
FileSize
3.77 KB
557 bytes

Since it is very easy to preserve backwards compatibility for the protected property in this case I think it is fine to keep it even if we consider protected properties @internal.

Fixed the @deprecated message.

neclimdul’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Let's change the deprecation message to removal in 8.4.x - it's nice to allow code to work with two branches at a time, but we shouldn't be maintaining unnecessary bc layers for the entirety of 8.x's lifetime.

klausi’s picture

Status: Needs work » Needs review
FileSize
190.56 KB
541 bytes

Sure.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
3.77 KB

Here's a patch without the extra in #25

  • catch committed f927e20 on 8.3.x
    Issue #2753733 by alexpott, klausi: AccountProxy can do unnecessary user...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.3.x, thanks!

Status: Fixed » Closed (fixed)

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

Gábor Hojtsy’s picture

The promise of the deprecation message committed here did not work out. We should not be removing stuff from Drupal 8, so opened #3033317: AccountProxy initialAccountId and ViewKernelTestBase will only be removed in Drupal 9.0.0, fix deprecation messages to fix the message.