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