RememberBackupCodesJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $this->jobList->remove(self::class, $argument);
  62. return;
  63. }
  64. $providers = $this->registry->getProviderStates($user);
  65. $state2fa = array_reduce($providers, function (bool $carry, bool $state) {
  66. return $carry || $state;
  67. }, false);
  68. /*
  69. * If no provider is active or if the backup codes are already generate
  70. * we can remove the job
  71. */
  72. if ($state2fa === false || (isset($providers['backup_codes']) && $providers['backup_codes'] === true)) {
  73. // Backup codes already generated lets remove this job
  74. $this->jobList->remove(self::class, $argument);
  75. return;
  76. }
  77. $date = new \DateTime();
  78. $date->setTimestamp($this->time->getTime());
  79. $notification = $this->notificationManager->createNotification();
  80. $notification->setApp('twofactor_backupcodes')
  81. ->setUser($user->getUID())
  82. ->setObject('create', 'codes')
  83. ->setSubject('create_backupcodes');
  84. $this->notificationManager->markProcessed($notification);
  85. if (!$user->isEnabled()) {
  86. // Don't recreate a notification for a user that can not read it
  87. $this->jobList->remove(self::class, $argument);
  88. return;
  89. }
  90. $notification->setDateTime($date);
  91. $this->notificationManager->notify($notification);
  92. }
  93. }