@@ -30,92 +30,92 @@ |
||
| 30 | 30 | use OCP\IDBConnection; |
| 31 | 31 | |
| 32 | 32 | class DatabaseBackend implements IBackend { |
| 33 | - private const TABLE_NAME = 'ratelimit_entries'; |
|
| 34 | - |
|
| 35 | - /** @var IDBConnection */ |
|
| 36 | - private $dbConnection; |
|
| 37 | - /** @var ITimeFactory */ |
|
| 38 | - private $timeFactory; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param IDBConnection $dbConnection |
|
| 42 | - * @param ITimeFactory $timeFactory |
|
| 43 | - */ |
|
| 44 | - public function __construct( |
|
| 45 | - IDBConnection $dbConnection, |
|
| 46 | - ITimeFactory $timeFactory |
|
| 47 | - ) { |
|
| 48 | - $this->dbConnection = $dbConnection; |
|
| 49 | - $this->timeFactory = $timeFactory; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param string $methodIdentifier |
|
| 54 | - * @param string $userIdentifier |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - private function hash(string $methodIdentifier, |
|
| 58 | - string $userIdentifier): string { |
|
| 59 | - return hash('sha512', $methodIdentifier . $userIdentifier); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param string $identifier |
|
| 64 | - * @param int $seconds |
|
| 65 | - * @return int |
|
| 66 | - * @throws \OCP\DB\Exception |
|
| 67 | - */ |
|
| 68 | - private function getExistingAttemptCount( |
|
| 69 | - string $identifier |
|
| 70 | - ): int { |
|
| 71 | - $currentTime = $this->timeFactory->getDateTime(); |
|
| 72 | - |
|
| 73 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 74 | - $qb->delete(self::TABLE_NAME) |
|
| 75 | - ->where( |
|
| 76 | - $qb->expr()->lte('delete_after', $qb->createNamedParameter($currentTime, IQueryBuilder::PARAM_DATE)) |
|
| 77 | - ) |
|
| 78 | - ->executeStatement(); |
|
| 79 | - |
|
| 80 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 81 | - $qb->select($qb->func()->count()) |
|
| 82 | - ->from(self::TABLE_NAME) |
|
| 83 | - ->where( |
|
| 84 | - $qb->expr()->eq('hash', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR)) |
|
| 85 | - ); |
|
| 86 | - |
|
| 87 | - $cursor = $qb->executeQuery(); |
|
| 88 | - $row = $cursor->fetchOne(); |
|
| 89 | - $cursor->closeCursor(); |
|
| 90 | - |
|
| 91 | - return (int)$row; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * {@inheritDoc} |
|
| 96 | - */ |
|
| 97 | - public function getAttempts(string $methodIdentifier, |
|
| 98 | - string $userIdentifier): int { |
|
| 99 | - $identifier = $this->hash($methodIdentifier, $userIdentifier); |
|
| 100 | - return $this->getExistingAttemptCount($identifier); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * {@inheritDoc} |
|
| 105 | - */ |
|
| 106 | - public function registerAttempt(string $methodIdentifier, |
|
| 107 | - string $userIdentifier, |
|
| 108 | - int $period) { |
|
| 109 | - $identifier = $this->hash($methodIdentifier, $userIdentifier); |
|
| 110 | - $deleteAfter = $this->timeFactory->getDateTime()->add(new \DateInterval("PT{$period}S")); |
|
| 111 | - |
|
| 112 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
| 113 | - |
|
| 114 | - $qb->insert(self::TABLE_NAME) |
|
| 115 | - ->values([ |
|
| 116 | - 'hash' => $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR), |
|
| 117 | - 'delete_after' => $qb->createNamedParameter($deleteAfter, IQueryBuilder::PARAM_DATE), |
|
| 118 | - ]) |
|
| 119 | - ->executeStatement(); |
|
| 120 | - } |
|
| 33 | + private const TABLE_NAME = 'ratelimit_entries'; |
|
| 34 | + |
|
| 35 | + /** @var IDBConnection */ |
|
| 36 | + private $dbConnection; |
|
| 37 | + /** @var ITimeFactory */ |
|
| 38 | + private $timeFactory; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param IDBConnection $dbConnection |
|
| 42 | + * @param ITimeFactory $timeFactory |
|
| 43 | + */ |
|
| 44 | + public function __construct( |
|
| 45 | + IDBConnection $dbConnection, |
|
| 46 | + ITimeFactory $timeFactory |
|
| 47 | + ) { |
|
| 48 | + $this->dbConnection = $dbConnection; |
|
| 49 | + $this->timeFactory = $timeFactory; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param string $methodIdentifier |
|
| 54 | + * @param string $userIdentifier |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + private function hash(string $methodIdentifier, |
|
| 58 | + string $userIdentifier): string { |
|
| 59 | + return hash('sha512', $methodIdentifier . $userIdentifier); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param string $identifier |
|
| 64 | + * @param int $seconds |
|
| 65 | + * @return int |
|
| 66 | + * @throws \OCP\DB\Exception |
|
| 67 | + */ |
|
| 68 | + private function getExistingAttemptCount( |
|
| 69 | + string $identifier |
|
| 70 | + ): int { |
|
| 71 | + $currentTime = $this->timeFactory->getDateTime(); |
|
| 72 | + |
|
| 73 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 74 | + $qb->delete(self::TABLE_NAME) |
|
| 75 | + ->where( |
|
| 76 | + $qb->expr()->lte('delete_after', $qb->createNamedParameter($currentTime, IQueryBuilder::PARAM_DATE)) |
|
| 77 | + ) |
|
| 78 | + ->executeStatement(); |
|
| 79 | + |
|
| 80 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 81 | + $qb->select($qb->func()->count()) |
|
| 82 | + ->from(self::TABLE_NAME) |
|
| 83 | + ->where( |
|
| 84 | + $qb->expr()->eq('hash', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR)) |
|
| 85 | + ); |
|
| 86 | + |
|
| 87 | + $cursor = $qb->executeQuery(); |
|
| 88 | + $row = $cursor->fetchOne(); |
|
| 89 | + $cursor->closeCursor(); |
|
| 90 | + |
|
| 91 | + return (int)$row; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * {@inheritDoc} |
|
| 96 | + */ |
|
| 97 | + public function getAttempts(string $methodIdentifier, |
|
| 98 | + string $userIdentifier): int { |
|
| 99 | + $identifier = $this->hash($methodIdentifier, $userIdentifier); |
|
| 100 | + return $this->getExistingAttemptCount($identifier); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * {@inheritDoc} |
|
| 105 | + */ |
|
| 106 | + public function registerAttempt(string $methodIdentifier, |
|
| 107 | + string $userIdentifier, |
|
| 108 | + int $period) { |
|
| 109 | + $identifier = $this->hash($methodIdentifier, $userIdentifier); |
|
| 110 | + $deleteAfter = $this->timeFactory->getDateTime()->add(new \DateInterval("PT{$period}S")); |
|
| 111 | + |
|
| 112 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
| 113 | + |
|
| 114 | + $qb->insert(self::TABLE_NAME) |
|
| 115 | + ->values([ |
|
| 116 | + 'hash' => $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR), |
|
| 117 | + 'delete_after' => $qb->createNamedParameter($deleteAfter, IQueryBuilder::PARAM_DATE), |
|
| 118 | + ]) |
|
| 119 | + ->executeStatement(); |
|
| 120 | + } |
|
| 121 | 121 | } |
@@ -30,37 +30,37 @@ |
||
| 30 | 30 | use OCP\Migration\SimpleMigrationStep; |
| 31 | 31 | |
| 32 | 32 | class Version24000Date20211213081604 extends SimpleMigrationStep { |
| 33 | - /** |
|
| 34 | - * @param IOutput $output |
|
| 35 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 36 | - * @param array $options |
|
| 37 | - * @return null|ISchemaWrapper |
|
| 38 | - */ |
|
| 39 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 40 | - /** @var ISchemaWrapper $schema */ |
|
| 41 | - $schema = $schemaClosure(); |
|
| 33 | + /** |
|
| 34 | + * @param IOutput $output |
|
| 35 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 36 | + * @param array $options |
|
| 37 | + * @return null|ISchemaWrapper |
|
| 38 | + */ |
|
| 39 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 40 | + /** @var ISchemaWrapper $schema */ |
|
| 41 | + $schema = $schemaClosure(); |
|
| 42 | 42 | |
| 43 | - $hasTable = $schema->hasTable('ratelimit_entries'); |
|
| 43 | + $hasTable = $schema->hasTable('ratelimit_entries'); |
|
| 44 | 44 | |
| 45 | - if (!$hasTable) { |
|
| 46 | - $table = $schema->createTable('ratelimit_entries'); |
|
| 47 | - $table->addColumn('id', Types::BIGINT, [ |
|
| 48 | - 'autoincrement' => true, |
|
| 49 | - 'notnull' => true, |
|
| 50 | - ]); |
|
| 51 | - $table->addColumn('hash', Types::STRING, [ |
|
| 52 | - 'notnull' => true, |
|
| 53 | - 'length' => 128, |
|
| 54 | - ]); |
|
| 55 | - $table->addColumn('delete_after', Types::DATETIME, [ |
|
| 56 | - 'notnull' => true, |
|
| 57 | - ]); |
|
| 58 | - $table->setPrimaryKey(['id']); |
|
| 59 | - $table->addIndex(['hash'], 'ratelimit_hash'); |
|
| 60 | - $table->addIndex(['delete_after'], 'ratelimit_delete_after'); |
|
| 61 | - return $schema; |
|
| 62 | - } |
|
| 45 | + if (!$hasTable) { |
|
| 46 | + $table = $schema->createTable('ratelimit_entries'); |
|
| 47 | + $table->addColumn('id', Types::BIGINT, [ |
|
| 48 | + 'autoincrement' => true, |
|
| 49 | + 'notnull' => true, |
|
| 50 | + ]); |
|
| 51 | + $table->addColumn('hash', Types::STRING, [ |
|
| 52 | + 'notnull' => true, |
|
| 53 | + 'length' => 128, |
|
| 54 | + ]); |
|
| 55 | + $table->addColumn('delete_after', Types::DATETIME, [ |
|
| 56 | + 'notnull' => true, |
|
| 57 | + ]); |
|
| 58 | + $table->setPrimaryKey(['id']); |
|
| 59 | + $table->addIndex(['hash'], 'ratelimit_hash'); |
|
| 60 | + $table->addIndex(['delete_after'], 'ratelimit_delete_after'); |
|
| 61 | + return $schema; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - return null; |
|
| 65 | - } |
|
| 64 | + return null; |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -29,22 +29,22 @@ |
||
| 29 | 29 | use OCP\Migration\SimpleMigrationStep; |
| 30 | 30 | |
| 31 | 31 | class Version24000Date20211213081506 extends SimpleMigrationStep { |
| 32 | - /** |
|
| 33 | - * @param IOutput $output |
|
| 34 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 35 | - * @param array $options |
|
| 36 | - * @return null|ISchemaWrapper |
|
| 37 | - */ |
|
| 38 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 39 | - /** @var ISchemaWrapper $schema */ |
|
| 40 | - $schema = $schemaClosure(); |
|
| 32 | + /** |
|
| 33 | + * @param IOutput $output |
|
| 34 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 35 | + * @param array $options |
|
| 36 | + * @return null|ISchemaWrapper |
|
| 37 | + */ |
|
| 38 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 39 | + /** @var ISchemaWrapper $schema */ |
|
| 40 | + $schema = $schemaClosure(); |
|
| 41 | 41 | |
| 42 | - $hasTable = $schema->hasTable('ratelimit_entries'); |
|
| 43 | - if ($hasTable) { |
|
| 44 | - $schema->dropTable('ratelimit_entries'); |
|
| 45 | - return $schema; |
|
| 46 | - } |
|
| 42 | + $hasTable = $schema->hasTable('ratelimit_entries'); |
|
| 43 | + if ($hasTable) { |
|
| 44 | + $schema->dropTable('ratelimit_entries'); |
|
| 45 | + return $schema; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - return null; |
|
| 49 | - } |
|
| 48 | + return null; |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -11,18 +11,18 @@ discard block |
||
| 11 | 11 | use OCP\Migration\SimpleMigrationStep; |
| 12 | 12 | |
| 13 | 13 | class Version23000Date20210906132259 extends SimpleMigrationStep { |
| 14 | - /** |
|
| 15 | - * @param IOutput $output |
|
| 16 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 17 | - * @param array $options |
|
| 18 | - * @return null|ISchemaWrapper |
|
| 19 | - */ |
|
| 20 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 21 | - /** |
|
| 22 | - * Table was missing a primary key |
|
| 23 | - * Therefore it was dropped with Version24000Date20211213081506 |
|
| 24 | - * and then recreated with a primary key in Version24000Date20211213081604 |
|
| 25 | - */ |
|
| 14 | + /** |
|
| 15 | + * @param IOutput $output |
|
| 16 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 17 | + * @param array $options |
|
| 18 | + * @return null|ISchemaWrapper |
|
| 19 | + */ |
|
| 20 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 21 | + /** |
|
| 22 | + * Table was missing a primary key |
|
| 23 | + * Therefore it was dropped with Version24000Date20211213081506 |
|
| 24 | + * and then recreated with a primary key in Version24000Date20211213081604 |
|
| 25 | + */ |
|
| 26 | 26 | // /** @var ISchemaWrapper $schema */ |
| 27 | 27 | // $schema = $schemaClosure(); |
| 28 | 28 | // |
@@ -42,6 +42,6 @@ discard block |
||
| 42 | 42 | // return $schema; |
| 43 | 43 | // } |
| 44 | 44 | |
| 45 | - return null; |
|
| 46 | - } |
|
| 45 | + return null; |
|
| 46 | + } |
|
| 47 | 47 | } |