ChangeUserStateCommand.php 5.0 KB

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