You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProviderDisabled.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, 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\Listener;
  26. use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob;
  27. use OCP\Authentication\TwoFactorAuth\IRegistry;
  28. use OCP\Authentication\TwoFactorAuth\RegistryEvent;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\EventDispatcher\Event;
  31. use OCP\EventDispatcher\IEventListener;
  32. /** @template-implements IEventListener<RegistryEvent> */
  33. class ProviderDisabled implements IEventListener {
  34. /** @var IRegistry */
  35. private $registry;
  36. /** @var IJobList */
  37. private $jobList;
  38. public function __construct(IRegistry $registry,
  39. IJobList $jobList) {
  40. $this->registry = $registry;
  41. $this->jobList = $jobList;
  42. }
  43. public function handle(Event $event): void {
  44. if (!($event instanceof RegistryEvent)) {
  45. return;
  46. }
  47. $providers = $this->registry->getProviderStates($event->getUser());
  48. // Loop over all providers. If all are disabled we remove the job
  49. $state = array_reduce($providers, function (bool $carry, bool $enabled) {
  50. return $carry || $enabled;
  51. }, false);
  52. if ($state === false) {
  53. $this->jobList->remove(RememberBackupCodesJob::class, ['uid' => $event->getUser()->getUID()]);
  54. }
  55. }
  56. }