diff --git a/system/src/Grav/Console/Cli/NewUserCommand.php b/system/src/Grav/Console/Cli/NewUserCommand.php
index 0dba9c670..1c51eb330 100644
--- a/system/src/Grav/Console/Cli/NewUserCommand.php
+++ b/system/src/Grav/Console/Cli/NewUserCommand.php
@@ -2,11 +2,6 @@
namespace Grav\Console\Cli;
use Grav\Console\ConsoleCommand;
-use Grav\Common\File\CompiledYamlFile;
-use Grav\Common\User\User;
-use Symfony\Component\Console\Helper\Helper;
-use Symfony\Component\Console\Question\ChoiceQuestion;
-use Symfony\Component\Console\Question\Question;
/**
* Class CleanCommand
@@ -22,9 +17,8 @@ class NewUserCommand extends ConsoleCommand
{
$this
->setName('new-user')
- ->setAliases(['newuser'])
- ->setDescription('Creates a new user')
- ->setHelp('The newuser creates a new user file in user/accounts/ folder')
+ ->setDescription('DEPRECATED: Creates a new user')
+ ->setHelp('The new-user from `bin/grav` has been deprecated. Please refer to `bin/plugin admin new-user')
;
}
@@ -33,118 +27,8 @@ class NewUserCommand extends ConsoleCommand
*/
protected function serve()
{
- $helper = $this->getHelper('question');
- $data = [];
-
- $this->output->writeln('Create new user');
$this->output->writeln('');
-
- // Get username and validate
- $question = new Question('Enter a username: ', 'admin');
- $question->setValidator(function ($value) {
- if (!preg_match('/^[a-z0-9_-]{3,16}$/', $value)) {
- throw new \RuntimeException(
- 'Username should be between 3 and 16 characters, including lowercase letters, numbers, underscores, and hyphens. Uppercase letters, spaces, and special characters are not allowed'
- );
- }
- if (file_exists(self::getGrav()['locator']->findResource('user://accounts/' . $value . YAML_EXT))) {
- throw new \RuntimeException(
- 'Username "' . $value . '" already exists, please pick another username'
- );
- }
- return $value;
- });
- $username = $helper->ask($this->input, $this->output, $question);
-
- // Get password and validate
- $password = $this->askForPassword($helper, 'Enter a password: ', function ($password1) use ($helper) {
- if (!preg_match('/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/', $password1)) {
- throw new \RuntimeException('Password must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters');
- }
- // Since input is hidden when prompting for passwords, the user is asked to repeat the password
- return $this->askForPassword($helper, 'Repeat the password: ', function ($password2) use ($password1) {
- if (strcmp($password2, $password1)) {
- throw new \RuntimeException('Passwords did not match.');
- }
- return $password2;
- });
- });
- $data['password'] = $password;
-
- // Get email and validate
- $question = new Question('Enter an email: ');
- $question->setValidator(function ($value) {
- if (!preg_match('/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/', $value)) {
- throw new \RuntimeException(
- 'Not a valid email address'
- );
- }
- return $value;
- });
- $data['email'] = $helper->ask($this->input, $this->output, $question);
-
- // Choose permissions
- $question = new ChoiceQuestion(
- 'Please choose a set of permissions:',
- array('a' => 'admin access', 's' => 'site access', 'b' => 'admin and site access'),
- 'a'
- );
- $question->setErrorMessage('permissions %s is invalid.');
- $permissions_choice = $helper->ask($this->input, $this->output, $question);
-
- switch ($permissions_choice) {
- case 'a':
- $data['access']['admin'] = ['login' => true, 'super' => true];
- break;
- case 's':
- $data['access']['site'] = ['login' => true];
- break;
- case 'b':
- $data['access']['admin'] = ['login' => true, 'super' => true];
- $data['access']['site'] = ['login' => true];
- }
-
- // Get fullname
- $question = new Question('Enter a fullname: ');
- $question->setValidator(function ($value) {
- if ($value === null || trim($value) == '') {
- throw new \RuntimeException(
- 'Fullname is required'
- );
- }
- return $value;
- });
- $data['fullname'] = $helper->ask($this->input, $this->output, $question);
-
- // Get title
- $question = new Question('Enter a title: ');
- $data['title'] = $helper->ask($this->input, $this->output, $question);
-
- // Create user object and save it
- $user = new User($data);
- $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
- $user->file($file);
- $user->save();
-
- $this->output->writeln('');
- $this->output->writeln('Success! User ' . $username . ' created.');
- }
-
- /**
- * Get password and validate.
- *
- * @param Helper $helper
- * @param string $question
- * @param callable $validator
- *
- * @return string
- */
- protected function askForPassword(Helper $helper, $question, callable $validator)
- {
- $question = new Question($question);
- $question->setValidator($validator);
- $question->setHidden(true);
- $question->setHiddenFallback(true);
- return $helper->ask($this->input, $this->output, $question);
+ $this->output->writeln('DEPRECATED COMMAND');
+ $this->output->writeln(' `bin/grav new-user` has been deprecated in favor of the new `bin/plugin admin new-user`');
}
}