From d604db471c9aa7070f500d79910dd0663386abab Mon Sep 17 00:00:00 2001 From: Iwona Just Date: Tue, 4 Apr 2023 09:02:20 +0100 Subject: [PATCH 1/3] check list of passed volumes on start indexing --- src/controllers/AssetIndexesController.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/controllers/AssetIndexesController.php b/src/controllers/AssetIndexesController.php index d487c7ddfe3..4525f37e7a3 100644 --- a/src/controllers/AssetIndexesController.php +++ b/src/controllers/AssetIndexesController.php @@ -8,11 +8,15 @@ namespace craft\controllers; use Craft; +use craft\base\Event; use craft\elements\Asset; use craft\errors\AssetException; +use craft\events\ListVolumesEvent; +use craft\helpers\ArrayHelper; use craft\helpers\Json; use craft\i18n\Locale; use craft\models\AssetIndexingSession; +use craft\utilities\AssetIndexes; use craft\web\Controller; use Throwable; use yii\web\BadRequestHttpException; @@ -58,6 +62,21 @@ public function actionStartIndexing(): Response return $this->asFailure(Craft::t('app', 'No volumes specified.')); } + // Fire a 'listVolumes' event + $event = new ListVolumesEvent([ + 'volumes' => Craft::$app->getVolumes()->getAllVolumes(), + ]); + Event::trigger(AssetIndexes::class, AssetIndexes::EVENT_LIST_VOLUMES, $event); + + // list of volumes passed from the request, cross-referenced against the list of volumes "allowed" via the event + $allowedVolumes = array_filter($volumes, function($volumeId) use ($event) { + return in_array($volumeId, ArrayHelper::getColumn($event->volumes, 'id')); + }); + + if (count($volumes) !== count($allowedVolumes)) { + return $this->asFailure(Craft::t('app', 'Selected volumes don’t match the allowed list.')); + } + $indexingSession = Craft::$app->getAssetIndexer()->startIndexingSession($volumes, $cacheRemoteImages, $listEmptyFolders); $sessionData = $this->prepareSessionData($indexingSession); From fadb0917fb85f2d1d21e18f27a8edf44bcd5d159 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 4 Apr 2023 08:24:20 -0700 Subject: [PATCH 2/3] Cleanup --- src/controllers/AssetIndexesController.php | 30 +++++++--------------- src/utilities/AssetIndexes.php | 19 +++++++++++--- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/controllers/AssetIndexesController.php b/src/controllers/AssetIndexesController.php index 4525f37e7a3..89a5a0fd569 100644 --- a/src/controllers/AssetIndexesController.php +++ b/src/controllers/AssetIndexesController.php @@ -8,14 +8,12 @@ namespace craft\controllers; use Craft; -use craft\base\Event; use craft\elements\Asset; use craft\errors\AssetException; -use craft\events\ListVolumesEvent; -use craft\helpers\ArrayHelper; use craft\helpers\Json; use craft\i18n\Locale; use craft\models\AssetIndexingSession; +use craft\models\Volume; use craft\utilities\AssetIndexes; use craft\web\Controller; use Throwable; @@ -54,30 +52,20 @@ public function beforeAction($action): bool public function actionStartIndexing(): Response { $request = Craft::$app->getRequest(); - $volumes = (array)$request->getRequiredBodyParam('volumes'); + $volumeIds = (array)$request->getRequiredBodyParam('volumes'); $cacheRemoteImages = (bool)$request->getBodyParam('cacheImages', false); $listEmptyFolders = (bool)$request->getBodyParam('listEmptyFolders', false); - if (empty($volumes)) { - return $this->asFailure(Craft::t('app', 'No volumes specified.')); - } - - // Fire a 'listVolumes' event - $event = new ListVolumesEvent([ - 'volumes' => Craft::$app->getVolumes()->getAllVolumes(), - ]); - Event::trigger(AssetIndexes::class, AssetIndexes::EVENT_LIST_VOLUMES, $event); + // Typecast volume IDs and filter out any disallowed volumes + $volumeIds = array_map(fn($volumeId) => (int)$volumeId, $volumeIds); + $allowedVolumeIds = array_map(fn(Volume $volume) => $volume->id, AssetIndexes::volumes()); + $volumeIds = array_intersect($volumeIds, $allowedVolumeIds); - // list of volumes passed from the request, cross-referenced against the list of volumes "allowed" via the event - $allowedVolumes = array_filter($volumes, function($volumeId) use ($event) { - return in_array($volumeId, ArrayHelper::getColumn($event->volumes, 'id')); - }); - - if (count($volumes) !== count($allowedVolumes)) { - return $this->asFailure(Craft::t('app', 'Selected volumes don’t match the allowed list.')); + if (empty($volumeIds)) { + return $this->asFailure(Craft::t('app', 'No volumes specified.')); } - $indexingSession = Craft::$app->getAssetIndexer()->startIndexingSession($volumes, $cacheRemoteImages, $listEmptyFolders); + $indexingSession = Craft::$app->getAssetIndexer()->startIndexingSession($volumeIds, $cacheRemoteImages, $listEmptyFolders); $sessionData = $this->prepareSessionData($indexingSession); $data = ['session' => $sessionData]; diff --git a/src/utilities/AssetIndexes.php b/src/utilities/AssetIndexes.php index 1fd98300c3b..26d15d1d494 100644 --- a/src/utilities/AssetIndexes.php +++ b/src/utilities/AssetIndexes.php @@ -12,6 +12,7 @@ use craft\events\ListVolumesEvent; use craft\helpers\Html; use craft\i18n\Locale; +use craft\models\Volume; use craft\web\assets\assetindexes\AssetIndexesAsset; use yii\base\Event; @@ -54,19 +55,29 @@ public static function iconPath(): ?string } /** - * @inheritdoc + * Returns all of the available volumes for indexing. + * + * @return Volume[] + * @since 4.4.6 */ - public static function contentHtml(): string + public static function volumes(): array { // Fire a 'listVolumes' event $event = new ListVolumesEvent([ 'volumes' => Craft::$app->getVolumes()->getAllVolumes(), ]); - Event::trigger(self::class, self::EVENT_LIST_VOLUMES, $event,); + Event::trigger(self::class, self::EVENT_LIST_VOLUMES, $event); + return $event->volumes; + } + /** + * @inheritdoc + */ + public static function contentHtml(): string + { $volumeOptions = []; - foreach ($event->volumes as $volume) { + foreach (static::volumes() as $volume) { $volumeOptions[] = [ 'label' => Html::encode($volume->name), 'value' => $volume->id, From 2adbc4506fe15ac53f90a49a39347bd2f2a87484 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 4 Apr 2023 08:30:22 -0700 Subject: [PATCH 3/3] Release notes [ci skip] --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4efa290eea8..68430ad023e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Selectize menus now expand upwards when there’s not ample space below them. ([#12976](https://github.com/craftcms/cms/issues/12976)) - Element index bulk action spinners are now centered on the viewport. ([#12972](https://github.com/craftcms/cms/issues/12972)) - All control panel errors are new presented via error notifications rather than browser alerts. ([#13024](https://github.com/craftcms/cms/issues/13024)) +- Added `craft\utilities\AssetIndexes::volumes()`. +- `craft\controllers\AssetIndexesController::actionStartIndexing()` now cross-references the selected volumes with those allowed by `craft\utilities\AssetIndexes::EVENT_LIST_VOLUMES` event handlers. ([#13039](https://github.com/craftcms/cms/pull/13039), [#12819](https://github.com/craftcms/cms/pull/12819)) - Fixed a bug where Assets fields weren’t respecting their View Mode setting when viewing entry revisions. ([#12948](https://github.com/craftcms/cms/issues/12948)) - Fixed a bug where asset pagination was broken when there was more than 100 subfolders. ([#12969](https://github.com/craftcms/cms/issues/12969)) - Fixed a bug where entry index pages’ “Revision Notes” and “Last Edited By” columns weren’t getting populated for disabled entries. ([#12981](https://github.com/craftcms/cms/issues/12981))