Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

Relation to user in EntrustRoleTrait needs an unmentioned modification in config/auth.php #531

Open jasper-zsh opened 8 years ago

jasper-zsh commented 8 years ago

My entrust version is 5.2.x-dev. I got an exception when using barravdh/laravel-ide-helper running php artisan ide-helper:model. [Symfony\Component\Debug\Exception\FatalThrowableError] Fatal error: Class name must be a valid object or a string Then I used -v to findout the problem and looked back to sources of entrust, found that in line 48 in EntrustRoleTrait.php it passed Config::get('auth.model') as first arg to belongsToMany, but there's no property model in config/auth.php in Laravel 5.2. I noticed that the document asked me to set property in config/auth.php in section Configuration but did not tell me what the property is. So is it a bug of entrust or just a lack of the document?

kp77 commented 8 years ago

There are other places in Entrust where this config property is needed. For me soft deleting was not working as it should because this property was not set (EntrustUserTrait.php line 67).

I believe this should be added to the documentation. Adding the model property to the auth config like this solved it for me:

'model' => App\User::class,

darrensapalo commented 8 years ago

The configuration keys it looks for is defined by the Zizaco/Entrust version. I figured out exactly what it was looking for by first looking at what line of code it fails. If you look at your storage/logs/laravel.log, you'll see that the last error shown is the failed migration. In my case it shows the following:

[2016-05-23 09:40:31] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class name must be a valid object or a string' in /Users/darrenkarlsapalo/git/formdev-portal/vendor/zizaco/entrust/src/commands/MigrationCommand.php:86
Stack trace:
#0 {main}  

By reading the stacktrace, it points me to look at vendor/zizaco/entrust/src/commands/MigrationCommand.php at line 86.

My inference is that the Laravel structure of configuration keys defined in config/auth.php varies depending on the Laravel version, but the zizaco/entrust plugin had an assumption as to what to look for, based on a certain Laravel version. If you check MigrationCommand.php, you'll find the following in lines 84-86:

$usersTable  = Config::get('auth.providers.users.table');
$userModel   = Config::get('auth.providers.users.model');
$userKeyName = (new $userModel())->getKeyName();

The structure expects that in your auth.php to have an array called providers, which has an array called users, which has the model configuration key-value pair.

Using tapos ghosh's solution, the structure which zizaco/entrust expects can be added.

Attached below is the code he suggests you place in your auth.php.

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table' => 'users',
    ],
],
rscotten commented 7 years ago

@Zizaco Please fix this Config::get('auth.model') in EntrustRoleTrait