@@ -61,15 +61,18 @@ and compare the IP address against a set of blacklisted IP addresses:
61
61
// src/Acme/DemoBundle/Security/Authorization/Voter/ClientIpVoter.php
62
62
namespace Acme\DemoBundle\Security\Authorization\Voter;
63
63
64
- use Symfony\Component\DependencyInjection\ContainerInterface ;
64
+ use Symfony\Component\HttpFoundation\RequestStack ;
65
65
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
66
66
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
67
67
68
68
class ClientIpVoter implements VoterInterface
69
69
{
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())
71
74
{
72
- $this->container = $container ;
75
+ $this->requestStack = $requestStack ;
73
76
$this->blacklistedIp = $blacklistedIp;
74
77
}
75
78
@@ -87,7 +90,7 @@ and compare the IP address against a set of blacklisted IP addresses:
87
90
88
91
public function vote(TokenInterface $token, $object, array $attributes)
89
92
{
90
- $request = $this->container->get('request' );
93
+ $request = $requestStack->getCurrentRequest( );
91
94
if (in_array($request->getClientIp(), $this->blacklistedIp)) {
92
95
return VoterInterface::ACCESS_DENIED;
93
96
}
@@ -124,7 +127,7 @@ and tag it as a "security.voter":
124
127
services :
125
128
security.access.blacklist_voter :
126
129
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]]
128
131
public : false
129
132
tags :
130
133
- { name: security.voter }
@@ -134,7 +137,7 @@ and tag it as a "security.voter":
134
137
<!-- src/Acme/AcmeBundle/Resources/config/services.xml -->
135
138
<service id =" security.access.blacklist_voter"
136
139
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" />
138
141
<argument type =" collection" >
139
142
<argument >123.123.123.123</argument >
140
143
<argument >171.171.171.171</argument >
@@ -151,7 +154,7 @@ and tag it as a "security.voter":
151
154
$definition = new Definition(
152
155
'Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter',
153
156
array(
154
- new Reference('service_container '),
157
+ new Reference('request_stack '),
155
158
array('123.123.123.123', '171.171.171.171'),
156
159
),
157
160
);
0 commit comments