vendor/symfony/security-core/Authentication/Token/Storage/UsageTrackingTokenStorage.php line 44

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\Authentication\Token\Storage;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  16. /**
  17. * A token storage that increments the session usage index when the token is accessed.
  18. *
  19. * @author Nicolas Grekas <p@tchwork.com>
  20. */
  21. final class UsageTrackingTokenStorage implements TokenStorageInterface, ServiceSubscriberInterface
  22. {
  23. private $storage;
  24. private $container;
  25. private $enableUsageTracking = false;
  26. public function __construct(TokenStorageInterface $storage, ContainerInterface $container)
  27. {
  28. $this->storage = $storage;
  29. $this->container = $container;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getToken(): ?TokenInterface
  35. {
  36. if ($this->shouldTrackUsage()) {
  37. // increments the internal session usage index
  38. $this->getSession()->getMetadataBag();
  39. }
  40. return $this->storage->getToken();
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function setToken(?TokenInterface $token = null): void
  46. {
  47. $this->storage->setToken($token);
  48. if ($token && $this->shouldTrackUsage()) {
  49. // increments the internal session usage index
  50. $this->getSession()->getMetadataBag();
  51. }
  52. }
  53. public function enableUsageTracking(): void
  54. {
  55. $this->enableUsageTracking = true;
  56. }
  57. public function disableUsageTracking(): void
  58. {
  59. $this->enableUsageTracking = false;
  60. }
  61. public static function getSubscribedServices(): array
  62. {
  63. return [
  64. 'request_stack' => RequestStack::class,
  65. ];
  66. }
  67. private function getSession(): SessionInterface
  68. {
  69. // BC for symfony/security-bundle < 5.3
  70. if ($this->container->has('session')) {
  71. trigger_deprecation('symfony/security-core', '5.3', 'Injecting the "session" in "%s" is deprecated, inject the "request_stack" instead.', __CLASS__);
  72. return $this->container->get('session');
  73. }
  74. return $this->container->get('request_stack')->getSession();
  75. }
  76. private function shouldTrackUsage(): bool
  77. {
  78. if (!$this->enableUsageTracking) {
  79. return false;
  80. }
  81. // BC for symfony/security-bundle < 5.3
  82. if ($this->container->has('session')) {
  83. return true;
  84. }
  85. if (!$this->container->get('request_stack')->getMainRequest()) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. }