1
0

ProviderManager.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Exceptions\InvalidProviderException;
  9. use OCP\Authentication\TwoFactorAuth\IActivatableByAdmin;
  10. use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin;
  11. use OCP\Authentication\TwoFactorAuth\IProvider;
  12. use OCP\Authentication\TwoFactorAuth\IRegistry;
  13. use OCP\IUser;
  14. class ProviderManager {
  15. /** @var ProviderLoader */
  16. private $providerLoader;
  17. /** @var IRegistry */
  18. private $providerRegistry;
  19. public function __construct(ProviderLoader $providerLoader, IRegistry $providerRegistry) {
  20. $this->providerLoader = $providerLoader;
  21. $this->providerRegistry = $providerRegistry;
  22. }
  23. private function getProvider(string $providerId, IUser $user): IProvider {
  24. $providers = $this->providerLoader->getProviders($user);
  25. if (!isset($providers[$providerId])) {
  26. throw new InvalidProviderException($providerId);
  27. }
  28. return $providers[$providerId];
  29. }
  30. /**
  31. * Try to enable the provider with the given id for the given user
  32. *
  33. * @param IUser $user
  34. *
  35. * @return bool whether the provider supports this operation
  36. */
  37. public function tryEnableProviderFor(string $providerId, IUser $user): bool {
  38. $provider = $this->getProvider($providerId, $user);
  39. if ($provider instanceof IActivatableByAdmin) {
  40. $provider->enableFor($user);
  41. $this->providerRegistry->enableProviderFor($provider, $user);
  42. return true;
  43. } else {
  44. return false;
  45. }
  46. }
  47. /**
  48. * Try to disable the provider with the given id for the given user
  49. *
  50. * @param IUser $user
  51. *
  52. * @return bool whether the provider supports this operation
  53. */
  54. public function tryDisableProviderFor(string $providerId, IUser $user): bool {
  55. $provider = $this->getProvider($providerId, $user);
  56. if ($provider instanceof IDeactivatableByAdmin) {
  57. $provider->disableFor($user);
  58. $this->providerRegistry->disableProviderFor($provider, $user);
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. }