From 7493184ac8a10b1df230b645b53b62bbe14a328a Mon Sep 17 00:00:00 2001 From: David Ward Date: Tue, 1 Jun 2021 07:46:44 -0700 Subject: [PATCH] Symfony 5.3 deprecates $token->getUsername() - use new `$token->getUserIdentifier()` if the function exists - check if the token is a `NullToken` as well as null --- Context/UserContextFactory.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Context/UserContextFactory.php b/Context/UserContextFactory.php index 5a9d9ee..367d56d 100644 --- a/Context/UserContextFactory.php +++ b/Context/UserContextFactory.php @@ -15,6 +15,7 @@ use Qandidate\Toggle\Context; use Qandidate\Toggle\ContextFactory; +use Symfony\Component\Security\Core\Authentication\Token\NullToken; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; class UserContextFactory extends ContextFactory @@ -35,8 +36,8 @@ public function createContext(): Context $token = $this->tokenStorage->getToken(); - if (null !== $token) { - $context->set('username', $token->getUsername()); + if (null !== $token && !$token instanceof NullToken) { + $context->set('username', method_exists($token, 'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername()); } return $context;