Skip to content

Commit 820bb3e

Browse files
committed
Changed parameter container to requestStack (2.4+)
There is no need to pass @service_container to the voter, @request is enough. please see symfony#3216
1 parent 799b2c6 commit 820bb3e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

cookbook/security/voters.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ and compare the IP address against a set of blacklisted IP addresses:
6161
// src/Acme/DemoBundle/Security/Authorization/Voter/ClientIpVoter.php
6262
namespace Acme\DemoBundle\Security\Authorization\Voter;
6363
64-
use Symfony\Component\DependencyInjection\ContainerInterface;
64+
use Symfony\Component\HttpFoundation\RequestStack;
6565
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
6666
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6767
6868
class ClientIpVoter implements VoterInterface
6969
{
70-
public function __construct(ContainerInterface $container, array $blacklistedIp = array())
70+
protected $requestStack;
71+
protected $blacklistedIp;
72+
73+
public function __construct(RequestStack $requestStack, array $blacklistedIp = array())
7174
{
72-
$this->container = $container;
75+
$this->requestStack = $requestStack;
7376
$this->blacklistedIp = $blacklistedIp;
7477
}
7578
@@ -87,7 +90,7 @@ and compare the IP address against a set of blacklisted IP addresses:
8790
8891
public function vote(TokenInterface $token, $object, array $attributes)
8992
{
90-
$request = $this->container->get('request');
93+
$request = $requestStack->getCurrentRequest();
9194
if (in_array($request->getClientIp(), $this->blacklistedIp)) {
9295
return VoterInterface::ACCESS_DENIED;
9396
}
@@ -124,7 +127,7 @@ and tag it as a "security.voter":
124127
services:
125128
security.access.blacklist_voter:
126129
class: Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter
127-
arguments: ["@service_container", [123.123.123.123, 171.171.171.171]]
130+
arguments: ["@request_stack", [123.123.123.123, 171.171.171.171]]
128131
public: false
129132
tags:
130133
- { name: security.voter }
@@ -134,7 +137,7 @@ and tag it as a "security.voter":
134137
<!-- src/Acme/AcmeBundle/Resources/config/services.xml -->
135138
<service id="security.access.blacklist_voter"
136139
class="Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter" public="false">
137-
<argument type="service" id="service_container" strict="false" />
140+
<argument type="service" id="request_stack" strict="false" />
138141
<argument type="collection">
139142
<argument>123.123.123.123</argument>
140143
<argument>171.171.171.171</argument>
@@ -151,7 +154,7 @@ and tag it as a "security.voter":
151154
$definition = new Definition(
152155
'Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter',
153156
array(
154-
new Reference('service_container'),
157+
new Reference('request_stack'),
155158
array('123.123.123.123', '171.171.171.171'),
156159
),
157160
);

0 commit comments

Comments
 (0)