From e2477b6b9cd6bc67b347602b73bb932ea2cbc1a9 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Tue, 1 Oct 2024 13:39:12 -0400 Subject: [PATCH] [5.x] Improve `AssetFolderPolicy` performance (#10868) --- src/Policies/AssetFolderPolicy.php | 43 +++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Policies/AssetFolderPolicy.php b/src/Policies/AssetFolderPolicy.php index 287e8b8a47..f977508f84 100644 --- a/src/Policies/AssetFolderPolicy.php +++ b/src/Policies/AssetFolderPolicy.php @@ -2,6 +2,8 @@ namespace Statamic\Policies; +use Illuminate\Support\Facades\Gate; +use Statamic\Contracts\Assets\Asset as AssetContract; use Statamic\Facades\User; class AssetFolderPolicy @@ -25,10 +27,14 @@ public function move($user, $assetFolder) return false; } - return $assetFolder - ->assets(true) - ->reject(fn ($asset) => $user->can('move', $asset)) - ->isEmpty(); + if ($this->isUsingCustomAssetPolicy()) { + return $assetFolder + ->assets(true) + ->reject(fn ($asset) => $user->can('move', $asset)) + ->isEmpty(); + } + + return $assetFolder->container()->allowMoving(); } public function rename($user, $assetFolder) @@ -39,10 +45,14 @@ public function rename($user, $assetFolder) return false; } - return $assetFolder - ->assets(true) - ->reject(fn ($asset) => $user->can('rename', $asset)) - ->isEmpty(); + if ($this->isUsingCustomAssetPolicy()) { + return $assetFolder + ->assets(true) + ->reject(fn ($asset) => $user->can('rename', $asset)) + ->isEmpty(); + } + + return $assetFolder->container()->allowRenaming(); } public function delete($user, $assetFolder) @@ -53,9 +63,18 @@ public function delete($user, $assetFolder) return false; } - return $assetFolder - ->assets(true) - ->reject(fn ($asset) => $user->can('delete', $asset)) - ->isEmpty(); + if ($this->isUsingCustomAssetPolicy()) { + return $assetFolder + ->assets(true) + ->reject(fn ($asset) => $user->can('delete', $asset)) + ->isEmpty(); + } + + return true; + } + + protected function isUsingCustomAssetPolicy() + { + return Gate::policies()[AssetContract::class] !== AssetPolicy::class; } }