diff --git a/core/lib/Thelia/Form/FirewallForm.php b/core/lib/Thelia/Form/FirewallForm.php index c0cbeea13..af6113464 100644 --- a/core/lib/Thelia/Form/FirewallForm.php +++ b/core/lib/Thelia/Form/FirewallForm.php @@ -10,6 +10,7 @@ /* file that was distributed with this source code. */ /*************************************************************************************/ namespace Thelia\Form; +use Propel\Runtime\ActiveQuery\Criteria; use Symfony\Component\HttpFoundation\Request; use Thelia\Core\Translation\Translator; use Thelia\Model\ConfigQuery; @@ -31,51 +32,39 @@ abstract class FirewallForm extends BaseForm const DEFAULT_TIME_TO_WAIT = 60; // 1 hour const DEFAULT_ATTEMPTS = 6; - /** @var \Thelia\Model\FormFirewall */ - protected $firewallInstance; - - public function __construct(Request $request, $type = "form", $data = array(), $options = array()) - { - $this->firewallInstance = FormFirewallQuery::create() - ->filterByFormName($this->getName()) - ->filterByIpAddress($request->getClientIp()) - ->findOne() - ; - parent::__construct($request, $type, $data, $options); - } - public function isFirewallOk() { + /** + * 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 ($this->isFirewallActive() && null !== $firewallRow = &$this->firewallInstance) { - /** @var \DateTime $lastRequestDateTime */ - $lastRequestDateTime = $firewallRow->getUpdatedAt(); + $collection->delete(); - $lastRequestTimestamp = $lastRequestDateTime->getTimestamp(); + $firewallInstance = FormFirewallQuery::create() + ->filterByFormName($this->getName()) + ->filterByIpAddress($this->request->getClientIp()) + ->findOne() + ; - /** - * Get the last request execution time in hour. - */ - $lastRequest = (time() - $lastRequestTimestamp) / 60; - - if ($lastRequest > $this->getConfigTime()) { - $firewallRow->resetAttempts(); - } - - if ($firewallRow->getAttempts() < $this->getConfigAttempts()) { - $firewallRow->incrementAttempts(); + if ($this->isFirewallActive() && null !== $firewallInstance) { + if ($firewallInstance->getAttempts() < $this->getConfigAttempts()) { + $firewallInstance->incrementAttempts(); } else { /** Set updated_at at NOW() */ - $firewallRow->save(); - + $firewallInstance->save(); return false; } } else { - $this->firewallInstance = $firewallRow = (new FormFirewall()) + $this->firewallInstance = $firewallInstance = (new FormFirewall()) ->setIpAddress($this->request->getClientIp()) ->setFormName($this->getName()) ; - $firewallRow->save(); + $firewallInstance->save(); } diff --git a/core/lib/Thelia/Tests/Form/FirewallTest.php b/core/lib/Thelia/Tests/Form/FirewallTest.php index eb6e2dd07..9b2b474fe 100644 --- a/core/lib/Thelia/Tests/Form/FirewallTest.php +++ b/core/lib/Thelia/Tests/Form/FirewallTest.php @@ -12,7 +12,6 @@ namespace Thelia\Tests\Form; use Symfony\Component\DependencyInjection\Container; -use Thelia\Core\HttpFoundation\Request; use Thelia\Core\HttpFoundation\Session\Session; use Thelia\Core\Translation\Translator; use Thelia\Model\ConfigQuery; @@ -38,8 +37,18 @@ class FirewallTest extends \PHPUnit_Framework_TestCase new Translator(new Container()); - $this->request = new Request(); - $this->request->setSession($session); + $this->request = $this->getMock("\Thelia\Core\HttpFoundation\Request"); + $this->request + ->expects($this->any()) + ->method("getClientIp") + ->willReturn("127.0.0.1") + ; + + $this->request + ->expects($this->any()) + ->method("getSession") + ->willReturn($session) + ; /** * Get an example form. We @@ -132,4 +141,33 @@ class FirewallTest extends \PHPUnit_Framework_TestCase $this->form->getWaitingTime() ); } + + public function testAutoDelete() + { + /** Add two rows */ + $this->form->isFirewallOk(); + + $this->form + ->expects($this->any()) + ->method('getName') + ->will($this->returnValue("test_form_firewall_2")) + ; + + $this->form->isFirewallOk(); + + /** Set the time to 1h and 1s after the limit */ + FormFirewallQuery::create() + ->findOne() + ->setUpdatedAt(date("Y-m-d G:i:s", time() - 3601)) + ->save() + ; + + $this->form->isFirewallOk(); + + /** Assert that the table is empty */ + $this->assertEquals( + 1, + FormFirewallQuery::create()->count() + ); + } }