mirror of
https://github.com/getformwork/formwork.git
synced 2025-02-24 09:42:43 +01:00
Add AccessLimiter
class
This commit is contained in:
parent
06d086d51e
commit
421aa92386
54
admin/src/Security/AccessLimiter.php
Normal file
54
admin/src/Security/AccessLimiter.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Formwork\Admin\Security;
|
||||
|
||||
use Formwork\Admin\Utils\Registry;
|
||||
use Formwork\Utils\HTTPRequest;
|
||||
use Formwork\Utils\Uri;
|
||||
|
||||
class AccessLimiter
|
||||
{
|
||||
protected $registry;
|
||||
|
||||
protected $limit;
|
||||
|
||||
protected $resetTime;
|
||||
|
||||
protected $attemptHash;
|
||||
|
||||
protected $attempts;
|
||||
|
||||
protected $lastAttemptTime;
|
||||
|
||||
public function __construct(Registry $registry, $limit, $resetTime)
|
||||
{
|
||||
$this->registry = $registry;
|
||||
$this->limit = $limit;
|
||||
$this->resetTime = $resetTime;
|
||||
|
||||
// Hash visitor IP address followed by current host
|
||||
$this->attemptHash = sha1(HTTPRequest::ip() . '@' . Uri::host());
|
||||
|
||||
$this->attempts = $registry->get($this->attemptHash)[0];
|
||||
$this->lastAttemptTime = $registry->get($this->attemptHash)[1];
|
||||
}
|
||||
|
||||
public function hasReachedLimit()
|
||||
{
|
||||
if (time() - $this->lastAttemptTime > $this->resetTime) {
|
||||
$this->resetAttempts();
|
||||
}
|
||||
return $this->attempts > $this->limit;
|
||||
}
|
||||
|
||||
public function registerAttempt()
|
||||
{
|
||||
$this->registry->set($this->attemptHash, array(++$this->attempts, time()));
|
||||
}
|
||||
|
||||
public function resetAttempts()
|
||||
{
|
||||
$this->attempts = 0;
|
||||
$this->registry->remove($this->attemptHash);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user