Replies: 2 comments 3 replies
-
This is expected behavior. Roles have permissions; users with those roles gain access to those permissions indirectly. If you wish to check for permissions via roles, use the |
Beta Was this translation helpful? Give feedback.
-
Can you create a fresh app in a public repo with the bare minimum of code required to recreate this problem? The process of creating a new app to demonstrate the problem often helps expose the reason behind the problem. :) |
Beta Was this translation helpful? Give feedback.
-
Hello all,
I've got an issue when I try to get the user's permissions from the User class. I have a User model which extends the Authenticatable and uses the
HasRoles
trait as per the documentation. There are no guards set in this class and the default isweb
in the auth config which is correct.I've got a database seeder which will create two roles ("Administrator" and "Editor") and some permissions that get assigned to the roles. We also create a couple test users and assign them those roles so we have an admin user and an editor user.
When using the User class (via
Auth::user()
) I can call any of the role methods likehasRole
and I will get the expected results. I can also access theroles
property and it returns the role of the user.My issue arrives when I try to use any of the
Permission
methods likegetAllPermissions
. It returns empty and thehasPermissionTo
will return false when checking the permission of theAuth::user()
.I know actual code is always helpful to see but I'm not sure which files would be the most useful to display without displaying a whole ton of them so I'll wait for feedback and see what files you would like me to display.
Now onto what I've looked into and my findings so far.
First, when I pull a role from the instance of our Role model (which extends the Spatie model Role) and check the
permissions
property it returns the full list of permissions associated with that role as expected. So there doesn't seem to be an issue with the association between roles and permissions.The above gives the following:
I've also been using the
permission
middleware (which is my end goal) and it always returns asUser does not have the right permissions.
. If I add the additional exception display, it says the user doesn't have the permission which I triple checked and they do in fact have that permission based off of their role.Bonus: I've also reset all my caches both when doing a fresh seeding as well as with the
php artisan
command.Any ideas or thoughts on what the cause could be? I'm newer to both Laravel and Spatie so I'm not too sure where to look next. It seems that the permissions aren't mapping through the user's role but I don't understand why this would be the case when they do map through the Role model itself.
Beta Was this translation helpful? Give feedback.
All reactions