Enforce.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 function implode;
  27. use OC\Authentication\TwoFactorAuth\EnforcementState;
  28. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Input\InputOption;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class Enforce extends Command {
  34. /** @var MandatoryTwoFactor */
  35. private $mandatoryTwoFactor;
  36. public function __construct(MandatoryTwoFactor $mandatoryTwoFactor) {
  37. parent::__construct();
  38. $this->mandatoryTwoFactor = $mandatoryTwoFactor;
  39. }
  40. protected function configure() {
  41. $this->setName('twofactorauth:enforce');
  42. $this->setDescription('Enabled/disable enforced two-factor authentication');
  43. $this->addOption(
  44. 'on',
  45. null,
  46. InputOption::VALUE_NONE,
  47. 'enforce two-factor authentication'
  48. );
  49. $this->addOption(
  50. 'off',
  51. null,
  52. InputOption::VALUE_NONE,
  53. 'don\'t enforce two-factor authenticaton'
  54. );
  55. $this->addOption(
  56. 'group',
  57. null,
  58. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  59. 'enforce only for the given group(s)'
  60. );
  61. $this->addOption(
  62. 'exclude',
  63. null,
  64. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  65. 'exclude mandatory two-factor auth for the given group(s)'
  66. );
  67. }
  68. protected function execute(InputInterface $input, OutputInterface $output): int {
  69. if ($input->getOption('on')) {
  70. $enforcedGroups = $input->getOption('group');
  71. $excludedGroups = $input->getOption('exclude');
  72. $this->mandatoryTwoFactor->setState(new EnforcementState(true, $enforcedGroups, $excludedGroups));
  73. } elseif ($input->getOption('off')) {
  74. $this->mandatoryTwoFactor->setState(new EnforcementState(false));
  75. }
  76. $state = $this->mandatoryTwoFactor->getState();
  77. if ($state->isEnforced()) {
  78. $this->writeEnforced($output, $state);
  79. } else {
  80. $this->writeNotEnforced($output);
  81. }
  82. return 0;
  83. }
  84. /**
  85. * @param OutputInterface $output
  86. */
  87. protected function writeEnforced(OutputInterface $output, EnforcementState $state) {
  88. if (empty($state->getEnforcedGroups())) {
  89. $message = 'Two-factor authentication is enforced for all users';
  90. } else {
  91. $message = 'Two-factor authentication is enforced for members of the group(s) ' . implode(', ', $state->getEnforcedGroups());
  92. }
  93. if (!empty($state->getExcludedGroups())) {
  94. $message .= ', except members of ' . implode(', ', $state->getExcludedGroups());
  95. }
  96. $output->writeln($message);
  97. }
  98. /**
  99. * @param OutputInterface $output
  100. */
  101. protected function writeNotEnforced(OutputInterface $output) {
  102. $output->writeln('Two-factor authentication is not enforced');
  103. }
  104. }