ProviderManager.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Authentication\TwoFactorAuth;
  25. use OC\Authentication\Exceptions\InvalidProviderException;
  26. use OCP\Authentication\TwoFactorAuth\IActivatableByAdmin;
  27. use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin;
  28. use OCP\Authentication\TwoFactorAuth\IProvider;
  29. use OCP\Authentication\TwoFactorAuth\IRegistry;
  30. use OCP\IUser;
  31. class ProviderManager {
  32. /** @var ProviderLoader */
  33. private $providerLoader;
  34. /** @var IRegistry */
  35. private $providerRegistry;
  36. public function __construct(ProviderLoader $providerLoader, IRegistry $providerRegistry) {
  37. $this->providerLoader = $providerLoader;
  38. $this->providerRegistry = $providerRegistry;
  39. }
  40. private function getProvider(string $providerId, IUser $user): IProvider {
  41. $providers = $this->providerLoader->getProviders($user);
  42. if (!isset($providers[$providerId])) {
  43. throw new InvalidProviderException($providerId);
  44. }
  45. return $providers[$providerId];
  46. }
  47. /**
  48. * Try to enable 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 tryEnableProviderFor(string $providerId, IUser $user): bool {
  55. $provider = $this->getProvider($providerId, $user);
  56. if ($provider instanceof IActivatableByAdmin) {
  57. $provider->enableFor($user);
  58. $this->providerRegistry->enableProviderFor($provider, $user);
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. /**
  65. * Try to disable the provider with the given id for the given user
  66. *
  67. * @param IUser $user
  68. *
  69. * @return bool whether the provider supports this operation
  70. */
  71. public function tryDisableProviderFor(string $providerId, IUser $user): bool {
  72. $provider = $this->getProvider($providerId, $user);
  73. if ($provider instanceof IDeactivatableByAdmin) {
  74. $provider->disableFor($user);
  75. $this->providerRegistry->disableProviderFor($provider, $user);
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. }