Merge branch 'develop' of https://github.com/getgrav/grav into develop

This commit is contained in:
Andy Miller
2015-11-18 17:13:31 -07:00
2 changed files with 16 additions and 125 deletions

View File

@@ -43,7 +43,8 @@ $grav['streams'];
$grav['plugins']->init();
$grav['themes']->init();
$app = new Application('Grav Plugins Commands', GRAV_VERSION);
$app = new Application('Grav Plugins Commands', GRAV_VERSION);
$pattern = '/([A-Z]\w+Command\.php)$/usm';
// get arguments and strip the application name
if (null === $argv) {
@@ -69,17 +70,23 @@ if (!$name) {
$output->writeln('');
$output->writeln("<red>Example:</red>");
$output->writeln(" {$bin} error log -l 1 --trace");
$list = Folder::all('plugins://', ['compare' => 'Pathname', 'pattern' => '/\/cli\/([A-Z][a-z]+Command\.php)$/usm']);
$list = Folder::all('plugins://', ['compare' => 'Pathname', 'pattern' => '\/cli\/' . $pattern]);
if (count($list)) {
$available = [];
$output->writeln('');
$output->writeln('<red>Plugins with CLI available:</red>');
foreach($list as $index => $entry) {
foreach ($list as $index => $entry) {
$split = explode('/', $entry);
$entry = array_shift($split);
$index = str_pad($index++ + 1, 2, '0', STR_PAD_LEFT);
$output->writeln(' ' . $index . ". <red>" . str_pad($entry, 15) . "</red> <white>${bin} ${entry} list</white>");
if (in_array($entry, $available)) {
continue;
}
$available[] = $entry;
$output->writeln(' ' . $index . ". <red>" . str_pad($entry, 15) . "</red> <white>${bin} ${entry} list</white>");
}
}
@@ -94,7 +101,7 @@ if ($plugin === null) {
$path = 'plugins://' . $name . '/cli';
try {
$commands = Folder::all($path, ['compare' => 'Filename', 'pattern' => '/^([A-Z][a-z]+Command\.php)$/usm']);
$commands = Folder::all($path, ['compare' => 'Filename', 'pattern' => $pattern]);
} catch (\RuntimeException $e) {
$output->writeln("<red>No Console Commands for <white>'{$name}'</white> where found in <white>'{$path}'</white></red>");
exit;

View File

@@ -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 <info>newuser</info> creates a new user file in user/accounts/ folder')
->setDescription('DEPRECATED: Creates a new user')
->setHelp('The <info>new-user</info> 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('<green>Create new user</green>');
$this->output->writeln('');
// Get username and validate
$question = new Question('Enter a <yellow>username</yellow>: ', '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 <yellow>password</yellow>: ', 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 <yellow>password</yellow>: ', 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 <yellow>email</yellow>: ');
$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 <yellow>permissions</yellow>:',
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 <yellow>fullname</yellow>: ');
$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 <yellow>title</yellow>: ');
$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('<green>Success!</green> User <cyan>' . $username . '</cyan> 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('<red>DEPRECATED COMMAND</red>');
$this->output->writeln(' <white>`bin/grav new-user`</white> has been <red>deprecated</red> in favor of the new <white>`bin/plugin admin new-user`</white>');
}
}