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 1 commit
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
15 changes: 7 additions & 8 deletions cookbook/security/voters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ 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\Request;
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())
public function __construct(Request $request, array $blacklistedIp = array())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be the request or the request_stack? 👶

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, indeed. For 2.3 this should be changed to use the request_stack

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it is for 2.4+ as mentioned in here.
I'll update PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, my mistake. Could you please reopen this PR for the 2.4 branch (master)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will do the same changes for 2.4 (master). But what about this PR (current branch) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pr should be closed, for the same reasons as mentioned in #3211

{
$this->container = $container;
$this->request = $request;
$this->blacklistedIp = $blacklistedIp;
}

Expand All @@ -87,8 +87,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');
if (in_array($request->getClientIp(), $this->blacklistedIp)) {
if (in_array($this->request->getClientIp(), $this->blacklistedIp)) {
return VoterInterface::ACCESS_DENIED;
}

Expand Down Expand Up @@ -124,7 +123,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", [123.123.123.123, 171.171.171.171]]
public: false
tags:
- { name: security.voter }
Expand All @@ -134,7 +133,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 strict="false" />
<argument type="collection">
<argument>123.123.123.123</argument>
<argument>171.171.171.171</argument>
Expand All @@ -151,7 +150,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'),
array('123.123.123.123', '171.171.171.171'),
),
);
Expand Down