0

I host a SPA with a Laravel API on AWS (EC2). When you fill out this form: http://ec2-52-59-214-55.eu-central-1.compute.amazonaws.com/register (feel free to do so), the server throws an error with a response code of 500, right at the point where a $user->can("...") method is called. I developed the app localy and everything worked fine there.

Local environment:

Windows 10 / XAMPP - Apache 2.4.41
PHP 7.4.4
Laravel 7.5.2

Server environment:

Ubuntu 18.04 / Apache 2.4.29
PHP 7.4.6
Laravel 7.5.2

It uses spatie/laravel-permission for role management, which also affects the can method provided by the Laravel framework.

Here is the code causing the error:

echo "Before store<br>";

        if($user->can("store files")) {                                                                                                                                                                                                                      echo "Inner store";

            // Store avatar
            if(isset($data["avatar"])) {
                if(!$data["avatar"]->isValid()) {
                    return reponse(null, 422);
                }

                // Check if avatar is new
                $current_avatar = $user->getAvatarAttribute();
                $store_images = get_new_files([$data["avatar"]], [$current_avatar]);


                if(isset($store_images[0])) {
                    // Create asset for avatar
                    $new_avatar = create_asset([
                        "file" => $store_images[0],
                        "user_id" => $user->id,
                        "type" => "avatar"
                    ]);

                    $new_avatar->save();
                }
            }
        }

        echo "After store<br>";

And here the error which I guess means, that the script failed as soon as if($user->can("...)) got called.

Do you have any idea how to fix that?

Tracer69
  • 1,050
  • 1
  • 11
  • 26
  • What does `dd($user->can('store files'))` return? – Spholt May 17 '20 at 11:35
  • 1
    Please post code as text not images so we can debugge it properly – Makdous May 17 '20 at 11:36
  • @Makdous I have pasted the code – Tracer69 May 17 '20 at 12:06
  • @Spholt The same error is thrown and it returns nothing – Tracer69 May 17 '20 at 12:07
  • OK, can you try `dd($user)` please? If it returns a user instance, then the problem is likely in your policies or middleware. We can then move on from there – Spholt May 17 '20 at 12:34
  • @Spholt yap, it returns the user instance – Tracer69 May 17 '20 at 12:42
  • Ok, the problem is almost certainly with your policies. See if you can find the 500 error in your logs (`storage/logs/`) to point you at the correct file and then post the error ;) – Spholt May 17 '20 at 12:47
  • The only file there is ``laravel.log`` and the error does not show up there – Tracer69 May 17 '20 at 12:51
  • I discovered that when I turn on debug mode, I get this error: UnexpectedValueException: The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in file /var/www/html/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 110 @Spholt – Tracer69 May 17 '20 at 13:05
  • Okay, thanks for the help :D This answer fixed it https://stackoverflow.com/questions/23411520/how-to-fix-error-laravel-log-could-not-be-opened – Tracer69 May 17 '20 at 13:09

1 Answers1

0

Logging errors are fairly common, especially when configured as a daily log.

https://laravel.com/docs/7.x/logging#configuration

Specifically look at the section on "Configuring The Single and Daily Channels" as this allows the permissions of the log file to be set on creation.

Temporarily, you can change the permissions for you logs like so:

sudo chmod 644 storage/log/.
Spholt
  • 3,724
  • 1
  • 18
  • 29