Add auto delete to keep the table small
modifié: core/lib/Thelia/Form/FirewallForm.php modifié: core/lib/Thelia/Tests/Form/FirewallTest.php
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
/* file that was distributed with this source code. */
|
/* file that was distributed with this source code. */
|
||||||
/*************************************************************************************/
|
/*************************************************************************************/
|
||||||
namespace Thelia\Form;
|
namespace Thelia\Form;
|
||||||
|
use Propel\Runtime\ActiveQuery\Criteria;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Thelia\Core\Translation\Translator;
|
use Thelia\Core\Translation\Translator;
|
||||||
use Thelia\Model\ConfigQuery;
|
use Thelia\Model\ConfigQuery;
|
||||||
@@ -31,51 +32,39 @@ abstract class FirewallForm extends BaseForm
|
|||||||
const DEFAULT_TIME_TO_WAIT = 60; // 1 hour
|
const DEFAULT_TIME_TO_WAIT = 60; // 1 hour
|
||||||
const DEFAULT_ATTEMPTS = 6;
|
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()
|
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) {
|
$collection->delete();
|
||||||
/** @var \DateTime $lastRequestDateTime */
|
|
||||||
$lastRequestDateTime = $firewallRow->getUpdatedAt();
|
|
||||||
|
|
||||||
$lastRequestTimestamp = $lastRequestDateTime->getTimestamp();
|
$firewallInstance = FormFirewallQuery::create()
|
||||||
|
->filterByFormName($this->getName())
|
||||||
|
->filterByIpAddress($this->request->getClientIp())
|
||||||
|
->findOne()
|
||||||
|
;
|
||||||
|
|
||||||
/**
|
if ($this->isFirewallActive() && null !== $firewallInstance) {
|
||||||
* Get the last request execution time in hour.
|
if ($firewallInstance->getAttempts() < $this->getConfigAttempts()) {
|
||||||
*/
|
$firewallInstance->incrementAttempts();
|
||||||
$lastRequest = (time() - $lastRequestTimestamp) / 60;
|
|
||||||
|
|
||||||
if ($lastRequest > $this->getConfigTime()) {
|
|
||||||
$firewallRow->resetAttempts();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($firewallRow->getAttempts() < $this->getConfigAttempts()) {
|
|
||||||
$firewallRow->incrementAttempts();
|
|
||||||
} else {
|
} else {
|
||||||
/** Set updated_at at NOW() */
|
/** Set updated_at at NOW() */
|
||||||
$firewallRow->save();
|
$firewallInstance->save();
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$this->firewallInstance = $firewallRow = (new FormFirewall())
|
$this->firewallInstance = $firewallInstance = (new FormFirewall())
|
||||||
->setIpAddress($this->request->getClientIp())
|
->setIpAddress($this->request->getClientIp())
|
||||||
->setFormName($this->getName())
|
->setFormName($this->getName())
|
||||||
;
|
;
|
||||||
$firewallRow->save();
|
$firewallInstance->save();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
|
|
||||||
namespace Thelia\Tests\Form;
|
namespace Thelia\Tests\Form;
|
||||||
use Symfony\Component\DependencyInjection\Container;
|
use Symfony\Component\DependencyInjection\Container;
|
||||||
use Thelia\Core\HttpFoundation\Request;
|
|
||||||
use Thelia\Core\HttpFoundation\Session\Session;
|
use Thelia\Core\HttpFoundation\Session\Session;
|
||||||
use Thelia\Core\Translation\Translator;
|
use Thelia\Core\Translation\Translator;
|
||||||
use Thelia\Model\ConfigQuery;
|
use Thelia\Model\ConfigQuery;
|
||||||
@@ -38,8 +37,18 @@ class FirewallTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
new Translator(new Container());
|
new Translator(new Container());
|
||||||
|
|
||||||
$this->request = new Request();
|
$this->request = $this->getMock("\Thelia\Core\HttpFoundation\Request");
|
||||||
$this->request->setSession($session);
|
$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
|
* Get an example form. We
|
||||||
@@ -132,4 +141,33 @@ class FirewallTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->form->getWaitingTime()
|
$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()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user