vendor/symfony/security-core/Security.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Bundle\SecurityBundle\Security as NewSecurityHelper;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. /**
  17.  * Helper class for commonly-needed security tasks.
  18.  *
  19.  * @deprecated since Symfony 6.2, use \Symfony\Bundle\SecurityBundle\Security instead
  20.  */
  21. class Security implements AuthorizationCheckerInterface
  22. {
  23.     public const ACCESS_DENIED_ERROR '_security.403_error';
  24.     public const AUTHENTICATION_ERROR '_security.last_error';
  25.     public const LAST_USERNAME '_security.last_username';
  26.     /**
  27.      * @deprecated since Symfony 6.2, use \Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge::MAX_USERNAME_LENGTH instead
  28.      */
  29.     public const MAX_USERNAME_LENGTH 4096;
  30.     private ContainerInterface $container;
  31.     public function __construct(ContainerInterface $containerbool $triggerDeprecation true)
  32.     {
  33.         $this->container $container;
  34.         if ($triggerDeprecation) {
  35.             trigger_deprecation('symfony/security-core''6.2''The "%s" class is deprecated, use "%s" instead.'__CLASS__NewSecurityHelper::class);
  36.         }
  37.     }
  38.     public function getUser(): ?UserInterface
  39.     {
  40.         if (!$token $this->getToken()) {
  41.             return null;
  42.         }
  43.         return $token->getUser();
  44.     }
  45.     /**
  46.      * Checks if the attributes are granted against the current authentication token and optionally supplied subject.
  47.      */
  48.     public function isGranted(mixed $attributesmixed $subject null): bool
  49.     {
  50.         return $this->container->get('security.authorization_checker')
  51.             ->isGranted($attributes$subject);
  52.     }
  53.     public function getToken(): ?TokenInterface
  54.     {
  55.         return $this->container->get('security.token_storage')->getToken();
  56.     }
  57. }