Registry.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 OC\Authentication\TwoFactorAuth;
  8. use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao;
  9. use OCP\Authentication\TwoFactorAuth\IProvider;
  10. use OCP\Authentication\TwoFactorAuth\IRegistry;
  11. use OCP\Authentication\TwoFactorAuth\RegistryEvent;
  12. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderDisabled;
  13. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered;
  14. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered;
  15. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderUserDeleted;
  16. use OCP\EventDispatcher\IEventDispatcher;
  17. use OCP\IUser;
  18. class Registry implements IRegistry {
  19. /** @var ProviderUserAssignmentDao */
  20. private $assignmentDao;
  21. /** @var IEventDispatcher */
  22. private $dispatcher;
  23. public function __construct(ProviderUserAssignmentDao $assignmentDao,
  24. IEventDispatcher $dispatcher) {
  25. $this->assignmentDao = $assignmentDao;
  26. $this->dispatcher = $dispatcher;
  27. }
  28. public function getProviderStates(IUser $user): array {
  29. return $this->assignmentDao->getState($user->getUID());
  30. }
  31. public function enableProviderFor(IProvider $provider, IUser $user) {
  32. $this->assignmentDao->persist($provider->getId(), $user->getUID(), 1);
  33. $event = new RegistryEvent($provider, $user);
  34. $this->dispatcher->dispatch(self::EVENT_PROVIDER_ENABLED, $event);
  35. $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserRegistered($user, $provider));
  36. }
  37. public function disableProviderFor(IProvider $provider, IUser $user) {
  38. $this->assignmentDao->persist($provider->getId(), $user->getUID(), 0);
  39. $event = new RegistryEvent($provider, $user);
  40. $this->dispatcher->dispatch(self::EVENT_PROVIDER_DISABLED, $event);
  41. $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserUnregistered($user, $provider));
  42. }
  43. public function deleteUserData(IUser $user): void {
  44. foreach ($this->assignmentDao->deleteByUser($user->getUID()) as $provider) {
  45. $event = new TwoFactorProviderDisabled($provider['provider_id']);
  46. $this->dispatcher->dispatchTyped($event);
  47. $this->dispatcher->dispatchTyped(new TwoFactorProviderUserDeleted($user, $provider['provider_id']));
  48. }
  49. }
  50. public function cleanUp(string $providerId) {
  51. $this->assignmentDao->deleteAll($providerId);
  52. }
  53. }