Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/registration.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoas Schilling <213943+nickvergessen@users.noreply.github.com>2022-07-11 11:51:21 +0300
committerGitHub <noreply@github.com>2022-07-11 11:51:21 +0300
commit4bbdd98ecdb231637a3f82375e4cce115a693fae (patch)
treea442d51aa4573c9873036f2a7845918642efacee
parent267bf04b97330efd364d7562d4cbfa0e39db5488 (diff)
parent062cfca481062b5dd219ad6ce166e40ca10b3762 (diff)
Merge pull request #434 from nextcloud/add-cleanup-registrations-job
Add cleanup registrations job
-rw-r--r--appinfo/info.xml3
-rw-r--r--lib/BackgroundJob/ExpireRegistrations.php78
-rw-r--r--lib/Db/Registration.php2
-rw-r--r--lib/Db/RegistrationMapper.php9
-rw-r--r--tests/Unit/BackgroundJob/ExpireRegistrationsTest.php80
5 files changed, 171 insertions, 1 deletions
diff --git a/appinfo/info.xml b/appinfo/info.xml
index 3516da1..5967ad9 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -40,6 +40,9 @@ This app allows users to register a new account.
<dependencies>
<nextcloud min-version="22" max-version="24" />
</dependencies>
+ <background-jobs>
+ <job>OCA\Registration\BackgroundJob\ExpireRegistrations</job>
+ </background-jobs>
<settings>
<admin>OCA\Registration\Settings\RegistrationSettings</admin>
<admin-section>OCA\Registration\Settings\RegistrationSettingsSection</admin-section>
diff --git a/lib/BackgroundJob/ExpireRegistrations.php b/lib/BackgroundJob/ExpireRegistrations.php
new file mode 100644
index 0000000..9c888c8
--- /dev/null
+++ b/lib/BackgroundJob/ExpireRegistrations.php
@@ -0,0 +1,78 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2022 Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @author Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Registration\BackgroundJob;
+
+use OCA\Registration\AppInfo\Application;
+use OCA\Registration\Db\RegistrationMapper;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\TimedJob;
+use OCP\IConfig;
+
+class ExpireRegistrations extends TimedJob {
+
+ /** @var RegistrationMapper */
+ protected $registrationMapper;
+
+ /** @var IConfig */
+ protected $config;
+
+ public function __construct(ITimeFactory $time,
+ RegistrationMapper $registrationMapper,
+ IConfig $config) {
+ parent::__construct($time);
+
+ // Run once per day
+ $this->setInterval(60 * 60 * 24);
+ /**
+ * @TODO Remove check with 24+
+ */
+ if (method_exists($this, 'setTimeSensitivity')) {
+ $this->setTimeSensitivity(self::TIME_INSENSITIVE);
+ }
+
+ $this->config = $config;
+ $this->registrationMapper = $registrationMapper;
+ }
+
+ public function run($argument): void {
+ $expireDays = $this->getDuration();
+ $expireDate = $this->time->getDateTime();
+ $interval = new \DateInterval("P" . $expireDays . "D");
+ $expireDate->sub($interval);
+
+ $this->registrationMapper->deleteOlderThan($expireDate);
+ }
+
+ private function getDuration(): int {
+ return max(
+ (int) $this->config->getAppValue(
+ Application::APP_ID,
+ 'expire_days',
+ '30'
+ ),
+ 1
+ );
+ }
+}
diff --git a/lib/Db/Registration.php b/lib/Db/Registration.php
index 46dbbb1..b03dd4c 100644
--- a/lib/Db/Registration.php
+++ b/lib/Db/Registration.php
@@ -64,6 +64,6 @@ class Registration extends Entity {
$this->addType('emailConfirmed', 'boolean');
$this->addType('token', 'string');
$this->addType('clientSecret', 'string');
- $this->addType('requested', 'string'); // TODO datetime is not supported?
+ $this->addType('requested', 'datetime');
}
}
diff --git a/lib/Db/RegistrationMapper.php b/lib/Db/RegistrationMapper.php
index 6c144a3..cf89fd3 100644
--- a/lib/Db/RegistrationMapper.php
+++ b/lib/Db/RegistrationMapper.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
+ * @author Thomas Citharel <nextcloud@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
@@ -28,6 +29,7 @@ namespace OCA\Registration\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
+use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
@@ -122,4 +124,11 @@ class RegistrationMapper extends QBMapper {
$token = $this->random->generate(32, ISecureRandom::CHAR_HUMAN_READABLE);
$registration->setClientSecret($token);
}
+
+ public function deleteOlderThan(\DateTime $date): void {
+ $query = $this->db->getQueryBuilder();
+ $query->delete($this->getTableName())
+ ->where($query->expr()->lt('requested', $query->createNamedParameter($date, IQueryBuilder::PARAM_DATE)))
+ ->executeStatement();
+ }
}
diff --git a/tests/Unit/BackgroundJob/ExpireRegistrationsTest.php b/tests/Unit/BackgroundJob/ExpireRegistrationsTest.php
new file mode 100644
index 0000000..95070b7
--- /dev/null
+++ b/tests/Unit/BackgroundJob/ExpireRegistrationsTest.php
@@ -0,0 +1,80 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022 Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @author Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+namespace OCA\Registration\Tests\Unit\BackgroundJob;
+
+use OCA\Registration\AppInfo\Application;
+use OCA\Registration\BackgroundJob\ExpireRegistrations;
+use OCA\Registration\Db\RegistrationMapper;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\IConfig;
+use PHPUnit\Framework\MockObject\MockObject;
+use Test\TestCase;
+
+class ExpireRegistrationsTest extends TestCase {
+
+ /** @var RegistrationMapper | MockObject */
+ private $registrationMapper;
+
+ /** @var ITimeFactory | MockObject */
+ private $timeFactory;
+
+ /** @var IConfig|MockObject */
+ private $config;
+
+ /** @var ExpireRegistrations */
+ private $backgroundJob;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->registrationMapper = $this->createMock(RegistrationMapper::class);
+ $this->timeFactory = $this->createMock(ITimeFactory::class);
+ $this->config = $this->createMock(IConfig::class);
+
+ $this->backgroundJob = new ExpireRegistrations($this->timeFactory, $this->registrationMapper, $this->config);
+ }
+
+ public function testRun() {
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with(Application::APP_ID, 'expire_days', '30')
+ ->willReturn('20');
+
+ $expireDate = new \DateTime();
+ $this->timeFactory->expects($this->once())
+ ->method('getDateTime')
+ ->with()
+ ->willReturn($expireDate);
+
+ $interval = new \DateInterval("P20D");
+ $expireDate->sub($interval);
+
+ $this->registrationMapper->expects($this->once())
+ ->method('deleteOlderThan')
+ ->with($expireDate);
+
+ $this->backgroundJob->run([]);
+ }
+}