spatie / laravel-permission

Associate users with roles and permissions
https://spatie.be/docs/laravel-permission
MIT License
12.18k stars 1.78k forks source link

Use middleware Permission error #1281

Closed mohuidong closed 5 years ago

mohuidong commented 5 years ago

Use middleware permission error The first time I use this package, when I use the role in the router middleware, there is no problem, but I will use the permission to report the errorMethod Illuminate\Database\Query\Builder::can does not exist. Why?

drbyte commented 5 years ago

Method Illuminate\Database\Query\Builder::can does not exist. occurs when you call can on a query object (not yet retrieved) instead of on a User object.

Can you please show your code?

mohuidong commented 5 years ago

Method Illuminate\Database\Query\Builder::can does not exist.

My BaseModel extends Illuminate\Database\Eloquent\Model

Class AdminModel extends BaseModel implements AuthenticatableContract, JWTSubject
{
     Use SoftDeletes, Authenticatable, HasRoles;
     ...
}
 $api->group(['prefix' => 'orders', 'middleware' => ['permission:orders']], function ($api) {
            $api->get('/', 'Orders@index');
        });
drbyte commented 5 years ago

NOTE: Laravel requires that your model must implement Authorizable in order to offer can support, not just Authenticatable.

eg: use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract

mohuidong commented 5 years ago

NOTE: Laravel requires that your model must implement Authorizable in order to offer can support, not just Authenticatable.

eg: use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract

thank you very much

mohuidong commented 5 years ago

NOTE: Laravel requires that your model must implement Authorizable in order to offer can support, not just Authenticatable.

eg: use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract

User does not have the right permissions. There is still a small problem, I use Illuminate\Foundation\Auth\Access\Authorizable in the model; However, when the initialization is added to the role, the model also adds the role, but the model_has_permissions table has no data, whether it causes the permission middleware to use app('auth')->user()->can($permission) to determine return false?And I use app('auth')->user()->hasPermissionTo($permission)to return true.

drbyte commented 5 years ago

User does not have the right permissions.

Please set display_permission_in_exception to true in your permissions config file, so that the exception shows which permission is being requested. https://github.com/spatie/laravel-permission/blob/753ce00d91bb2b5a2f03ecb7d1740de29c0a24fa/config/permission.php#L93

Also, are you using "only" the api guard? If so, is api the first one listed in your auth.php config file? If not, make it the first one.

kievo23 commented 5 years ago

Route::resource('messages','SMSController')->middleware('permission:logs'); When trying to use the permissions middleware i get the following error

[Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Call to a member function contains() on null"

Stacktrace:
#62 Symfony\Component\Debug\Exception\FatalThrowableError in /var/www/html/twigadashboard/vendor/spatie/laravel-permission/src/Traits/HasPermissions.php:258
#61 App\User:hasDirectPermission in /var/www/html/twigadashboard/vendor/spatie/laravel-permission/src/Traits/HasPermissions.php:141
#60 App\User:hasPermissionTo in /var/www/html/twigadashboard/vendor/spatie/laravel-permission/src/PermissionRegistrar.php:103
#59 Spatie\Permission\PermissionRegistrar:Spatie\Permission\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php:473
#58 Illuminate\Auth\Access\Gate:callBeforeCallbacks in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php:348
#57 Illuminate\Auth\Access\Gate:raw in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php:278
#56 Illuminate\Auth\Access\Gate:Illuminate\Auth\Access\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Support/Collection.php:512
#55 Illuminate\Support\Collection:every in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php:282
#54 Illuminate\Auth\Access\Gate:check in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Auth/Access/Authorizable.php:18
#53 Illuminate\Foundation\Auth\User:can in /var/www/html/twigadashboard/vendor/spatie/laravel-permission/src/Middlewares/PermissionMiddleware.php:21
#52 Spatie\Permission\Middlewares\PermissionMiddleware:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#51 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#50 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
#49 Illuminate\Routing\Middleware\SubstituteBindings:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#48 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#47 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:75
#46 Illuminate\Foundation\Http\Middleware\VerifyCsrfToken:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#45 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#44 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Session/Middleware/AuthenticateSession.php:39
#43 Illuminate\Session\Middleware\AuthenticateSession:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#42 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#41 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:43
#40 Illuminate\Auth\Middleware\Authenticate:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#39 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#38 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
#37 Illuminate\View\Middleware\ShareErrorsFromSession:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#36 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#35 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:56
#34 Illuminate\Session\Middleware\StartSession:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#33 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#32 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37
#31 Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#30 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#29 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:66
#28 Illuminate\Cookie\Middleware\EncryptCookies:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#27 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#26 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
#25 Illuminate\Pipeline\Pipeline:then in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Router.php:682
#24 Illuminate\Routing\Router:runRouteWithinStack in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Router.php:657
#23 Illuminate\Routing\Router:runRoute in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Router.php:623
#22 Illuminate\Routing\Router:dispatchToRoute in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Router.php:612
#21 Illuminate\Routing\Router:dispatch in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
#20 Illuminate\Foundation\Http\Kernel:Illuminate\Foundation\Http\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
#19 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
#18 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#17 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#16 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
#15 Illuminate\Foundation\Http\Middleware\TransformsRequest:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#14 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#13 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
#12 Illuminate\Foundation\Http\Middleware\ValidatePostSize:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#11 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#10 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:62
#9 Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#8 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#7 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/fideloper/proxy/src/TrustProxies.php:57
#6 Fideloper\Proxy\TrustProxies:handle in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
#5 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
#4 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
#3 Illuminate\Pipeline\Pipeline:then in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
#2 Illuminate\Foundation\Http\Kernel:sendRequestThroughRouter in /var/www/html/twigadashboard/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
#1 Illuminate\Foundation\Http\Kernel:handle in /var/www/html/twigadashboard/public/index.php:55
#0 require_once in /var/www/html/twigadashboard/server.php:21
drbyte commented 5 years ago

[Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Call to a member function contains() on null"

Sounds like you have a $permissions property on your User model, which is conflicting with the permissions relation used by this package.

kievo23 commented 5 years ago

[Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Call to a member function contains() on null"

Sounds like you have a $permissions property on your User model, which is conflicting with the permissions relation used by this package.

Thanks that solved my problem.