Skip to content

Commit

Permalink
Merge pull request #16148 from craftcms/feature/cms-1348-resaveall-to…
Browse files Browse the repository at this point in the history
…-check-if-provided-options-are-supported

check if options passed to `resave/all` are supported by subcommands
  • Loading branch information
brandonkelly authored Nov 19, 2024
2 parents 5f72aba + 862109e commit 46d941c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed an error that could occur if an invalid folder ID was passed to `craft\services\Assets::deleteFoldersByIds()`. ([#16147](https://github.com/craftcms/cms/pull/16147))
- Fixed a SQL error that occurred when creating a new Single section. ([#16145](https://github.com/craftcms/cms/issues/16145))
- Fixed an error that occurred when running the `resave/all` command, if any of the options passed weren’t supported by other `resave/*` commands. ([#16148](https://github.com/craftcms/cms/pull/16148))
- Fixed an RCE vulnerability.

## 5.5.1.1 - 2024-11-18
Expand Down
74 changes: 72 additions & 2 deletions src/console/controllers/ResaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use craft\events\MultiElementActionEvent;
use craft\helpers\Console;
use craft\helpers\ElementHelper;
use craft\helpers\Inflector;
use craft\helpers\Queue;
use craft\helpers\StringHelper;
use craft\models\CategoryGroup;
Expand Down Expand Up @@ -373,11 +374,41 @@ public function actionAll(): int
array_push($actions, ...array_keys($this->actions()));

$params = $this->getPassedOptionValues();
$actionsToSkip = [];

// check if all actions support all the params
foreach ($actions as $key => $id) {
if (!$this->doesActionSupportsAllOptions($id, $params)) {
$actionsToSkip[] = $id;
unset($actions[$key]);
}
}

// ask for confirmation
if ($this->interactive && !empty($actionsToSkip)) {
$this->output('The following commands don’t support the provided options, and will be skipped:', Console::FG_YELLOW);
foreach ($actionsToSkip as $id) {
$invalidParams = array_map(
fn($param) => sprintf('`--%s`', StringHelper::toKebabCase($param)),
$this->getUnsupportedOptions($id, $params)
);
$this->output(' ' . $this->markdownToAnsi(sprintf(
'- `resave/%s` doesn’t support %s',
$id,
Inflector::sentence($invalidParams)
)));
}
Console::outdent();
if (!$this->confirm('Continue?', true)) {
return ExitCode::OK;
}
}

// run the actions which support all the params
foreach ($actions as $id) {
try {
$this->output();
$this->do("Running `resave/$id`", function() use ($id, $params) {
$this->output();
Console::indent();
try {
$this->runAction($id, $params);
Expand Down Expand Up @@ -802,7 +833,46 @@ private function _resaveElements(ElementQueryInterface $query): int

$label = isset($this->propagateTo) ? 'propagating' : 'resaving';
$this->output("Done $label $elementsText.", Console::FG_YELLOW);
$this->output();
return $fail ? ExitCode::UNSPECIFIED_ERROR : ExitCode::OK;
}

/**
* Returns whether all options passed to an action are supported.
* Used by resave/all command.
*
* @param string $actionId
* @param array $params
* @return bool
*/
private function doesActionSupportsAllOptions(string $actionId, array $params): bool
{
$options = $this->options($actionId);
foreach ($params as $param => $value) {
if (!in_array($param, $options)) {
return false;
}
}

return true;
}

/**
* Returns an array of options that are not supported by the action.
*
* @param string $actionId
* @param array $params
* @return array
*/
private function getUnsupportedOptions(string $actionId, array $params): array
{
$unsupportedParams = [];
$options = $this->options($actionId);
foreach ($params as $param => $value) {
if (!in_array($param, $options)) {
$unsupportedParams[] = $param;
}
}

return $unsupportedParams;
}
}

0 comments on commit 46d941c

Please sign in to comment.