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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2018 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\Core\Command\TwoFactorAuth;
  25. use OC\Core\Command\Base;
  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. /** @var IUserManager */
  35. private $userManager;
  36. public function __construct(IRegistry $registry, IUserManager $userManager) {
  37. parent::__construct('twofactorauth:state');
  38. $this->registry = $registry;
  39. $this->userManager = $userManager;
  40. }
  41. protected function configure() {
  42. parent::configure();
  43. $this->setName('twofactorauth:state');
  44. $this->setDescription('Get the two-factor authentication (2FA) state of a user');
  45. $this->addArgument('uid', InputArgument::REQUIRED);
  46. }
  47. protected function execute(InputInterface $input, OutputInterface $output) {
  48. $uid = $input->getArgument('uid');
  49. $user = $this->userManager->get($uid);
  50. if (is_null($user)) {
  51. $output->writeln("<error>Invalid UID</error>");
  52. return;
  53. }
  54. $providerStates = $this->registry->getProviderStates($user);
  55. $filtered = $this->filterEnabledDisabledUnknownProviders($providerStates);
  56. list ($enabled, $disabled) = $filtered;
  57. if (!empty($enabled)) {
  58. $output->writeln("Two-factor authentication is enabled for user $uid");
  59. } else {
  60. $output->writeln("Two-factor authentication is not enabled for user $uid");
  61. }
  62. $output->writeln("");
  63. $this->printProviders("Enabled providers", $enabled, $output);
  64. $this->printProviders("Disabled providers", $disabled, $output);
  65. }
  66. private function filterEnabledDisabledUnknownProviders(array $providerStates): array {
  67. $enabled = [];
  68. $disabled = [];
  69. foreach ($providerStates as $providerId => $isEnabled) {
  70. if ($isEnabled) {
  71. $enabled[] = $providerId;
  72. } else {
  73. $disabled[] = $providerId;
  74. }
  75. }
  76. return [$enabled, $disabled];
  77. }
  78. private function printProviders(string $title, array $providers,
  79. OutputInterface $output) {
  80. if (empty($providers)) {
  81. // Ignore and don't print anything
  82. return;
  83. }
  84. $output->writeln($title . ":");
  85. foreach ($providers as $provider) {
  86. $output->writeln("- " . $provider);
  87. }
  88. }
  89. }