RememberBackupCodesJob.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\IJobList;
  29. use OCP\BackgroundJob\TimedJob;
  30. use OCP\IUserManager;
  31. use OCP\Notification\IManager;
  32. class RememberBackupCodesJob extends TimedJob {
  33. /** @var IRegistry */
  34. private $registry;
  35. /** @var IUserManager */
  36. private $userManager;
  37. /** @var IManager */
  38. private $notificationManager;
  39. /** @var IJobList */
  40. private $jobList;
  41. public function __construct(IRegistry $registry,
  42. IUserManager $userManager,
  43. ITimeFactory $timeFactory,
  44. IManager $notificationManager,
  45. IJobList $jobList) {
  46. parent::__construct($timeFactory);
  47. $this->registry = $registry;
  48. $this->userManager = $userManager;
  49. $this->time = $timeFactory;
  50. $this->notificationManager = $notificationManager;
  51. $this->jobList = $jobList;
  52. $this->setInterval(60 * 60 * 24 * 14);
  53. }
  54. protected function run($argument) {
  55. $uid = $argument['uid'];
  56. $user = $this->userManager->get($uid);
  57. if ($user === null) {
  58. // We can't run with an invalid user
  59. return;
  60. }
  61. $providers = $this->registry->getProviderStates($user);
  62. $state2fa = array_reduce($providers, function (bool $carry, bool $state) {
  63. return $carry || $state;
  64. }, false);
  65. /*
  66. * If no provider is active or if the backup codes are already generate
  67. * we can remove the job
  68. */
  69. if ($state2fa === false || (isset($providers['backup_codes']) && $providers['backup_codes'] === true)) {
  70. // Backup codes already generated lets remove this job
  71. $this->jobList->remove(self::class, $argument);
  72. return;
  73. }
  74. $date = new \DateTime();
  75. $date->setTimestamp($this->time->getTime());
  76. $notification = $this->notificationManager->createNotification();
  77. $notification->setApp('twofactor_backupcodes')
  78. ->setUser($user->getUID())
  79. ->setDateTime($date)
  80. ->setObject('create', 'codes')
  81. ->setSubject('create_backupcodes');
  82. $this->notificationManager->notify($notification);
  83. }
  84. }