Skip to content

Commit

Permalink
[multisite:new] Validated uri name (#4039)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuarez20 authored and enzolutions committed May 16, 2019
1 parent a122272 commit 039a064
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Command/Multisite/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Drupal\Console\Command\Multisite;

use Drupal\Console\Core\Command\Command;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -25,14 +26,21 @@ class NewCommand extends Command
{
protected $appRoot;

/**
* @var Validator
*/
protected $validator;

/**
* DebugCommand constructor.
*
* @param $appRoot
* @param Validator $validator
*/
public function __construct($appRoot)
public function __construct($appRoot, Validator $validator)
{
$this->appRoot = $appRoot;
$this->validator = $validator;
parent::__construct();
}

Expand Down Expand Up @@ -80,6 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->fs = new Filesystem();
$this->directory = $input->getArgument('directory');
$uri = $this->validator->validateUriName($input->getArgument('uri'));

if (!$this->directory) {
$this->getIo()->error($this->trans('commands.multisite.new.errors.subdir-empty'));
Expand Down Expand Up @@ -117,7 +126,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$uri = $input->getArgument('uri');
try {
$this->addToSitesFile($uri);
} catch (\Exception $e) {
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Validator
const REGEX_CONTROLLER_CLASS_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+Controller$/';
const REGEX_MACHINE_NAME = '/^[a-z0-9_]+$/';
const REGEX_DEPENDENCY_NAME = '/^[a-z0-9_:]+$/';
const REGEX_URI_NAME = '/^[a-z0-9_.]+$/';
// This REGEX remove spaces between words
const REGEX_REMOVE_SPACES = '/[\\s+]/';
// Max length to 32
Expand Down Expand Up @@ -66,6 +67,20 @@ public function validateClassName($class_name)
}
}

public function validateUriName($uri_name)
{
if (preg_match(self::REGEX_URI_NAME, $uri_name)) {
return $uri_name;
} else {
throw new \InvalidArgumentException(
sprintf(
'Uri name "%s" is invalid, it must starts with a letter, followed by any number of letters, numbers, or underscores.',
$uri_name
)
);
}
}

public function validateBundleTitle($bundle_title)
{
if (!empty($bundle_title)) {
Expand Down
2 changes: 1 addition & 1 deletion uninstall.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ services:
- { name: drupal.command }
console.multisite_new:
class: Drupal\Console\Command\Multisite\NewCommand
arguments: ['@app.root']
arguments: ['@app.root', '@console.validator']
tags:
- { name: drupal.command }
console.multisite_update:
Expand Down

0 comments on commit 039a064

Please sign in to comment.