statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.09k stars 534 forks source link

[5.x] Fix error when using user ID as folder name for avatar asset field in non-admin context #11141

Closed jonasemde closed 1 day ago

jonasemde commented 3 days ago

Problem

When configuring the assets field in the User Blueprint to use the user ID as the folder name, non-admin users encounter an error. This happens because the first() method can return false when no matching action is found, causing the subsequent call to toArray() to fail.

Root Cause

The issue arises from the following code:

$action = Action::for($assetFolder, [
    'container' => $container->handle(),
    'folder' => $folder,
])->first(fn ($action) => get_class($action) === RenameAssetFolder::class)->toArray();

If no matching RenameAssetFolder action is found, first() returns false. The toArray() method is then called on this false value, leading to an error.

Solution

The fix ensures that the first() method's return value is properly handled using the null-safe operator ?-> before attempting to call toArray():

$action = Action::for($assetFolder, [
    'container' => $container->handle(),
    'folder' => $folder,
])->first(fn ($action) => get_class($action) === RenameAssetFolder::class)?->toArray();

This prevents the error when no matching action is found and maintains compatibility for non-admin users.