ChangeUserStateCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2021 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Console;
  9. use Grav\Common\Grav;
  10. use Grav\Common\User\Interfaces\UserCollectionInterface;
  11. use Grav\Console\ConsoleCommand;
  12. use Grav\Plugin\Login\Login;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Question\ChoiceQuestion;
  15. use Symfony\Component\Console\Question\Question;
  16. /**
  17. * Class CleanCommand
  18. *
  19. * @package Grav\Console\Cli
  20. */
  21. class ChangeUserStateCommand extends ConsoleCommand
  22. {
  23. /** @var array */
  24. protected $options = [];
  25. /** @var Login */
  26. protected $login;
  27. /**
  28. * Configure the command
  29. */
  30. protected function configure()
  31. {
  32. $this
  33. ->setName('toggle-user')
  34. ->setAliases(['disableuser', 'enableuser', 'toggleuser', 'change-user-state'])
  35. ->addOption(
  36. 'user',
  37. 'u',
  38. InputOption::VALUE_REQUIRED,
  39. 'The username'
  40. )
  41. ->addOption(
  42. 'state',
  43. 's',
  44. InputOption::VALUE_REQUIRED,
  45. 'The state of the account. Can be either `enabled` or `disabled`. [default: "enabled"]'
  46. )
  47. ->setDescription('Changes whether user can login or not')
  48. ->setHelp('The <info>toggle-user</info> sets a user\'s login status to enabled or disabled.')
  49. ;
  50. }
  51. /**
  52. * @return int|null|void
  53. */
  54. protected function serve()
  55. {
  56. include __DIR__ . '/../vendor/autoload.php';
  57. $grav = Grav::instance();
  58. if (!isset($grav['login'])) {
  59. $grav['login'] = new Login($grav);
  60. }
  61. $this->login = $grav['login'];
  62. $this->options = [
  63. 'user' => $this->input->getOption('user'),
  64. 'state' => $this->input->getOption('state')
  65. ];
  66. $this->validateOptions();
  67. $helper = $this->getHelper('question');
  68. $data = [];
  69. $this->output->writeln('<green>Setting User State</green>');
  70. $this->output->writeln('');
  71. /** @var UserCollectionInterface $users */
  72. $users = $grav['accounts'];
  73. if (!$this->options['user']) {
  74. // Get username and validate
  75. $question = new Question('Enter a <yellow>username</yellow>: ');
  76. $question->setValidator(function ($value) use ($users) {
  77. $this->validate('user', $value);
  78. if (!$users->find($value, ['username'])->exists()) {
  79. throw new \RuntimeException('Username "' . $value . '" does not exist, please pick another username');
  80. };
  81. return $value;
  82. });
  83. $username = $helper->ask($this->input, $this->output, $question);
  84. } else {
  85. $username = $this->options['user'];
  86. }
  87. if (!$this->options['state'] && !\count(array_filter($this->options))) {
  88. // Choose State
  89. $question = new ChoiceQuestion(
  90. 'Please choose the <yellow>state</yellow> for the account:',
  91. array('enabled' => 'Enabled', 'disabled' => 'Disabled'),
  92. 'enabled'
  93. );
  94. $question->setErrorMessage('State %s is invalid.');
  95. $data['state'] = $helper->ask($this->input, $this->output, $question);
  96. } else {
  97. $data['state'] = $this->options['state'] ?: 'enabled';
  98. }
  99. $user = $users->load($username);
  100. if (!$user->exists()) {
  101. $this->output->writeln('<red>Failure!</red> User <cyan>' . $username . '</cyan> does not exist!');
  102. exit();
  103. }
  104. //Set the state field to new state
  105. $user->set('state', $data['state']);
  106. $user->save();
  107. $this->invalidateCache();
  108. $this->output->writeln('');
  109. $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '</cyan> state set to .' . $data['state']);
  110. }
  111. /**
  112. *
  113. */
  114. protected function validateOptions()
  115. {
  116. foreach (array_filter($this->options) as $type => $value) {
  117. $this->validate($type, $value);
  118. }
  119. }
  120. /**
  121. * @param string $type
  122. * @param mixed $value
  123. * @param string $extra
  124. *
  125. * @return string
  126. */
  127. protected function validate($type, $value)
  128. {
  129. return $this->login->validateField($type, $value);
  130. }
  131. }