RememberBackupCodesJob.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\Authentication\TwoFactorAuth\IRegistry;
  10. use OCP\BackgroundJob\IJob;
  11. use OCP\BackgroundJob\IJobList;
  12. use OCP\BackgroundJob\TimedJob;
  13. use OCP\IUserManager;
  14. use OCP\Notification\IManager;
  15. class RememberBackupCodesJob extends TimedJob {
  16. /** @var IRegistry */
  17. private $registry;
  18. /** @var IUserManager */
  19. private $userManager;
  20. /** @var IManager */
  21. private $notificationManager;
  22. /** @var IJobList */
  23. private $jobList;
  24. public function __construct(IRegistry $registry,
  25. IUserManager $userManager,
  26. ITimeFactory $timeFactory,
  27. IManager $notificationManager,
  28. IJobList $jobList) {
  29. parent::__construct($timeFactory);
  30. $this->registry = $registry;
  31. $this->userManager = $userManager;
  32. $this->time = $timeFactory;
  33. $this->notificationManager = $notificationManager;
  34. $this->jobList = $jobList;
  35. $this->setInterval(60 * 60 * 24 * 14);
  36. $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
  37. }
  38. protected function run($argument) {
  39. $uid = $argument['uid'];
  40. $user = $this->userManager->get($uid);
  41. if ($user === null) {
  42. // We can't run with an invalid user
  43. $this->jobList->remove(self::class, $argument);
  44. return;
  45. }
  46. $providers = $this->registry->getProviderStates($user);
  47. $state2fa = array_reduce($providers, function (bool $carry, bool $state) {
  48. return $carry || $state;
  49. }, false);
  50. /*
  51. * If no provider is active or if the backup codes are already generate
  52. * we can remove the job
  53. */
  54. if ($state2fa === false || (isset($providers['backup_codes']) && $providers['backup_codes'] === true)) {
  55. // Backup codes already generated lets remove this job
  56. $this->jobList->remove(self::class, $argument);
  57. return;
  58. }
  59. $date = new \DateTime();
  60. $date->setTimestamp($this->time->getTime());
  61. $notification = $this->notificationManager->createNotification();
  62. $notification->setApp('twofactor_backupcodes')
  63. ->setUser($user->getUID())
  64. ->setObject('create', 'codes')
  65. ->setSubject('create_backupcodes');
  66. $this->notificationManager->markProcessed($notification);
  67. if (!$user->isEnabled()) {
  68. // Don't recreate a notification for a user that can not read it
  69. $this->jobList->remove(self::class, $argument);
  70. return;
  71. }
  72. $notification->setDateTime($date);
  73. $this->notificationManager->notify($notification);
  74. }
  75. }