Skip to content

Commit

Permalink
[5.x] Improve AssetFolderPolicy performance (#10868)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite authored Oct 1, 2024
1 parent d9d6b20 commit e2477b6
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/Policies/AssetFolderPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}
}

0 comments on commit e2477b6

Please sign in to comment.