8000 [Security] Deprecate returning stringish objects from Security::getUser by ro0NL · Pull Request #27943 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Deprecate returning stringish objects from 8000 Security::getUser #27943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-4.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Security
custom anonymous and remember me token classes is deprecated. To
use custom tokens, extend the existing `Symfony\Component\Security\Core\Authentication\Token\AnonymousToken`
or `Symfony\Component\Security\Core\Authentication\Token\RememberMeToken`.
* Accessing the user object that is not an instance of `UserInterface` from `Security::getUser()` is deprecated.

SecurityBundle
--------------
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Security
* The `FirewallMapInterface::getListeners()` method must return an array of 3 elements,
the 3rd one must be either a `LogoutListener` instance or `null`.
* The `AuthenticationTrustResolver` constructor arguments have been removed.
* A user object that is not an instance of `UserInterface` cannot be accessed from `Security::getUser()` anymore and returns `null` instead.

SecurityBundle
--------------
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
use custom tokens, extend the existing `Symfony\Component\Security\Core\Authentication\Token\AnonymousToken`
or `Symfony\Component\Security\Core\Authentication\Token\RememberMeToken`.
* allow passing null as $filter in LdapUserProvider to get the default filter
* accessing the user object that is not an instance of `UserInterface` from `Security::getUser()` is deprecated

4.1.0
-----
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Security/Core/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function getUser()
return null;
}

if (!$user instanceof UserInterface) {
@trigger_error(sprintf('Accessing the user object "%s" that is not an instance of "%s" from "%s()" is deprecated since Symfony 4.2, use "getToken()->getUser()" instead.', get_class($user), UserInterface::class, __METHOD__), E_USER_DEPRECATED);
Copy link
Member
@xabbuh xabbuh Jul 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$user may not be an object here: is_object($user) ? get_class($user) : $user

Ah no, we check that just before.

//return null; // 5.0 behavior
}

return $user;
}

Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Security/Core/Tests/SecurityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,34 @@ public function getUserTests()

yield array('string_username', null);

//yield array(new StringishUser(), null); // 5.0 behavior

$user = new User('nice_user', 'foo');
yield array($user, $user);
}

/**
* @group legacy
* @expectedDeprecation Accessing the user object "Symfony\Component\Security\Core\Tests\StringishUser" that is not an instance of "Symfony\Component\Security\Core\User\UserInterface" from "Symfony\Component\Security\Core\Security::getUser()" is deprecated since Symfony 4.2, use "getToken()->getUser()" instead.
*/
public function testGetUserLegacy()
{
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
$token->expects($this->any())
->method('getUser')
->will($this->returnValue($user = new StringishUser()));
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();

$tokenStorage->expects($this->once())
->method('getToken')
->will($this->returnValue($token));

$container = $this->createContainer('security.token_storage', $tokenStorage);

$security = new Security($container);
$this->assertSame($user, $security->getUser());
}

public function testIsGranted()
{
$authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
Expand Down Expand Up @@ -95,3 +119,11 @@ private function createContainer($serviceId, $serviceObject)
return $container;
}
}

class StringishUser
{
public function __toString()
{
return 'stringish_user';
}
}
0