RememberBackupCodesJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\TwoFactorBackupCodes\BackgroundJob;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\Authentication\TwoFactorAuth\IRegistry;
  28. use OCP\BackgroundJob\IJob;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\BackgroundJob\TimedJob;
  31. use OCP\IUserManager;
  32. use OCP\Notification\IManager;
  33. class RememberBackupCodesJob extends TimedJob {
  34. /** @var IRegistry */
  35. private $registry;
  36. /** @var IUserManager */
  37. private $userManager;
  38. /** @var IManager */
  39. private $notificationManager;
  40. /** @var IJobList */
  41. private $jobList;
  42. public function __construct(IRegistry $registry,
  43. IUserManager $userManager,
  44. ITimeFactory $timeFactory,
  45. IManager $notificationManager,
  46. IJobList $jobList) {
  47. parent::__construct($timeFactory);
  48. $this->registry = $registry;
  49. $this->userManager = $userManager;
  50. $this->time = $timeFactory;
  51. $this->notificationManager = $notificationManager;
  52. $this->jobList = $jobList;
  53. $this->setInterval(60 * 60 * 24 * 14);
  54. $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
  55. }
  56. protected function run($argument) {
  57. $uid = $argument['uid'];
  58. $user = $this->userManager->get($uid);
  59. if ($user === null) {
  60. // We can't run with an invalid user
  61. return;
  62. }
  63. $providers = $this->registry->getProviderStates($user);
  64. $state2fa = array_reduce($providers, function (bool $carry, bool $state) {
  65. return $carry || $state;
  66. }, false);
  67. /*
  68. * If no provider is active or if the backup codes are already generate
  69. * we can remove the job
  70. */
  71. if ($state2fa === false || (isset($providers['backup_codes']) && $providers['backup_codes'] === true)) {
  72. // Backup codes already generated lets remove this job
  73. $this->jobList->remove(self::class, $argument);
  74. return;
  75. }
  76. $date = new \DateTime();
  77. $date->setTimestamp($this->time->getTime());
  78. $notification = $this->notificationManager->createNotification();
  79. $notification->setApp('twofactor_backupcodes')
  80. ->setUser($user->getUID())
  81. ->setDateTime($date)
  82. ->setObject('create', 'codes')
  83. ->setSubject('create_backupcodes');
  84. $this->notificationManager->notify($notification);
  85. }
  86. }