Skip to content

Changed parameter container to request #3216

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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions cookbook/security/voters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ and compare the IP address against a set of blacklisted IP addresses:
// src/Acme/DemoBundle/Security/Authorization/Voter/ClientIpVoter.php
namespace Acme\DemoBundle\Security\Authorization\Voter;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class ClientIpVoter implements VoterInterface
{
public function __construct(ContainerInterface $container, array $blacklistedIp = array())
protected $requestStack;
protected $blacklistedIp;

public function __construct(RequestStack $requestStack, array $blacklistedIp = array())
{
$this->container = $container;
$this->requestStack = $requestStack;
$this->blacklistedIp = $blacklistedIp;
}

Expand All @@ -87,7 +90,7 @@ and compare the IP address against a set of blacklisted IP addresses:

public function vote(TokenInterface $token, $object, array $attributes)
{
$request = $this->container->get('request');
$request = $requestStack->getCurrentRequest();
if (in_array($request->getClientIp(), $this->blacklistedIp)) {
return VoterInterface::ACCESS_DENIED;
}
Expand Down Expand Up @@ -124,7 +127,7 @@ and tag it as a "security.voter":
services:
security.access.blacklist_voter:
class: Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter
arguments: ["@service_container", [123.123.123.123, 171.171.171.171]]
arguments: ["@request_stack", [123.123.123.123, 171.171.171.171]]
public: false
tags:
- { name: security.voter }
Expand All @@ -134,7 +137,7 @@ and tag it as a "security.voter":
<!-- src/Acme/AcmeBundle/Resources/config/services.xml -->
<service id="security.access.blacklist_voter"
class="Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter" public="false">
<argument type="service" id="service_container" strict="false" />
<argument type="service" id="request_stack" strict="false" />
<argument type="collection">
<argument>123.123.123.123</argument>
<argument>171.171.171.171</argument>
Expand All @@ -151,7 +154,7 @@ and tag it as a "security.voter":
$definition = new Definition(
'Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter',
array(
new Reference('service_container'),
new Reference('request_stack'),
array('123.123.123.123', '171.171.171.171'),
),
);
Expand Down