diff --git a/src/Abuse/Adapters/Database.php b/src/Abuse/Adapters/Database/TimeLimit.php similarity index 86% rename from src/Abuse/Adapters/Database.php rename to src/Abuse/Adapters/Database/TimeLimit.php index 534ee49..0d40829 100644 --- a/src/Abuse/Adapters/Database.php +++ b/src/Abuse/Adapters/Database/TimeLimit.php @@ -1,7 +1,8 @@ db->createCollection(Database::COLLECTION, $attributes, $indexes); + $this->db->createCollection(TimeLimit::COLLECTION, $attributes, $indexes); } catch (Duplicate) { // Collection already exists } @@ -128,7 +129,7 @@ protected function count(string $key, string $datetime): int /** @var array $result */ $result = Authorization::skip(function () use ($key, $datetime) { - return $this->db->find(Database::COLLECTION, [ + return $this->db->find(TimeLimit::COLLECTION, [ Query::equal('key', [$key]), Query::equal('time', [$datetime]), ]); @@ -160,7 +161,7 @@ protected function hit(string $key, string $datetime): void } Authorization::skip(function () use ($datetime, $key) { - $data = $this->db->findOne(Database::COLLECTION, [ + $data = $this->db->findOne(TimeLimit::COLLECTION, [ Query::equal('key', [$key]), Query::equal('time', [$datetime]), ]); @@ -171,14 +172,14 @@ protected function hit(string $key, string $datetime): void 'key' => $key, 'time' => $datetime, 'count' => 1, - '$collection' => Database::COLLECTION, + '$collection' => TimeLimit::COLLECTION, ]; try { - $this->db->createDocument(Database::COLLECTION, new Document($data)); + $this->db->createDocument(TimeLimit::COLLECTION, new Document($data)); } catch (Duplicate $e) { // Duplicate in case of race condition - $data = $this->db->findOne(Database::COLLECTION, [ + $data = $this->db->findOne(TimeLimit::COLLECTION, [ Query::equal('key', [$key]), Query::equal('time', [$datetime]), ]); @@ -188,14 +189,14 @@ protected function hit(string $key, string $datetime): void if (\is_numeric($count)) { $this->count = intval($count); } - $this->db->increaseDocumentAttribute(Database::COLLECTION, $data->getId(), 'count'); + $this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count'); } else { throw new \Exception('Document Not Found'); } } } else { /** @var Document $data */ - $this->db->increaseDocumentAttribute(Database::COLLECTION, $data->getId(), 'count'); + $this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count'); } }); @@ -227,7 +228,7 @@ public function getLogs(?int $offset = null, ?int $limit = 25): array $queries[] = Query::limit($limit); } - return $this->db->find(Database::COLLECTION, $queries); + return $this->db->find(TimeLimit::COLLECTION, $queries); }); return $results; @@ -245,12 +246,12 @@ public function cleanup(string $datetime): bool { Authorization::skip(function () use ($datetime) { do { - $documents = $this->db->find(Database::COLLECTION, [ + $documents = $this->db->find(TimeLimit::COLLECTION, [ Query::lessThan('time', $datetime), ]); foreach ($documents as $document) { - $this->db->deleteDocument(Database::COLLECTION, $document->getId()); + $this->db->deleteDocument(TimeLimit::COLLECTION, $document->getId()); } } while (! empty($documents)); }); diff --git a/src/Abuse/Adapters/Redis.php b/src/Abuse/Adapters/Redis/TimeLimit.php similarity index 96% rename from src/Abuse/Adapters/Redis.php rename to src/Abuse/Adapters/Redis/TimeLimit.php index d9b0892..f234921 100644 --- a/src/Abuse/Adapters/Redis.php +++ b/src/Abuse/Adapters/Redis/TimeLimit.php @@ -1,10 +1,11 @@ setNamespace('namespace'); $this->db = $db; - $adapter = new AdaptersDatabase('login-attempt-from-{{ip}}', 3, 60 * 5, $db); + $adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $db); if (! $db->exists('utopiaTests')) { $db->create(); $adapter->setup(); diff --git a/tests/Abuse/Bench/RedisBench.php b/tests/Abuse/Bench/Redis/TimeLimitBench.php similarity index 56% rename from tests/Abuse/Bench/RedisBench.php rename to tests/Abuse/Bench/Redis/TimeLimitBench.php index b3ad310..0d8cdb7 100644 --- a/tests/Abuse/Bench/RedisBench.php +++ b/tests/Abuse/Bench/Redis/TimeLimitBench.php @@ -1,12 +1,13 @@ redis = new Client(); $this->redis->connect('redis', 6379); - $this->adapter = new Redis('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis); + $this->adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis); $this->abuse = new Abuse($this->adapter); } } diff --git a/tests/Abuse/DatabaseTest.php b/tests/Abuse/DatabaseTest.php index 5927d0f..b3820e6 100755 --- a/tests/Abuse/DatabaseTest.php +++ b/tests/Abuse/DatabaseTest.php @@ -5,7 +5,7 @@ use PDO; use Utopia\Abuse\Abuse; use Utopia\Abuse\Adapter; -use Utopia\Abuse\Adapters\Database as AdaptersDatabase; +use Utopia\Abuse\Adapters\Database\TimeLimit as AdaptersDatabase; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Cache; use Utopia\Database\Adapter\MariaDB; diff --git a/tests/Abuse/RedisTest.php b/tests/Abuse/RedisTest.php index b29b568..517ee94 100644 --- a/tests/Abuse/RedisTest.php +++ b/tests/Abuse/RedisTest.php @@ -3,10 +3,10 @@ namespace Utopia\Tests; use DateInterval; -use Utopia\Abuse\Adapter; -use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapters\Redis; use Redis as Client; +use Utopia\Abuse\Abuse; +use Utopia\Abuse\Adapter; +use Utopia\Abuse\Adapters\Redis\TimeLimit; use Utopia\Exception; class RedisTest extends Base @@ -21,14 +21,14 @@ public function setUp(): void { $this->redis = new Client(); $this->redis->connect('redis', 6379); - $adapter = new Redis('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis); + $adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis); $adapter->setParam('{{ip}}', '127.0.0.1'); $this->abuse = new Abuse($adapter); } public function getAdapter(string $key, int $limit, int $seconds): Adapter { - return new Redis($key, $limit, $seconds, $this->redis); + return new TimeLimit($key, $limit, $seconds, $this->redis); } public function getCleanupDateTime(): string