From ff16587acf96cfcfb0b9ea87087ccb067b4ec236 Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Tue, 12 Aug 2014 15:07:37 +0200 Subject: [PATCH] =?UTF-8?q?Apply=20firewall=20rules=20only=20in=20prod=20e?= =?UTF-8?q?nvironment=20=09modifi=C3=A9:=20=20=20=20=20=20=20=20=20core/li?= =?UTF-8?q?b/Thelia/Controller/BaseController.php=20=09modifi=C3=A9:=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20core/lib/Thelia/Form/FirewallForm.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lib/Thelia/Controller/BaseController.php | 4 +- core/lib/Thelia/Form/FirewallForm.php | 56 ++++++++++--------- core/lib/Thelia/Tests/Form/FirewallTest.php | 10 ++-- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index b039887c6..997fd5673 100644 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -201,7 +201,9 @@ abstract class BaseController extends ContainerAware $form->bind($aBaseForm->getRequest()); if ($form->isValid()) { - if ($aBaseForm instanceof FirewallForm && !$aBaseForm->isFirewallOk()) { + $env = $this->container->getParameter("kernel.environment"); + + if ($aBaseForm instanceof FirewallForm && !$aBaseForm->isFirewallOk($env)) { throw new FormValidationException( $this->getTranslator()->trans( "You've submitted this form too many times. Further submissions will be ignored during %time", diff --git a/core/lib/Thelia/Form/FirewallForm.php b/core/lib/Thelia/Form/FirewallForm.php index 085ac024b..198901bd2 100644 --- a/core/lib/Thelia/Form/FirewallForm.php +++ b/core/lib/Thelia/Form/FirewallForm.php @@ -32,41 +32,43 @@ abstract class FirewallForm extends BaseForm const DEFAULT_TIME_TO_WAIT = 60; // 1 hour const DEFAULT_ATTEMPTS = 6; - public function isFirewallOk() + public function isFirewallOk($env) { - /** - * Empty the firewall - */ - $deleteTime = date("Y-m-d G:i:s", time() - $this->getConfigTime() * 60 ); - $collection = FormFirewallQuery::create() - ->filterByFormName($this->getName()) - ->filterByUpdatedAt($deleteTime, Criteria::LESS_THAN) - ->find(); + if ($env === "prod") { + /** + * Empty the firewall + */ + $deleteTime = date("Y-m-d G:i:s", time() - $this->getConfigTime() * 60 ); + $collection = FormFirewallQuery::create() + ->filterByFormName($this->getName()) + ->filterByUpdatedAt($deleteTime, Criteria::LESS_THAN) + ->find(); - $collection->delete(); + $collection->delete(); - $firewallInstance = FormFirewallQuery::create() - ->filterByFormName($this->getName()) - ->filterByIpAddress($this->request->getClientIp()) - ->findOne() - ; + $firewallInstance = FormFirewallQuery::create() + ->filterByFormName($this->getName()) + ->filterByIpAddress($this->request->getClientIp()) + ->findOne() + ; - if ($this->isFirewallActive() && null !== $firewallInstance) { - if ($firewallInstance->getAttempts() < $this->getConfigAttempts()) { - $firewallInstance->incrementAttempts(); + if ($this->isFirewallActive() && null !== $firewallInstance) { + if ($firewallInstance->getAttempts() < $this->getConfigAttempts()) { + $firewallInstance->incrementAttempts(); + } else { + /** Set updated_at at NOW() */ + $firewallInstance->save(); + + return false; + } } else { - /** Set updated_at at NOW() */ + $firewallInstance = (new FormFirewall()) + ->setIpAddress($this->request->getClientIp()) + ->setFormName($this->getName()) + ; $firewallInstance->save(); - return false; } - } else { - $firewallInstance = (new FormFirewall()) - ->setIpAddress($this->request->getClientIp()) - ->setFormName($this->getName()) - ; - $firewallInstance->save(); - } return true; diff --git a/core/lib/Thelia/Tests/Form/FirewallTest.php b/core/lib/Thelia/Tests/Form/FirewallTest.php index 9b2b474fe..bdbe7f139 100644 --- a/core/lib/Thelia/Tests/Form/FirewallTest.php +++ b/core/lib/Thelia/Tests/Form/FirewallTest.php @@ -88,11 +88,11 @@ class FirewallTest extends \PHPUnit_Framework_TestCase if ($i > 6) { $this->assertFalse( - $this->form->isFirewallOk() + $this->form->isFirewallOk("prod") ); } else { $this->assertTrue( - $this->form->isFirewallOk() + $this->form->isFirewallOk("prod") ); } @@ -145,7 +145,7 @@ class FirewallTest extends \PHPUnit_Framework_TestCase public function testAutoDelete() { /** Add two rows */ - $this->form->isFirewallOk(); + $this->form->isFirewallOk("prod"); $this->form ->expects($this->any()) @@ -153,7 +153,7 @@ class FirewallTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue("test_form_firewall_2")) ; - $this->form->isFirewallOk(); + $this->form->isFirewallOk("prod"); /** Set the time to 1h and 1s after the limit */ FormFirewallQuery::create() @@ -162,7 +162,7 @@ class FirewallTest extends \PHPUnit_Framework_TestCase ->save() ; - $this->form->isFirewallOk(); + $this->form->isFirewallOk("prod"); /** Assert that the table is empty */ $this->assertEquals(