Skip to content

Commit 322fddb

Browse files
committed
[HttpFoundation] deprecated finding deep items in Request and ParameterBag
1 parent 00dffe7 commit 322fddb

13 files changed

+149
-18
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.8.0
5+
-----
6+
7+
* Finding deep items in `ParameterBag::get()` is deprecated since version 2.8 and
8+
will be removed in 3.0.
9+
410
2.6.0
511
-----
612

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function add(array $parameters = array())
7878
/**
7979
* Returns a parameter by name.
8080
*
81+
* Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
82+
*
8183
* @param string $path The key
8284
* @param mixed $default The default value if the parameter key does not exist
8385
* @param bool $deep If true, a path like foo[bar] will find deeper items
@@ -88,6 +90,10 @@ public function add(array $parameters = array())
8890
*/
8991
public function get($path, $default = null, $deep = false)
9092
{
93+
if (true === $deep) {
94+
@trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
95+
}
96+
9197
if (!$deep || false === $pos = strpos($path, '[')) {
9298
return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
9399
}

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ public static function getHttpMethodParameterOverride()
714714
* It is better to explicitly get request parameters from the appropriate
715715
* public property instead (query, attributes, request).
716716
*
717+
* Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
718+
*
717719
* @param string $key the key
718720
* @param mixed $default the default value
719721
* @param bool $deep is parameter deep in multidimensional array
@@ -722,6 +724,10 @@ public static function getHttpMethodParameterOverride()
722724
*/
723725
public function get($key, $default = null, $deep = false)
724726
{
727+
if (true === $deep) {
728+
@trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
729+
}
730+
725731
if ($this !== $result = $this->query->get($key, $this, $deep)) {
726732
return $result;
727733
}

src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function testGetDoesNotUseDeepByDefault()
8686
}
8787

8888
/**
89+
* @group legacy
8990
* @dataProvider getInvalidPaths
9091
* @expectedException \InvalidArgumentException
9192
*/
@@ -106,6 +107,9 @@ public function getInvalidPaths()
106107
);
107108
}
108109

110+
/**
111+
* @group legacy
112+
*/
109113
public function testGetDeep()
110114
{
111115
$bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1818
use Symfony\Component\Security\Core\Security;
1919
use Symfony\Component\Security\Http\HttpUtils;
20+
use Symfony\Component\Security\Http\ParameterBagUtils;
2021

2122
/**
2223
* Class with the default authentication failure handling logic.
@@ -82,7 +83,7 @@ public function setOptions(array $options)
8283
*/
8384
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
8485
{
85-
if ($failureUrl = $request->get($this->options['failure_path_parameter'], null, true)) {
86+
if ($failureUrl = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['failure_path_parameter'])) {
8687
$this->options['failure_path'] = $failureUrl;
8788
}
8889

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php

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

1212
namespace Symfony\Component\Security\Http\Authentication;
1313

14+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1415
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\Security\Http\HttpUtils;
@@ -108,7 +109,7 @@ protected function determineTargetUrl(Request $request)
108109
return $this->options['default_target_path'];
109110
}
110111

111-
if ($targetUrl = $request->get($this->options['target_path_parameter'], null, true)) {
112+
if ($targetUrl = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['target_path_parameter'])) {
112113
return $targetUrl;
113114
}
114115

src/Symfony/Component/Security/Http/Firewall/LogoutListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
1515
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
16+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
@@ -98,7 +99,7 @@ public function handle(GetResponseEvent $event)
9899
}
99100

100101
if (null !== $this->csrfTokenManager) {
101-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
102+
$csrfToken = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['csrf_parameter']);
102103

103104
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
104105
throw new LogoutException('Invalid CSRF token.');

src/Symfony/Component/Security/Http/Firewall/SimpleFormAuthenticationListener.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1515
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
1616
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
17+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
1920
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
@@ -101,19 +102,19 @@ protected function requiresAuthentication(Request $request)
101102
protected function attemptAuthentication(Request $request)
102103
{
103104
if (null !== $this->csrfTokenManager) {
104-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
105+
$csrfToken = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['csrf_parameter']);
105106

106107
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
107108
throw new InvalidCsrfTokenException('Invalid CSRF token.');
108109
}
109110
}
110111

111112
if ($this->options['post_only']) {
112-
$username = trim($request->request->get($this->options['username_parameter'], null, true));
113-
$password = $request->request->get($this->options['password_parameter'], null, true);
113+
$username = trim(ParameterBagUtils::getParameterWithPath($request->request, $this->options['username_parameter']));
114+
$password = ParameterBagUtils::getParameterWithPath($request->request, $this->options['password_parameter']);
114115
} else {
115-
$username = trim($request->get($this->options['username_parameter'], null, true));
116-
$password = $request->get($this->options['password_parameter'], null, true);
116+
$username = trim(ParameterBagUtils::getRequestParameterWithPath($request, $this->options['username_parameter']));
117+
$password = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['password_parameter']);
117118
}
118119

119120
$request->getSession()->set(Security::LAST_USERNAME, $username);

src/Symfony/Component/Security/Http/Firewall/UsernamePasswordFormAuthenticationListener.php

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

1414
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
1515
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
16+
use Symfony\Component\HttpFoundation\ParameterBagUtils;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Psr\Log\LoggerInterface;
1819
use Symfony\Component\Security\Csrf\CsrfToken;
@@ -76,19 +77,19 @@ protected function requiresAuthentication(Request $request)
7677
protected function attemptAuthentication(Request $request)
7778
{
7879
if (null !== $this->csrfTokenManager) {
79-
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
80+
$csrfToken = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['csrf_parameter']);
8081

8182
if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['intention'], $csrfToken))) {
8283
throw new InvalidCsrfTokenException('Invalid CSRF token.');
8384
}
8485
}
8586

8687
if ($this->options['post_only']) {
87-
$username = trim($request->request->get($this->options['username_parameter'], null, true));
88-
$password = $request->request->get($this->options['password_parameter'], null, true);
88+
$username = trim(ParameterBagUtils::getParameterWithPath($request->request, $this->options['username_parameter']));
89+
$password = ParameterBagUtils::getParameterWithPath($request->request, $this->options['password_parameter']);
8990
} else {
90-
$username = trim($request->get($this->options['username_parameter'], null, true));
91-
$password = $request->get($this->options['password_parameter'], null, true);
91+
$username = trim(ParameterBagUtils::getRequestParameterWithPath($request, $this->options['username_parameter']));
92+
$password = ParameterBagUtils::getRequestParameterWithPath($request, $this->options['password_parameter']);
9293
}
9394

9495
$request->getSession()->set(Security::LAST_USERNAME, $username);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
12+
namespace Symfony\Component\Security\Http;
13+
14+
use Symfony\Component\HttpFoundation\ParameterBag;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\PropertyAccess\PropertyAccess;
17+
18+
/**
19+
* @internal
20+
*
21+
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
22+
*/
23+
final class ParameterBagUtils
24+
{
25+
private static $propertyAccessor;
26+
27+
/**
28+
* Returns a "parameter" value.
29+
*
30+
* Paths like foo[bar] will be evaluated to find deeper items in nested data structures.
31+
*
32+
* @param ParameterBag $parameters The parameter bag
33+
* @param string $path The key
34+
* @param mixed $default The default value if the parameter key does not exist
35+
*
36+
* @return mixed
37+
*
38+
* @throws \InvalidArgumentException when the path is malformed
39+
*/
40+
public static function getParameterWithPath(ParameterBag $parameters, $path, $default = null)
41+
{
42+
if (false === $pos = strpos($path, '[')) {
43+
return $parameters->get($path, $default);
44+
}
45+
46+
$root = substr($path, 0, $pos);
47+
48+
if (null === $value = $parameters->get($root)) {
49+
return $default;
50+
}
51+
52+
if (null === self::$propertyAccessor) {
53+
self::$propertyAccessor = PropertyAccess::createPropertyAccessor();
54+
}
55+
56+
return self::$propertyAccessor->getValue($value, substr($path, $pos));
57+
}
58+
59+
/**
60+
* Returns a request "parameter" value.
61+
*
62+
* Paths like foo[bar] will be evaluated to find deeper items in nested data structures.
63+
*
64+
* @param Request $request The request
65+
* @param string $path The key
66+
*
67+
* @return mixed
68+
*
69+
* @throws \InvalidArgumentException when the path is malformed
70+
*/
71+
public static function getRequestParameterWithPath(Request $request, $path)
72+
{
73+
if (false === $pos = strpos($path, '[')) {
74+
return $request->get($path);
75+
}
76+
77+
$root = substr($path, 0, $pos);
78+
79+
if (null === $value = $request->get($root)) {
80+
return;
81+
}
82+
83+
if (null === self::$propertyAccessor) {
84+
self::$propertyAccessor = PropertyAccess::createPropertyAccessor();
85+
}
86+
87+
return self::$propertyAccessor->getValue($value, substr($path, $pos));
88+
}
89+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy