CheckBackupCodes.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\BackgroundJob;
  8. use OC\Authentication\TwoFactorAuth\Manager;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\Authentication\TwoFactorAuth\IRegistry;
  11. use OCP\BackgroundJob\IJobList;
  12. use OCP\BackgroundJob\QueuedJob;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. class CheckBackupCodes extends QueuedJob {
  16. /** @var Manager */
  17. private $twofactorManager;
  18. public function __construct(
  19. ITimeFactory $timeFactory,
  20. private IUserManager $userManager,
  21. private IJobList $jobList,
  22. Manager $twofactorManager,
  23. private IRegistry $registry,
  24. ) {
  25. parent::__construct($timeFactory);
  26. $this->twofactorManager = $twofactorManager;
  27. }
  28. protected function run($argument) {
  29. $this->userManager->callForSeenUsers(function (IUser $user): void {
  30. if (!$user->isEnabled()) {
  31. return;
  32. }
  33. $providers = $this->registry->getProviderStates($user);
  34. $isTwoFactorAuthenticated = $this->twofactorManager->isTwoFactorAuthenticated($user);
  35. if ($isTwoFactorAuthenticated && isset($providers['backup_codes']) && $providers['backup_codes'] === false) {
  36. $this->jobList->add(RememberBackupCodesJob::class, ['uid' => $user->getUID()]);
  37. }
  38. });
  39. }
  40. }