WildsideUK / Laravel-Userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created_by` and `updated_by` columns on your model, populated by the currently authenticated user in your application.
https://wildside.uk
MIT License
570 stars 64 forks source link

User class not found #29

Closed tkayfun closed 5 years ago

tkayfun commented 5 years ago

Hi WildSideUK,

Tayfun here again!

I've found a bug in the getUserClass function, when using this via API.

I got the following error:

 "message": "Call to a member function getModel() on null",
    "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
    "file": "/home/vagrant/code/dw-client-portal/vendor/wildside/userstamps/src/Userstamps.php",
    "line": 151,

The function (now):

protected function getUserClass()
    {
        if (get_class(auth()) === 'Illuminate\Auth\Guard') {
            return auth() -> getProvider() -> getModel();
        }

        return auth() -> guard() -> getProvider() -> getModel();
    }

I've changed it to:

protected function getUserClass()
    {
        if(class_exists('App\\User')) {
            return \App\User::class;
        }

        return config('auth.providers.users.model');
    }

But when aa user has a User class which is not the default User model, it will conflict. So I've changed it to: (so it always takes from your auth.php)

protected function getUserClass()
    {
        return config('auth.providers.users.model');
    }

As far as I know (Laravel > 5.*), default User class is in the namespace App, otherwise its configured in your auth.php. So what I've did is a check on class App\\User, and otherwise it takes the class from your auth.php. But this was not a good solution since a developer can also have a User model in their App namespace which is not their default User model, so I've changed it to take it from your config file called auth.php

I think this is a better way to get the Auth Provider class. I'll make a PR in a bit.

Looking forward to your reply!

Cheers,

Tayfun Kayahan

mattmcdonald-uk commented 5 years ago

This will only work in 5.2+, but 5.0 and 5.1 are very old now, so I've updated dependencies to 5.2+.

Will put out a new release soon.