-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Split of the SecurityContext #11690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; | ||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
|
||
class GlobalVariablesTest extends TestCase | ||
{ | ||
private $container; | ||
private $globals; | ||
|
||
public function setUp() | ||
{ | ||
$this->container = new Container(); | ||
$this->globals = new GlobalVariables($this->container); | ||
} | ||
|
||
public function testGetSecurity() | ||
{ | ||
$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); | ||
|
||
$this->assertNull($this->globals->getSecurity()); | ||
$this->container->set('security.context', $securityContext); | ||
$this->assertSame($securityContext, $this->globals->getSecurity()); | ||
} | ||
|
||
public function testGetUser() | ||
{ | ||
// missing test cases to return null, only happy flow tested | ||
$securityContext = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); | ||
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); | ||
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); | ||
|
||
$this->container->set('security.token_storage', $securityContext); | ||
|
||
$token | ||
->expects($this->once()) | ||
->method('getUser') | ||
->will($this->returnValue($user)); | ||
|
||
$securityContext | ||
->expects($this->once()) | ||
->method('getToken') | ||
->will($this->returnValue($token)); | ||
|
||
$this->assertSame($user, $this->globals->getUser()); | ||
} | ||
|
||
public function testGetRequest() | ||
{ | ||
$this->markTestIncomplete(); | ||
} | ||
|
||
public function testGetSession() | ||
{ | ||
$this->markTestIncomplete(); | ||
} | ||
|
||
public function testGetEnvironment() | ||
{ | ||
$this->markTestIncomplete(); | ||
} | ||
|
||
public function testGetDubug() | ||
{ | ||
$this->markTestIncomplete(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
|
||
<parameters> | ||
<parameter key="security.context.class">Symfony\Component\Security\Core\SecurityContext</parameter> | ||
<parameter key="security.authorization_checker.class">Symfony\Component\Security\Core\Authorization\AuthorizationChecker</parameter> | ||
<parameter key="security.token_storage.class">Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage</parameter> | ||
|
||
<parameter key="security.user_checker.class">Symfony\Component\Security\Core\User\UserChecker</parameter> | ||
|
||
|
@@ -54,11 +56,19 @@ | |
|
||
<services> | ||
<service id="security.context" class="%security.context.class%"> | ||
<argument type="service" id="security.token_storage" /> | ||
<argument type="service" id="security.authorization_checker" /> | ||
</service> | ||
|
||
<service id="security.authorization_checker" class="%security.authorization_checker.class%"> | ||
<argument type="service" id="security.token_storage" /> | ||
<argument type="service" id="security.authentication.manager" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you forgot the |
||
<argument type="service" id="security.access.decision_manager" /> | ||
<argument>%security.access.always_authenticate_before_granting%</argument> | ||
</service> | ||
|
||
<service id="security.token_storage" class="%security.token_storage.class%" /> | ||
|
||
<!-- Authentication related services --> | ||
<service id="security.authentication.manager" class="%security.authentication.manager.class%" public="false"> | ||
<argument type="collection" /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authentication\Token\Storage; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
||
/** | ||
* TokenStorage contains a TokenInterface | ||
* | ||
* It gives access to the token representing the current user authentication. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
class TokenStorage implements TokenStorageInterface | ||
{ | ||
private $token; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getToken() | ||
{ | ||
return $this->token; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setToken(TokenInterface $token = null) | ||
{ | ||
$this->token = $token; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authentication\Token\Storage; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
||
/** | ||
* The TokenStorageInterface. | ||
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you created this interface, didn't you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In one of Stof's (now outdated) comments, hence I've put the original names there. |
||
*/ | ||
interface TokenStorageInterface | ||
{ | ||
/** | ||
* Returns the current security token. | ||
* | ||
* @return TokenInterface|null A TokenInterface instance or null if no authentication information is available | ||
*/ | ||
public function getToken(); | ||
|
||
/** | ||
* Sets the authentication token. | ||
* | ||
* @param TokenInterface $token A TokenInterface token, or null if no further authentication information should be stored | ||
*/ | ||
public function setToken(TokenInterface $token = null); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization; | ||
|
||
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; | ||
|
||
/** | ||
* AuthorizationChecker is the main authorization point of the Security component. | ||
* | ||
* It gives access to the token representing the current user authentication. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
class AuthorizationChecker implements AuthorizationCheckerInterface | ||
{ | ||
private $tokenStorage; | ||
private $accessDecisionManager; | ||
private $authenticationManager; | ||
private $alwaysAuthenticate; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param TokenStorageInterface $tokenStorage | ||
* @param AuthenticationManagerInterface $authenticationManager An AuthenticationManager instance | ||
* @param AccessDecisionManagerInterface $accessDecisionManager An AccessDecisionManager instance | ||
* @param bool $alwaysAuthenticate | ||
*/ | ||
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, AccessDecisionManagerInterface $accessDecisionManager, $alwaysAuthenticate = false) | ||
{ | ||
$this->tokenStorage = $tokenStorage; | ||
$this->authenticationManager = $authenticationManager; | ||
$this->accessDecisionManager = $accessDecisionManager; | ||
$this->alwaysAuthenticate = $alwaysAuthenticate; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token. | ||
*/ | ||
final public function isGranted($attributes, $object = null) | ||
{ | ||
if (null === ($token = $this->tokenStorage->getToken())) { | ||
throw new AuthenticationCredentialsNotFoundException('The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.'); | ||
} | ||
|
||
if ($this->alwaysAuthenticate || !$token->isAuthenticated()) { | ||
$this->tokenStorage->setToken($token = $this->authenticationManager->authenticate($token)); | ||
} | ||
|
||
if (!is_array($attributes)) { | ||
$attributes = array($attributes); | ||
} | ||
|
||
return $this->accessDecisionManager->decide($token, $attributes, $object); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization; | ||
|
||
/** | ||
* The AuthorizationCheckerInterface. | ||
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
interface AuthorizationCheckerInterface | ||
{ | ||
/** | ||
* Checks if the attributes are granted against the current authentication token and optionally supplied object. | ||
* | ||
* @param mixed $attributes | ||
* @param mixed $object | ||
* | ||
* @return bool | ||
*/ | ||
public function isGranted($attributes, $object = null); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be put in the 3.0 upgrade guide? (deprecations aren't documented in upgrade afaik)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have put them here on purpose. When people read it, they will know it will be removed in 3.0. I have no idea where else to give them a heads up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wouterj the deprecated goes in 2.6. The removal in 3.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof but deprecations are put in the changelog of a component and breaks in the upgrade file, or am I wrong?