1
0

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