-
Hi, I'm using
In laravel-multitenancy I use separate databases for landlord and tenants. In laravel-permission I created roles and permissions with the respective guard_names of landlord and tenants. These are available in the respective databases. There also permissions are assigned to roles. In Filament I use two panels. One for the landlord, one for the tenants. In each of them I set up a UserResource to manage the respective users. Landlord panel and tenant panel each have their own code (User Resource, policies) and models. The models use the respective database-traits for landlord and tenants. In the landlord panel everything works fine. The Active User gets access to the UserResource according to the set permissions (e.g. list and view, but not edit and delete). When I change the permissions the panel responds to the changes as expected. In the tenant panel however access to the UserResource is declined, although in the tenant database the appropriate permission is set for the role User and the Active User has the role User. Adding a direct permission to the user didn't change anything. When I remove the UserPolicy of the tenant, access is granted and the correct tenant-database is used (which can be judged based on the content). In order to investigate the problem I activated the UserPolicy again. Next I checked if in viewAny() the correct tenant-user is known. This is the case: Next I checked for the permissions of the user knonw at viewAny(). list.users is amongst them: So far there seems to be no reason, why access to the user list should be declined. But still $user->can('list.users') returns false. As it turned out $user->hasPermissionTo('list.users') queries the landlord database instead of the tenant database. The list.users permission that exists there is bound to the landlord guard_name and therefore does not match. When I add the same permission with the tenants guard_name in the landlord database, the exception disappears, but access is still declined. If I add this new tenant-permission to the User role int the landlord database, access is granted. So, for any reasons that I do not understand, $user->getAllPermissions() returns the correct result from the tenant-database while $user->hasPermissionTo() and $user->can() return an incorrect result from the landlord-database, while acknowledging in the log that "tenantId":1 is active. All of this happens immediately one after the other in viewAny() in the UserPolicy. Honestly, I have no idea why this happens and how to investigate it further. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
A longer Xdebug-Session confirmed, that the two methods of the HasPermission trait (part of laravel-permissons) getAllPermissions() and hasPermissionTo() use the tenant- and the landlord-database respectively. That $user->can() declines access is a result of hasPermissionTo(), which is in the call-chain of can(). I still did not find the cause of this behaviour and opened a discussion at laravel-permissions that points to this discussion in case the root-cause is to be found there. |
Beta Was this translation helpful? Give feedback.
-
A solution has been found. The laravel-permission config needs to be made tenant-aware with a switch-task: spatie/laravel-permission#2699 (comment) |
Beta Was this translation helpful? Give feedback.
A solution has been found. The laravel-permission config needs to be made tenant-aware with a switch-task: spatie/laravel-permission#2699 (comment)