Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal-3338541
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
drupal-3338541
Commits
f927e20b
Commit
f927e20b
authored
8 years ago
by
catch
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2753733
by alexpott, klausi: AccountProxy can do unnecessary user loads to get an ID
parent
08025b5e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/lib/Drupal/Core/Session/AccountProxy.php
+14
-4
14 additions, 4 deletions
core/lib/Drupal/Core/Session/AccountProxy.php
core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php
+53
-0
53 additions, 0 deletions
core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php
with
67 additions
and
4 deletions
core/lib/Drupal/Core/Session/AccountProxy.php
+
14
−
4
View file @
f927e20b
...
...
@@ -22,10 +22,19 @@ class AccountProxy implements AccountProxyInterface {
*/
protected
$account
;
/**
* Account id.
*
* @var int
*/
protected
$id
=
0
;
/**
* Initial account id.
*
* @var int
*
* @deprecated Scheduled for removal in Drupal 8.4.x. Use $this->id instead.
*/
protected
$initialAccountId
;
...
...
@@ -39,6 +48,7 @@ public function setAccount(AccountInterface $account) {
$account
=
$account
->
getAccount
();
}
$this
->
account
=
$account
;
$this
->
id
=
$account
->
id
();
date_default_timezone_set
(
drupal_get_user_timezone
());
}
...
...
@@ -47,11 +57,11 @@ public function setAccount(AccountInterface $account) {
*/
public
function
getAccount
()
{
if
(
!
isset
(
$this
->
account
))
{
if
(
$this
->
i
nitialAccountI
d
)
{
if
(
$this
->
id
)
{
// After the container is rebuilt, DrupalKernel sets the initial
// account to the id of the logged in user. This is necessary in order
// to refresh the user account reference here.
$this
->
setAccount
(
$this
->
loadUserEntity
(
$this
->
i
nitialAccountI
d
));
$this
->
setAccount
(
$this
->
loadUserEntity
(
$this
->
id
));
}
else
{
$this
->
account
=
new
AnonymousUserSession
();
...
...
@@ -65,7 +75,7 @@ public function getAccount() {
* {@inheritdoc}
*/
public
function
id
()
{
return
$this
->
getAccount
()
->
id
()
;
return
$this
->
id
;
}
/**
...
...
@@ -160,7 +170,7 @@ public function setInitialAccountId($account_id) {
throw
new
\LogicException
(
'AccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy'
);
}
$this
->
initialAccountId
=
$account_id
;
$this
->
id
=
$this
->
initialAccountId
=
$account_id
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php
0 → 100644
+
53
−
0
View file @
f927e20b
<?php
namespace
Drupal\Tests\Core\Session
;
use
Drupal\Core\Session\AccountInterface
;
use
Drupal\Tests\UnitTestCase
;
use
Drupal\Core\Session\AccountProxy
;
/**
* @coversDefaultClass \Drupal\Core\Session\AccountProxy
* @group Session
*/
class
AccountProxyTest
extends
UnitTestCase
{
/**
* @covers ::id
* @covers ::setInitialAccountId
*/
public
function
testId
()
{
$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
());
$current_user
=
$this
->
prophesize
(
AccountInterface
::
class
);
$current_user
->
id
()
->
willReturn
(
2
);
$account_proxy
->
setAccount
(
$current_user
->
reveal
());
$this
->
assertSame
(
2
,
$account_proxy
->
id
());
}
/**
* @covers ::setInitialAccountId
*/
public
function
testSetInitialAccountIdException
()
{
$this
->
setExpectedException
(
\LogicException
::
class
);
$account_proxy
=
new
AccountProxy
();
$current_user
=
$this
->
prophesize
(
AccountInterface
::
class
);
$account_proxy
->
setAccount
(
$current_user
->
reveal
());
$account_proxy
->
setInitialAccountId
(
1
);
}
}
namespace
Drupal\Core\Session
;
if
(
!
function_exists
(
'drupal_get_user_timezone'
))
{
function
drupal_get_user_timezone
()
{
return
date_default_timezone_get
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment