You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

State.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\Core\Command\TwoFactorAuth;
  8. use OCP\Authentication\TwoFactorAuth\IRegistry;
  9. use OCP\IUserManager;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class State extends Base {
  14. public function __construct(
  15. private IRegistry $registry,
  16. IUserManager $userManager,
  17. ) {
  18. parent::__construct(
  19. 'twofactorauth:state',
  20. $userManager,
  21. );
  22. }
  23. protected function configure() {
  24. parent::configure();
  25. $this->setName('twofactorauth:state');
  26. $this->setDescription('Get the two-factor authentication (2FA) state of a user');
  27. $this->addArgument('uid', InputArgument::REQUIRED);
  28. }
  29. protected function execute(InputInterface $input, OutputInterface $output): int {
  30. $uid = $input->getArgument('uid');
  31. $user = $this->userManager->get($uid);
  32. if (is_null($user)) {
  33. $output->writeln("<error>Invalid UID</error>");
  34. return 1;
  35. }
  36. $providerStates = $this->registry->getProviderStates($user);
  37. $filtered = $this->filterEnabledDisabledUnknownProviders($providerStates);
  38. [$enabled, $disabled] = $filtered;
  39. if (!empty($enabled)) {
  40. $output->writeln("Two-factor authentication is enabled for user $uid");
  41. } else {
  42. $output->writeln("Two-factor authentication is not enabled for user $uid");
  43. }
  44. $output->writeln("");
  45. $this->printProviders("Enabled providers", $enabled, $output);
  46. $this->printProviders("Disabled providers", $disabled, $output);
  47. return 0;
  48. }
  49. private function filterEnabledDisabledUnknownProviders(array $providerStates): array {
  50. $enabled = [];
  51. $disabled = [];
  52. foreach ($providerStates as $providerId => $isEnabled) {
  53. if ($isEnabled) {
  54. $enabled[] = $providerId;
  55. } else {
  56. $disabled[] = $providerId;
  57. }
  58. }
  59. return [$enabled, $disabled];
  60. }
  61. private function printProviders(string $title, array $providers,
  62. OutputInterface $output) {
  63. if (empty($providers)) {
  64. // Ignore and don't print anything
  65. return;
  66. }
  67. $output->writeln($title . ":");
  68. foreach ($providers as $provider) {
  69. $output->writeln("- " . $provider);
  70. }
  71. }
  72. }