State.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Joas Schilling <coding@schilljs.com>
  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\Core\Command\TwoFactorAuth;
  26. use OCP\Authentication\TwoFactorAuth\IRegistry;
  27. use OCP\IUserManager;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class State extends Base {
  32. /** @var IRegistry */
  33. private $registry;
  34. public function __construct(IRegistry $registry, IUserManager $userManager) {
  35. parent::__construct('twofactorauth:state');
  36. $this->registry = $registry;
  37. $this->userManager = $userManager;
  38. }
  39. protected function configure() {
  40. parent::configure();
  41. $this->setName('twofactorauth:state');
  42. $this->setDescription('Get the two-factor authentication (2FA) state of a user');
  43. $this->addArgument('uid', InputArgument::REQUIRED);
  44. }
  45. protected function execute(InputInterface $input, OutputInterface $output): int {
  46. $uid = $input->getArgument('uid');
  47. $user = $this->userManager->get($uid);
  48. if (is_null($user)) {
  49. $output->writeln("<error>Invalid UID</error>");
  50. return 1;
  51. }
  52. $providerStates = $this->registry->getProviderStates($user);
  53. $filtered = $this->filterEnabledDisabledUnknownProviders($providerStates);
  54. list($enabled, $disabled) = $filtered;
  55. if (!empty($enabled)) {
  56. $output->writeln("Two-factor authentication is enabled for user $uid");
  57. } else {
  58. $output->writeln("Two-factor authentication is not enabled for user $uid");
  59. }
  60. $output->writeln("");
  61. $this->printProviders("Enabled providers", $enabled, $output);
  62. $this->printProviders("Disabled providers", $disabled, $output);
  63. return 0;
  64. }
  65. private function filterEnabledDisabledUnknownProviders(array $providerStates): array {
  66. $enabled = [];
  67. $disabled = [];
  68. foreach ($providerStates as $providerId => $isEnabled) {
  69. if ($isEnabled) {
  70. $enabled[] = $providerId;
  71. } else {
  72. $disabled[] = $providerId;
  73. }
  74. }
  75. return [$enabled, $disabled];
  76. }
  77. private function printProviders(string $title, array $providers,
  78. OutputInterface $output) {
  79. if (empty($providers)) {
  80. // Ignore and don't print anything
  81. return;
  82. }
  83. $output->writeln($title . ":");
  84. foreach ($providers as $provider) {
  85. $output->writeln("- " . $provider);
  86. }
  87. }
  88. }