outl1ne / nova-notes-field

This Laravel Nova package adds a notes field to Nova's arsenal of fields.
MIT License
51 stars 36 forks source link

Bug with Auth::user() in addNote class #73

Open mateusz-peczkowski opened 4 months ago

mateusz-peczkowski commented 4 months ago

Hi.

I found a bug in version 3.1.0 (latest). Don't know if this was on old releases as well.

The issue is with column "created_by".

This column in DB is filled only when default guard === nova guard for auth :) I had different for frontend and different for nova and that's why I found this issue.

In Models/Note.php you have:

public function createdBy()
    {
        $provider = 'users';
        if (config('nova.guard')) $provider = config('auth.guards.' . config('nova.guard') . '.provider');
        $userClass = config('auth.providers.' . $provider . '.model');
        return $this->belongsTo($userClass, 'created_by');
    }

which is OK but then under trait HasNotes you are using:

$user = $user ? Auth::user() : null;
        return $this->notes()->create([
            'text' => $note,
            'created_by' => isset($user) ? $user->id : null,
            'system' => $system,
        ]);

Without providing guard Auth is returning default, in my case it was api while nova auth was web. It should be rather Auth::guard(config('nova.guard'))->user()

Steps to reproduce:

mateusz-peczkowski commented 4 months ago

I made a fork and submitted pull request for this fix #74