From 7abb8688dd4ae81a436c85ed9cf3f61e382e3b9a Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Fri, 3 Jan 2025 22:04:13 -0800 Subject: [PATCH 1/2] Avoid custom field validation from users/create --- CHANGELOG.md | 4 ++++ src/console/controllers/UsersController.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64a1d148713..5f888276837 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Craft CMS 4 +## Unreleased + +- Fixed a bug where custom fields could cause validation errors when running the `users/create` command. + ## 4.13.8 - 2025-01-02 - Fixed a bug where asset edit page URLs contained spaces if the asset filename contained spaces. ([#15236](https://github.com/craftcms/cms/issues/15236)) diff --git a/src/console/controllers/UsersController.php b/src/console/controllers/UsersController.php index a80b7b575c7..3ff7b1a7a9d 100644 --- a/src/console/controllers/UsersController.php +++ b/src/console/controllers/UsersController.php @@ -176,7 +176,7 @@ public function actionCreate(): int $user = new User($attributesFromArgs); - if (!$user->validate(array_keys($attributesFromArgs))) { + if (!empty($attributesFromArgs) && !$user->validate(array_keys($attributesFromArgs))) { $this->stderr('Invalid arguments:' . PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $user->getErrorSummary(true)) . PHP_EOL, Console::FG_RED); return ExitCode::USAGE; } @@ -218,7 +218,7 @@ public function actionCreate(): int $this->stdout('Saving the user ... '); - if (!Craft::$app->getElements()->saveElement($user)) { + if (!Craft::$app->getElements()->saveElement($user, false)) { $this->stderr('failed:' . PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $user->getErrorSummary(true)) . PHP_EOL, Console::FG_RED); return ExitCode::USAGE; From 9c2ab9aafd5016c032e76685c1c296af4de79872 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Sat, 4 Jan 2025 10:57:31 -0800 Subject: [PATCH 2/2] Fully, recursively delete asset data when deleting a volume folder --- CHANGELOG.md | 1 + src/services/Assets.php | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f888276837..4c176861530 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed a bug where custom fields could cause validation errors when running the `users/create` command. +- Fixed a bug where deleting a volume folder wasn’t fully deleting asset data in descendant folders. ## 4.13.8 - 2025-01-02 diff --git a/src/services/Assets.php b/src/services/Assets.php index a4bb7f9cfce..c51b7490960 100644 --- a/src/services/Assets.php +++ b/src/services/Assets.php @@ -306,7 +306,7 @@ public function renameFolderById(int $folderId, string $newName): string */ public function deleteFoldersByIds(int|array $folderIds, bool $deleteDir = true): void { - $folders = []; + $allFolderIds = []; foreach ((array)$folderIds as $folderId) { $folder = $this->getFolderById((int)$folderId); @@ -314,8 +314,11 @@ public function deleteFoldersByIds(int|array $folderIds, bool $deleteDir = true) continue; } - $folders[] = $folder; + $allFolderIds[] = $folder->id; + $descendants = $this->getAllDescendantFolders($folder, withParent: false); + array_push($allFolderIds, ...array_map(fn(VolumeFolder $folder) => $folder->id, $descendants)); + // Delete the directory on the filesystem if ($folder->path && $deleteDir) { $volume = $folder->getVolume(); try { @@ -327,25 +330,18 @@ public function deleteFoldersByIds(int|array $folderIds, bool $deleteDir = true) } } - /** @var Asset[] $assets */ - $assets = Asset::find()->folderId($folderIds)->all(); - + // Delete the elements + $assetQuery = Asset::find()->folderId($allFolderIds); $elementService = Craft::$app->getElements(); - foreach ($assets as $asset) { + foreach ($assetQuery->each() as $asset) { + /** @var Asset $asset */ $asset->keepFileOnDelete = !$deleteDir; $elementService->deleteElement($asset, true); } - foreach ($folders as $folder) { - $descendants = $this->getAllDescendantFolders($folder); - usort($descendants, static fn($a, $b) => substr_count($a->path, '/') < substr_count($b->path, '/')); - - foreach ($descendants as $descendant) { - VolumeFolderRecord::deleteAll(['id' => $descendant->id]); - } - VolumeFolderRecord::deleteAll(['id' => $folder->id]); - } + // Delete the folder records + VolumeFolderRecord::deleteAll(['id' => $allFolderIds]); } /**