From 1bd696ea76adfef95a57d24f69ebe3afd11331c8 Mon Sep 17 00:00:00 2001 From: Maks Rafalko Date: Thu, 21 Nov 2013 19:47:59 +0300 Subject: [PATCH 1/4] Changed parameter container to request There is no need to pass @service_container to the voter, @request is enough. --- cookbook/security/voters.rst | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cookbook/security/voters.rst b/cookbook/security/voters.rst index 38e43cd5e0f..bea3d7f7c1b 100644 --- a/cookbook/security/voters.rst +++ b/cookbook/security/voters.rst @@ -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()) { - $this->container = $container; + $this->request = $request; $this->blacklistedIp = $blacklistedIp; } @@ -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; } @@ -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 } @@ -134,7 +133,7 @@ and tag it as a "security.voter": - + 123.123.123.123 171.171.171.171 @@ -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'), ), ); From 3c76be2a254b03a266b0178cdd7cac662bd5f4c2 Mon Sep 17 00:00:00 2001 From: Maks Rafalko Date: Thu, 21 Nov 2013 19:49:42 +0300 Subject: [PATCH 2/4] small typo --- cookbook/security/voters.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/security/voters.rst b/cookbook/security/voters.rst index bea3d7f7c1b..cd7037c49df 100644 --- a/cookbook/security/voters.rst +++ b/cookbook/security/voters.rst @@ -133,7 +133,7 @@ and tag it as a "security.voter": - + 123.123.123.123 171.171.171.171 From 2dd818050f8a42f8360d251207d830458f09c372 Mon Sep 17 00:00:00 2001 From: Maks Rafalko Date: Thu, 21 Nov 2013 20:01:15 +0300 Subject: [PATCH 3/4] Replaced @request with @request_stack --- cookbook/security/voters.rst | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cookbook/security/voters.rst b/cookbook/security/voters.rst index cd7037c49df..477bf03c956 100644 --- a/cookbook/security/voters.rst +++ b/cookbook/security/voters.rst @@ -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\HttpFoundation\Request; + 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(Request $request, array $blacklistedIp = array()) + public function __construct(RequestStack $requestStack, array $blacklistedIp = array()) { - $this->request = $request; + $this->requestStack = $requestStack; $this->blacklistedIp = $blacklistedIp; } @@ -87,7 +87,8 @@ and compare the IP address against a set of blacklisted IP addresses: public function vote(TokenInterface $token, $object, array $attributes) { - if (in_array($this->request->getClientIp(), $this->blacklistedIp)) { + $request = $requestStack->getCurrentRequest(); + if (in_array($request->getClientIp(), $this->blacklistedIp)) { return VoterInterface::ACCESS_DENIED; } @@ -123,7 +124,7 @@ and tag it as a "security.voter": services: security.access.blacklist_voter: class: Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter - arguments: ["@request", [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 } @@ -133,7 +134,7 @@ and tag it as a "security.voter": - + 123.123.123.123 171.171.171.171 @@ -150,7 +151,7 @@ and tag it as a "security.voter": $definition = new Definition( 'Acme\DemoBundle\Security\Authorization\Voter\ClientIpVoter', array( - new Reference('request'), + new Reference('request_stack'), array('123.123.123.123', '171.171.171.171'), ), ); From 64772f606878d2f65bd642f9df07235d67a148ec Mon Sep 17 00:00:00 2001 From: Maks Rafalko Date: Thu, 21 Nov 2013 20:05:51 +0300 Subject: [PATCH 4/4] added properties --- cookbook/security/voters.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cookbook/security/voters.rst b/cookbook/security/voters.rst index 477bf03c956..7b674e22105 100644 --- a/cookbook/security/voters.rst +++ b/cookbook/security/voters.rst @@ -67,6 +67,9 @@ and compare the IP address against a set of blacklisted IP addresses: class ClientIpVoter implements VoterInterface { + protected $requestStack; + protected $blacklistedIp; + public function __construct(RequestStack $requestStack, array $blacklistedIp = array()) { $this->requestStack = $requestStack;