Registry.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  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 OC\Authentication\TwoFactorAuth;
  26. use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao;
  27. use OCP\Authentication\TwoFactorAuth\IProvider;
  28. use OCP\Authentication\TwoFactorAuth\IRegistry;
  29. use OCP\Authentication\TwoFactorAuth\RegistryEvent;
  30. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderDisabled;
  31. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered;
  32. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered;
  33. use OCP\Authentication\TwoFactorAuth\TwoFactorProviderUserDeleted;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\IUser;
  36. class Registry implements IRegistry {
  37. /** @var ProviderUserAssignmentDao */
  38. private $assignmentDao;
  39. /** @var IEventDispatcher */
  40. private $dispatcher;
  41. public function __construct(ProviderUserAssignmentDao $assignmentDao,
  42. IEventDispatcher $dispatcher) {
  43. $this->assignmentDao = $assignmentDao;
  44. $this->dispatcher = $dispatcher;
  45. }
  46. public function getProviderStates(IUser $user): array {
  47. return $this->assignmentDao->getState($user->getUID());
  48. }
  49. public function enableProviderFor(IProvider $provider, IUser $user) {
  50. $this->assignmentDao->persist($provider->getId(), $user->getUID(), 1);
  51. $event = new RegistryEvent($provider, $user);
  52. $this->dispatcher->dispatch(self::EVENT_PROVIDER_ENABLED, $event);
  53. $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserRegistered($user, $provider));
  54. }
  55. public function disableProviderFor(IProvider $provider, IUser $user) {
  56. $this->assignmentDao->persist($provider->getId(), $user->getUID(), 0);
  57. $event = new RegistryEvent($provider, $user);
  58. $this->dispatcher->dispatch(self::EVENT_PROVIDER_DISABLED, $event);
  59. $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserUnregistered($user, $provider));
  60. }
  61. public function deleteUserData(IUser $user): void {
  62. foreach ($this->assignmentDao->deleteByUser($user->getUID()) as $provider) {
  63. $event = new TwoFactorProviderDisabled($provider['provider_id']);
  64. $this->dispatcher->dispatchTyped($event);
  65. $this->dispatcher->dispatchTyped(new TwoFactorProviderUserDeleted($user, $provider['provider_id']));
  66. }
  67. }
  68. public function cleanUp(string $providerId) {
  69. $this->assignmentDao->deleteAll($providerId);
  70. }
  71. }