CheckBackupCodes.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 IUserManager */
  17. private $userManager;
  18. /** @var IJobList */
  19. private $jobList;
  20. /** @var IRegistry */
  21. private $registry;
  22. /** @var Manager */
  23. private $twofactorManager;
  24. public function __construct(ITimeFactory $timeFactory, IUserManager $userManager, IJobList $jobList, Manager $twofactorManager, IRegistry $registry) {
  25. parent::__construct($timeFactory);
  26. $this->userManager = $userManager;
  27. $this->jobList = $jobList;
  28. $this->twofactorManager = $twofactorManager;
  29. $this->registry = $registry;
  30. }
  31. protected function run($argument) {
  32. $this->userManager->callForSeenUsers(function (IUser $user) {
  33. if (!$user->isEnabled()) {
  34. return;
  35. }
  36. $providers = $this->registry->getProviderStates($user);
  37. $isTwoFactorAuthenticated = $this->twofactorManager->isTwoFactorAuthenticated($user);
  38. if ($isTwoFactorAuthenticated && isset($providers['backup_codes']) && $providers['backup_codes'] === false) {
  39. $this->jobList->add(RememberBackupCodesJob::class, ['uid' => $user->getUID()]);
  40. }
  41. });
  42. }
  43. }