8000 [Security] removed usage of the deprecated SecurityContextInterface · symfony/symfony@5f20448 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f20448

Browse files
committed
[Security] removed usage of the deprecated SecurityContextInterface
1 parent 0ea8ae9 commit 5f20448

File tree

48 files changed

+463
-376
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+463
-376
lines changed

src/Symfony/Bridge/Twig/Extension/SecurityExtension.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Security\Acl\Voter\FieldVote;
1515
use Symfony\Component\Security\Core\SecurityContextInterface;
16+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1617

1718
/**
1819
* SecurityExtension exposes security context features.
@@ -21,24 +22,29 @@
2122
*/
2223
class SecurityExtension extends \Twig_Extension
2324
{
24-
private $context;
25+
private $securityChecker;
2526

26-
public function __construct(SecurityContextInterface $context = null)
27+
/**
28+
* @param SecurityContextInterface|AuthorizationCheckerInterface
29+
*
30+
* Passing a SecurityContextInterface as a first argument was deprecated in 2.7 and will be removed in 3.0
31+
*/
32+
public function __construct($securityChecker = null)
2733
{
28-
$this->context = $context;
34+
$this->securityChecker = $securityChecker;
2935
}
3036

3137
public function isGranted($role, $object = null, $field = null)
3238
{
33-
if (null === $this->context) {
39+
if (null === $this->securityChecker) {
3440
return false;
3541
}
3642

3743
if (null !== $field) {
3844
$object = new FieldVote($object, $field);
3945
}
4046

41-
return $this->context->isGranted($role, $object);
47+
return $this->securityChecker->isGranted($role, $object);
4248
}
4349

4450
/**

src/Symfony/Bundle/FrameworkBundle/Resources/config/form_csrf.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<service id="form.type_extension.csrf" class="Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension">
1313
<tag name="form.type_extension" alias="form" />
14-
<argument type="service" id="form.csrf_provider" />
14+
<argument type="service" id="security.csrf.token_manager" />
1515
<argument>%form.type_extension.csrf.enabled%</argument>
1616
<argument>%form.type_extension.csrf.field_name%</argument>
1717
<argument type="service" id="translator.default" />

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115

116116
<service id="templating.form.renderer" class="%templating.form.renderer.class%" public="false">
117117
<argument type="service" id="templating.form.engine" />
118-
<argument type="service" id="form.csrf_provider" on-invalid="null" />
118+
<argument type="service" id="security.csrf.token_manager" on-invalid="null" />
119119
</service>
120120

121121
<service id="templating.globals" class="%templating.globals.class%">

src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<services>
5353
<service id="security.authentication.listener.anonymous" class="%security.authentication.listener.anonymous.class%" public="false">
5454
<tag name="monolog.logger" channel="security" />
55-
<argument type="service" id="security.context" />
55+
<argument type="service" id="security.token_storage" />
5656
<argument /> <!-- Key -->
5757
<argument type="service" id="logger" on-invalid="null" />
5858
<argument type="service" id="security.authentication.manager" />
@@ -82,15 +82,15 @@
8282

8383
<service id="security.context_listener" class="%security.context_listener.class%" public="false">
8484
<tag name="monolog.logger" channel="security" />
85-
<argument type="service" id="security.context" />
85+
<argument type="service" id="security.token_storage" />
8686
<argument type="collection"></argument>
8787
<argument /> <!-- Provider Key -->
8888
<argument type="service" id="logger" on-invalid="null" />
8989
<argument type="service" id="event_dispatcher" on-invalid="null"/>
9090
</service>
9191

9292
<service id="security.logout_listener" class="%security.logout_listener.class%" public="false" abstract="true">
93-
<argument type="service" id="security.context" />
93+
<argument type="service" id="security.token_storage" />
9494
<argument type="service" id="security.http_utils" />
9595
<argument type="service" id="security.logout.success_handler" />
9696
<argument /> <!-- Options -->
@@ -109,7 +109,7 @@
109109

110110
<service id="security.authentication.listener.abstract" abstract="true" public="false">
111111
<tag name="monolog.logger" channel="security" />
112-
<argument type="service" id="security.context" />
112+
<argument type="service" id="security.token_storage" />
113113
<argument type="service" id="security.authentication.manager" />
114114
<argument type="service" id="security.authentication.session_strategy" />
115115
<argument type="service" id="security.http_utils" />
@@ -167,7 +167,7 @@
167167

168168
<service id="security.authentication.listener.simple_preauth" class="%security.authentication.listener.simple_preauth.class%" public="false" abstract="true">
169169
<tag name="monolog.logger" channel="security" />
170-
<argument type="service" id="security.context" />
170+
<argument type="service" id="security.token_storage" />
171171
<argument type="service" id="security.authentication.manager" />
172172
<argument /> <!-- Provider-shared Key -->
173173
<argument /> <!-- Authenticator -->
@@ -177,7 +177,7 @@
177177

178178
<service id="security.authentication.listener.x509" class="%security.authentication.listener.x509.class%" public="false" abstract="true">
179179
<tag name="monolog.logger" channel="security" />
180-
<argument type="service" id="security.context" />
180+
<argument type="service" id="security.token_storage" />
181181
<argument type="service" id="security.authentication.manager" />
182182
<argument /> <!-- Provider-shared Key -->
183183
<argument /> <!-- x509 user -->
@@ -188,7 +188,7 @@
188188

189189
<service id="security.authentication.listener.remote_user" class="Symfony\Component\Security\Http\Firewall\RemoteUserAuthenticationListener" public="false" abstract="true">
190190
<tag name="monolog.logger" channel="security" />
191-
<argument type="service" id="security.context" />
191+
<argument type="service" id="security.token_storage" />
192192
<argument type="service" id="security.authentication.manager" />
193193
<argument /> <!-- Provider-shared Key -->
194194
<argument /> <!-- REMOTE_USER server env var -->
@@ -198,7 +198,7 @@
198198

199199
<service id="security.authentication.listener.basic" class="%security.authentication.listener.basic.class%" public="false" abstract="true">
200200
<tag name="monolog.logger" channel="security" />
201-
<argument type="service" id="security.context" />
201+
<argument type="service" id="security.token_storage" />
202202
<argument type="service" id="security.authentication.manager" />
203203
<argument /> <!-- Provider-shared Key -->
204204
<argument /> <!-- Entry Point -->
@@ -207,7 +207,7 @@
207207

208208
<service id="security.authentication.listener.digest" class="%security.authentication.listener.digest.class%" public="false" abstract="true">
209209
<tag name="monolog.logger" channel="security" />
210-
<argument type="service" id="security.context" />
210+
<argument type="service" id="security.token_storage" />
211211
<argument /> <!-- User Provider -->
212212
<argument /> <!-- Provider-shared Key -->
213213
<argument /> <!-- Entry Point -->
@@ -235,7 +235,7 @@
235235

236236
<service id="security.exception_listener" class="%security.exception_listener.class%" public="false" abstract="true">
237237
<tag name="monolog.logger" channel="security" />
238-
<argument type="service" id="security.context" />
238+
<argument type="service" id="security.token_storage" />
239239
<argument type="service" id="security.authentication.trust_resolver" />
240240
<argument type="service" id="security.http_utils" />
241241
<argument />
@@ -247,7 +247,7 @@
247247

248248
<service id="security.authentication.switchuser_listener" class="%security.authentication.switchuser_listener.class%" public="false" abstract="true">
249249
<tag name="monolog.logger" channel="security" />
250-
<argument type="service" id="security.context" />
250+
<argument type="service" id="security.token_storage" />
251251
<argument /> <!-- User Provider -->
252252
<argument type="service" id="security.user_checker" />
253253
<argument /> <!-- Provider Key -->
@@ -260,7 +260,7 @@
260260

261261
<service id="security.access_listener" class="%security.access_listener.class%" public="false">
262262
<tag name="monolog.logger" channel="security" />
263-
<argument type="service" id="security.context" />
263+
<argument type="service" id="security.token_storage" />
264264
<argument type="service" id="security.access.decision_manager" />
265265
<argument type="service" id="security.access_map" />
266266
<argument type="service" id="security.authentication.manager" />

src/Symfony/Bundle/SecurityBundle/Resources/config/security_rememberme.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<services>
2020
<service id="security.authentication.listener.rememberme" class="%security.authentication.listener.rememberme.class%" public="false" abstract="true">
2121
<tag name="monolog.logger" channel="security" />
22-
<argument type="service" id="security.context" />
22+
<argument type="service" id="security.token_storage" />
2323
<argument type="service" id="security.authentication.rememberme" />
2424
<argument type="service" id="security.authentication.manager" />
2525
<argument type="service" id="logger" on-invalid="null" />

src/Symfony/Bundle/SecurityBundle/Resources/config/templating_php.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<service id="templating.helper.security" class="%templating.helper.security.class%">
2020
<tag name="templating.helper" alias="security" />
21-
<argument type="service" id="security.context" on-invalid="ignore" />
21+
<argument type="service" id="security.authorization_checker" on-invalid="ignore" />
2222
</service>
2323
</services>
2424
</container>

src/Symfony/Bundle/SecurityBundle/Resources/config/templating_twig.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<service id="twig.extension.security" class="%twig.extension.security.class%" public="false">
1919
<tag name="twig.extension" />
20-
<argument type="service" id="security.context" on-invalid="ignore" />
20+
<argument type="service" id="security.authorization_checker" on-invalid="ignore" />
2121
</service>
2222
</services>
2323
</container>

src/Symfony/Bundle/SecurityBundle/Templating/Helper/SecurityHelper.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,38 @@
1414
use Symfony\Component\Security\Acl\Voter\FieldVote;
1515
use Symfony\Component\Templating\Helper\Helper;
1616
use Symfony\Component\Security\Core\SecurityContextInterface;
17+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1718

1819
/**
19-
* SecurityHelper provides read-only access to the security context.
20+
* SecurityHelper provides read-only access to the security checker.
2021
*
2122
* @author Fabien Potencier <fabien@symfony.com>
2223
*/
2324
class SecurityHelper extends Helper
2425
{
25-
private $context;
26+
private $securityChecker;
2627

2728
/**
28-
* Constructor.
29+
* @param SecurityContextInterface|AuthorizationCheckerInterface
2930
*
30-
* @param SecurityContextInterface $context A SecurityContext instance
31+
* Passing a SecurityContextInterface as a first argument was deprecated in 2.7 and will be removed in 3.0
3132
*/
32-
public function __construct(SecurityContextInterface $context = null)
33+
public function __construct($securityChecker = null)
3334
{
34-
$this->context = $context;
35+
$this->securityChecker = $securityChecker;
3536
}
3637

3738
public function isGranted($role, $object = null, $field = null)
3839
{
39-
if (null === $this->context) {
40+
if (null === $this->securityChecker) {
4041
return false;
4142
}
4243

4344
if (null !== $field) {
4445
$object = new FieldVote($object, $field);
4546
}
4647

47-
return $this->context->isGranted($role, $object);
48+
return $this->securityChecker->isGranted($role, $object);
4849
}
4950

5051
/**

src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function testCollectWhenSecurityIsDisabled()
2525
$this->assertEmpty($collector->getUser());
2626
}
2727

28-
/** @dataProvider provideTokenStorage */
29-
public function testCollectWhenAuthenticationTokenIsNull($tokenStorage)
28+
public function testCollectWhenAuthenticationTokenIsNull()
3029
{
30+
$tokenStorage = new TokenStorage();
3131
$collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
3232
$collector->collect($this->getRequest(), $this->getResponse());
3333

@@ -40,12 +40,21 @@ public function testCollectWhenAuthenticationTokenIsNull($tokenStorage)
4040
$this->assertEmpty($collector->getUser());
4141
}
4242

43-
public function provideTokenStorage()
43+
public function testLegacyCollectWhenAuthenticationTokenIsNull()
4444
{
45-
return array(
46-
array(new TokenStorage()),
47-
array($this->getMock('Symfony\Component\Security\Core\SecurityContextInterface')),
48-
);
45+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
46+
47+
$tokenStorage = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
48+
$collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
49+
$collector->collect($this->getRequest(), $this->getResponse());
50+
51+
$this->assertTrue($collector->isEnabled());
52+
$this->assertFalse($collector->isAuthenticated());
53+
$this->assertNull($collector->getTokenClass());
54+
$this->assertTrue($collector->supportsRoleHierarchy());
55+
$this->assertCount(0, $collector->getRoles());
56+
$this->assertCount(0, $collector->getInheritedRoles());
57+
$this->assertEmpty($collector->getUser());
4958
}
5059

5160
/** @dataProvider provideRoles */

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
1313

14-
use Symfony\Component\Security\Core\SecurityContext;
14+
use Symfony\Component\Security\Core\Security;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\DependencyInjection\ContainerAware;
@@ -21,15 +21,15 @@ class LocalizedController extends ContainerAware
2121
public function loginAction(Request $request)
2222
{
2323
// get the login error if there is one
24-
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
25-
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
24+
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
25+
$error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
2626
} else {
27-
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
27+
$error = $request->getSession()->get(Security::AUTHENTICATION_ERROR);
2828
}
2929

3030
return $this->container->get('templating')->renderResponse('FormLoginBundle:Localized:login.html.twig', array(
3131
// last username entered by the user
32-
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
32+
'last_username' => $request->getSession()->get(Security::LAST_USERNAME),
3333
'error' => $error,
3434
));
3535
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LoginController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17-
use Symfony\Component\Security\Core\SecurityContext;
17+
use Symfony\Component\Security\Core\Security;
1818
use Symfony\Component\DependencyInjection\ContainerAware;
1919

2020
class LoginController extends ContainerAware
2121
{
2222
public function loginAction(Request $request)
2323
{
2424
// get the login error if there is one
25-
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
26-
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
25+
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
26+
$error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
2727
} else {
28-
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
28+
$error = $request->getSession()->get(Security::AUTHENTICATION_ERROR);
2929
}
3030

3131
return $this->container->get('templating')->renderResponse('FormLoginBundle:Login:login.html.twig', array(
3232
// last username entered by the user
33-
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
33+
'last_username' => $request->getSession()->get(Security::LAST_USERNAME),
3434
'error' => $error,
3535
));
3636
}

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=5.3.3",
20-
"symfony/security": "~2.6|~3.0.0",
20+
"symfony/security": "~2.7|~3.0.0",
2121
"symfony/http-kernel": "~2.2|~3.0.0"
2222
},
2323
"require-dev": {

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114

115115
<service id="twig.form.renderer" class="%twig.form.renderer.class%" public="false">
116116
<argument type="service" id="twig.form.engine" />
117-
<argument type="service" id="form.csrf_provider" on-invalid="null" />
117+
<argument type="service" id="security.csrf.token_manager" on-invalid="null" />
118118
</service>
119119

120120
<service id="twig.translation.extractor" class="%twig.translation.extractor.class%">

src/Symfony/Component/Form/Extension/Csrf/CsrfProvider/CsrfTokenManagerAdapter.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Form\Extension\Csrf\CsrfProvider;
1313

14-
trigger_error('The '.__NAMESPACE__.'\CsrfTokenManagerAdapter is deprecated since version 2.4 and will be removed in version 3.0. Use the Symfony\Component\Security\Csrf\CsrfTokenManager class instead.', E_USER_DEPRECATED);
15-
1614
use Symfony\Component\Security\Csrf\CsrfToken;
1715
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
1816

@@ -36,8 +34,12 @@ public function __construct(CsrfTokenManagerInterface $tokenManager)
3634
$this->tokenManager = $tokenManager;
3735
}
3836

39-
public function getTokenManager()
37+
public function getTokenManager($triggerDeprecationError = true)
4038
{
39+
if ($triggerDeprecationError) {
40+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in version 3.0. Use the Symfony\Component\Security\Csrf\CsrfTokenManager class instead.', E_USER_DEPRECATED);
41+
}
42+
4143
return $this->tokenManager;
4244
}
4345

@@ -46,6 +48,8 @@ public function getTokenManager()
4648
*/
4749
public function generateCsrfToken($intention)
4850
{
51+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in version 3.0. Use the Symfony\Component\Security\Csrf\CsrfTokenManager class instead.', E_USER_DEPRECATED);
52+
4953
return $this->tokenManager->getToken($intention)->getValue();
5054
}
5155

@@ -54,6 +58,8 @@ public function generateCsrfToken($intention)
5458
*/
5559
public function isCsrfTokenValid($intention, $token)
5660
{
61+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in version 3.0. Use the Symfony\Component\Security\Csrf\CsrfTokenManager class instead.', E_USER_DEPRECATED);
62+
5763
return $this->tokenManager->isTokenValid(new CsrfToken($intention, $token));
5864
}
5965
}

0 commit comments

Comments
 (0)
0