SecurityContext.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. @trigger_error('The '.__NAMESPACE__.'\SecurityContext class is deprecated since Symfony 2.6 and will be removed in 3.0. Use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage or Symfony\Component\Security\Core\Authorization\AuthorizationChecker instead.', E_USER_DEPRECATED);
  12. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  18. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  19. /**
  20. * SecurityContext is the main entry point of the Security component.
  21. *
  22. * It gives access to the token representing the current user authentication.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26. *
  27. * @deprecated since version 2.6, to be removed in 3.0.
  28. */
  29. class SecurityContext implements SecurityContextInterface
  30. {
  31. /**
  32. * @var TokenStorageInterface
  33. */
  34. private $tokenStorage;
  35. /**
  36. * @var AuthorizationCheckerInterface
  37. */
  38. private $authorizationChecker;
  39. /**
  40. * For backwards compatibility, the signature of sf <2.6 still works.
  41. *
  42. * @param TokenStorageInterface|AuthenticationManagerInterface $tokenStorage
  43. * @param AuthorizationCheckerInterface|AccessDecisionManagerInterface $authorizationChecker
  44. * @param bool $alwaysAuthenticate Only applicable with old signature
  45. */
  46. public function __construct($tokenStorage, $authorizationChecker, $alwaysAuthenticate = false)
  47. {
  48. $oldSignature = $tokenStorage instanceof AuthenticationManagerInterface && $authorizationChecker instanceof AccessDecisionManagerInterface;
  49. $newSignature = $tokenStorage instanceof TokenStorageInterface && $authorizationChecker instanceof AuthorizationCheckerInterface;
  50. // confirm possible signatures
  51. if (!$oldSignature && !$newSignature) {
  52. throw new \BadMethodCallException('Unable to construct SecurityContext, please provide the correct arguments');
  53. }
  54. if ($oldSignature) {
  55. // renamed for clarity
  56. $authenticationManager = $tokenStorage;
  57. $accessDecisionManager = $authorizationChecker;
  58. $tokenStorage = new TokenStorage();
  59. $authorizationChecker = new AuthorizationChecker($tokenStorage, $authenticationManager, $accessDecisionManager, $alwaysAuthenticate);
  60. }
  61. $this->tokenStorage = $tokenStorage;
  62. $this->authorizationChecker = $authorizationChecker;
  63. }
  64. /**
  65. * @deprecated since version 2.6, to be removed in 3.0. Use TokenStorageInterface::getToken() instead.
  66. *
  67. * {@inheritdoc}
  68. */
  69. public function getToken()
  70. {
  71. return $this->tokenStorage->getToken();
  72. }
  73. /**
  74. * @deprecated since version 2.6, to be removed in 3.0. Use TokenStorageInterface::setToken() instead.
  75. *
  76. * {@inheritdoc}
  77. */
  78. public function setToken(TokenInterface $token = null)
  79. {
  80. return $this->tokenStorage->setToken($token);
  81. }
  82. /**
  83. * @deprecated since version 2.6, to be removed in 3.0. Use AuthorizationCheckerInterface::isGranted() instead.
  84. *
  85. * {@inheritdoc}
  86. */
  87. public function isGranted($attributes, $object = null)
  88. {
  89. return $this->authorizationChecker->isGranted($attributes, $object);
  90. }
  91. }