Browse Source

Merge pull request #13586 from nextcloud/enh/2fa_backup_codes_improvements

Some twofactor_backupcodes cleanup
Roeland Jago Douma 5 years ago
parent
commit
170995b213

+ 4 - 2
apps/twofactor_backupcodes/lib/BackgroundJob/CheckBackupCodes.php

@@ -25,9 +25,10 @@ declare(strict_types=1);
 namespace OCA\TwoFactorBackupCodes\BackgroundJob;
 
 use OC\Authentication\TwoFactorAuth\Manager;
-use OC\BackgroundJob\QueuedJob;
+use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\Authentication\TwoFactorAuth\IRegistry;
 use OCP\BackgroundJob\IJobList;
+use OCP\BackgroundJob\QueuedJob;
 use OCP\IUser;
 use OCP\IUserManager;
 
@@ -45,7 +46,8 @@ class CheckBackupCodes extends QueuedJob {
 	/** @var Manager */
 	private $twofactorManager;
 
-	public function __construct(IUserManager $userManager, IJobList $jobList, Manager $twofactorManager, IRegistry $registry) {
+	public function __construct(ITimeFactory $timeFactory, IUserManager $userManager, IJobList $jobList, Manager $twofactorManager, IRegistry $registry) {
+		parent::__construct($timeFactory);
 		$this->userManager = $userManager;
 		$this->jobList = $jobList;
 		$this->twofactorManager = $twofactorManager;

+ 2 - 4
apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php

@@ -24,10 +24,10 @@ declare(strict_types=1);
 
 namespace OCA\TwoFactorBackupCodes\BackgroundJob;
 
-use OC\BackgroundJob\TimedJob;
 use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\Authentication\TwoFactorAuth\IRegistry;
 use OCP\BackgroundJob\IJobList;
+use OCP\BackgroundJob\TimedJob;
 use OCP\IUserManager;
 use OCP\Notification\IManager;
 
@@ -39,9 +39,6 @@ class RememberBackupCodesJob extends TimedJob {
 	/** @var IUserManager */
 	private $userManager;
 
-	/** @var ITimeFactory */
-	private $time;
-
 	/** @var IManager */
 	private $notificationManager;
 
@@ -53,6 +50,7 @@ class RememberBackupCodesJob extends TimedJob {
 								ITimeFactory $timeFactory,
 								IManager $notificationManager,
 								IJobList $jobList) {
+		parent::__construct($timeFactory);
 		$this->registry = $registry;
 		$this->userManager = $userManager;
 		$this->time = $timeFactory;

+ 3 - 8
apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php

@@ -22,11 +22,12 @@
 namespace OCA\TwoFactorBackupCodes\Db;
 
 use OCP\AppFramework\Db\Mapper;
+use OCP\AppFramework\Db\QBMapper;
 use OCP\DB\QueryBuilder\IQueryBuilder;
 use OCP\IDBConnection;
 use OCP\IUser;
 
-class BackupCodeMapper extends Mapper {
+class BackupCodeMapper extends QBMapper {
 
 	public function __construct(IDBConnection $db) {
 		parent::__construct($db, 'twofactor_backupcodes');
@@ -43,14 +44,8 @@ class BackupCodeMapper extends Mapper {
 		$qb->select('id', 'user_id', 'code', 'used')
 			->from('twofactor_backupcodes')
 			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
-		$result = $qb->execute();
 
-		$rows = $result->fetchAll();
-		$result->closeCursor();
-
-		return array_map(function ($row) {
-			return BackupCode::fromRow($row);
-		}, $rows);
+		return self::findEntities($qb);
 	}
 
 	/**

+ 2 - 0
apps/twofactor_backupcodes/tests/Unit/BackgroundJob/CheckBackupCodeTest.php

@@ -27,6 +27,7 @@ namespace OCA\TwoFactorBackupCodes\Tests\Unit\BackgroundJob;
 use OC\Authentication\TwoFactorAuth\Manager;
 use OCA\TwoFactorBackupCodes\BackgroundJob\CheckBackupCodes;
 use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
+use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\Authentication\TwoFactorAuth\IRegistry;
 use OCP\BackgroundJob\IJobList;
 use OCP\IUser;
@@ -70,6 +71,7 @@ class CheckBackupCodeTest extends TestCase {
 			}));
 
 		$this->checkBackupCodes = new CheckBackupCodes(
+			$this->createMock(ITimeFactory::class),
 			$this->userManager,
 			$this->jobList,
 			$this->manager,