Registry.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\EventDispatcher\IEventDispatcher;
  32. use OCP\IUser;
  33. class Registry implements IRegistry {
  34. /** @var ProviderUserAssignmentDao */
  35. private $assignmentDao;
  36. /** @var IEventDispatcher */
  37. private $dispatcher;
  38. public function __construct(ProviderUserAssignmentDao $assignmentDao,
  39. IEventDispatcher $dispatcher) {
  40. $this->assignmentDao = $assignmentDao;
  41. $this->dispatcher = $dispatcher;
  42. }
  43. public function getProviderStates(IUser $user): array {
  44. return $this->assignmentDao->getState($user->getUID());
  45. }
  46. public function enableProviderFor(IProvider $provider, IUser $user) {
  47. $this->assignmentDao->persist($provider->getId(), $user->getUID(), 1);
  48. $event = new RegistryEvent($provider, $user);
  49. $this->dispatcher->dispatch(self::EVENT_PROVIDER_ENABLED, $event);
  50. }
  51. public function disableProviderFor(IProvider $provider, IUser $user) {
  52. $this->assignmentDao->persist($provider->getId(), $user->getUID(), 0);
  53. $event = new RegistryEvent($provider, $user);
  54. $this->dispatcher->dispatch(self::EVENT_PROVIDER_DISABLED, $event);
  55. }
  56. public function deleteUserData(IUser $user): void {
  57. foreach ($this->assignmentDao->deleteByUser($user->getUID()) as $provider) {
  58. $event = new TwoFactorProviderDisabled($provider['provider_id']);
  59. $this->dispatcher->dispatchTyped($event);
  60. }
  61. }
  62. public function cleanUp(string $providerId) {
  63. $this->assignmentDao->deleteAll($providerId);
  64. }
  65. }