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

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.     /**
  24.      * @deprecated since Symfony 6.2, use \Symfony\Bundle\SecurityBundle\Security::ACCESS_DENIED_ERROR instead
  25.      */
  26.     public const ACCESS_DENIED_ERROR '_security.403_error';
  27.     /**
  28.      * @deprecated since Symfony 6.2, use \Symfony\Bundle\SecurityBundle\Security::AUTHENTICATION_ERROR instead
  29.      */
  30.     public const AUTHENTICATION_ERROR '_security.last_error';
  31.     /**
  32.      * @deprecated since Symfony 6.2, use \Symfony\Bundle\SecurityBundle\Security::LAST_USERNAME instead
  33.      */
  34.     public const LAST_USERNAME '_security.last_username';
  35.     /**
  36.      * @deprecated since Symfony 6.2, use \Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge::MAX_USERNAME_LENGTH instead
  37.      */
  38.     public const MAX_USERNAME_LENGTH 4096;
  39.     private ContainerInterface $container;
  40.     public function __construct(ContainerInterface $containerbool $triggerDeprecation true)
  41.     {
  42.         $this->container $container;
  43.         if ($triggerDeprecation) {
  44.             trigger_deprecation('symfony/security-core''6.2''The "%s" class is deprecated, use "%s" instead.'__CLASS__NewSecurityHelper::class);
  45.         }
  46.     }
  47.     public function getUser(): ?UserInterface
  48.     {
  49.         if (!$token $this->getToken()) {
  50.             return null;
  51.         }
  52.         return $token->getUser();
  53.     }
  54.     /**
  55.      * Checks if the attributes are granted against the current authentication token and optionally supplied subject.
  56.      */
  57.     public function isGranted(mixed $attributesmixed $subject null): bool
  58.     {
  59.         return $this->container->get('security.authorization_checker')
  60.             ->isGranted($attributes$subject);
  61.     }
  62.     public function getToken(): ?TokenInterface
  63.     {
  64.         return $this->container->get('security.token_storage')->getToken();
  65.     }
  66. }