Д – Дизайн
/** * https://github.com/symfony/symfony/blob/5.0/src/Symfony/Component/Security/Core/Authentication/Token/TokenInterface.php#L45-L53 * Returns a user representation. * * @return string|object Can be a UserInterface instance, an object implementing a __toString method, * or the username as a regular string * * @see AbstractToken::setUser() */ public function getUser();
Н – Наслаждение
// Authorized echo get_class($tokenStorage->getToken()->getUser()); // App\Entity\User // Anonymous echo $tokenStorage->getToken()->getUser(); // 'anon.'
О – Очарование
// https://github.com/symfony/symfony/blob/5.0/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php#L361-L377 protected function getUser() { if (!$this->container->has('security.token_storage')) { throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".'); } if (null === $token = $this->container->get('security.token_storage')->getToken()) { return null; } if (!\is_object($user = $token->getUser())) { // e.g. anonymous authentication return null; } return $user; }
2020.02.06 19:59