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 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 function implode;
  26. use OC\Authentication\TwoFactorAuth\EnforcementState;
  27. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Input\InputOption;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Enforce extends Command {
  33. /** @var MandatoryTwoFactor */
  34. private $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) {
  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. }
  82. /**
  83. * @param OutputInterface $output
  84. */
  85. protected function writeEnforced(OutputInterface $output, EnforcementState $state) {
  86. if (empty($state->getEnforcedGroups())) {
  87. $message = 'Two-factor authentication is enforced for all users';
  88. } else {
  89. $message = 'Two-factor authentication is enforced for members of the group(s) ' . implode(', ', $state->getEnforcedGroups());
  90. }
  91. if (!empty($state->getExcludedGroups())) {
  92. $message .= ', except members of ' . implode(', ', $state->getExcludedGroups());
  93. }
  94. $output->writeln($message);
  95. }
  96. /**
  97. * @param OutputInterface $output
  98. */
  99. protected function writeNotEnforced(OutputInterface $output) {
  100. $output->writeln('Two-factor authentication is not enforced');
  101. }
  102. }