Enforce.php 2.6 KB

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 OC\Authentication\TwoFactorAuth\EnforcementState;
  9. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use function implode;
  15. class Enforce extends Command {
  16. public function __construct(
  17. private MandatoryTwoFactor $mandatoryTwoFactor,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure() {
  22. $this->setName('twofactorauth:enforce');
  23. $this->setDescription('Enabled/disable enforced two-factor authentication');
  24. $this->addOption(
  25. 'on',
  26. null,
  27. InputOption::VALUE_NONE,
  28. 'enforce two-factor authentication'
  29. );
  30. $this->addOption(
  31. 'off',
  32. null,
  33. InputOption::VALUE_NONE,
  34. 'don\'t enforce two-factor authenticaton'
  35. );
  36. $this->addOption(
  37. 'group',
  38. null,
  39. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  40. 'enforce only for the given group(s)'
  41. );
  42. $this->addOption(
  43. 'exclude',
  44. null,
  45. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  46. 'exclude mandatory two-factor auth for the given group(s)'
  47. );
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output): int {
  50. if ($input->getOption('on')) {
  51. $enforcedGroups = $input->getOption('group');
  52. $excludedGroups = $input->getOption('exclude');
  53. $this->mandatoryTwoFactor->setState(new EnforcementState(true, $enforcedGroups, $excludedGroups));
  54. } elseif ($input->getOption('off')) {
  55. $this->mandatoryTwoFactor->setState(new EnforcementState(false));
  56. }
  57. $state = $this->mandatoryTwoFactor->getState();
  58. if ($state->isEnforced()) {
  59. $this->writeEnforced($output, $state);
  60. } else {
  61. $this->writeNotEnforced($output);
  62. }
  63. return 0;
  64. }
  65. protected function writeEnforced(OutputInterface $output, EnforcementState $state) {
  66. if (empty($state->getEnforcedGroups())) {
  67. $message = 'Two-factor authentication is enforced for all users';
  68. } else {
  69. $message = 'Two-factor authentication is enforced for members of the group(s) ' . implode(', ', $state->getEnforcedGroups());
  70. }
  71. if (!empty($state->getExcludedGroups())) {
  72. $message .= ', except members of ' . implode(', ', $state->getExcludedGroups());
  73. }
  74. $output->writeln($message);
  75. }
  76. protected function writeNotEnforced(OutputInterface $output) {
  77. $output->writeln('Two-factor authentication is not enforced');
  78. }
  79. }