spatie / laravel-permission

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

Permissions and Role Disapper(empty) from Model after ~10-14 requests #2693

Open jihadismail8 opened 1 month ago

jihadismail8 commented 1 month ago

Description

ok guys i been looking for this issue for two days already and can not find the problem i am using laravel 11 with octane (roadrunner) all the latest versions (fresh install) , i got jetstream as well , and spatie permissions with teams ON . so during requesting my user model with permissions and roles , i get the data and everything is good , after around 14 requests in a row , the role and permissions from user Model becomes empty , i tried to use " setPermissionsTeamId('1')" almost everywhere ( routes, in __construct of the controller and in the method of the controller , nothing helped , i tried to flush in octane settings in "OperationTerminated" also did not help , keep notice that in artisan serve mode it works fine , so it seems something with octane is the issue or with this package , if anything , i am trying to upgrade my project from laravel 10 to laravel 11 , which in it i have this package ( version 5.9 ) and older octane and laravel 10 , and it is working fine no issues with it .

Steps To Reproduce

1.clone the repo , then - docker compose build ( sorry for the long build time , all the extentions are needed ) 2.docker compose up 3.postman ( post request to (http://0.0.0.0:8081/api/login ) ) get the token , user can be found in seeder RolesAndPermissionsSeeder 4.GET request to (http://0.0.0.0:8081/api/permissions ) not to forget to add Bearer Token in header

Example Application

https://github.com/jihadismail8/OctaneSpatie

Version of spatie/laravel-permission package:

6.9

Version of laravel/framework package:

11

PHP version:

8.3

Database engine and version:

sqlite

OS: Windows/Mac/Linux version:

ubuntu 22

jihadismail8 commented 1 month ago

i tested the API's calls and i tested livewire , on both the issue persists . you can try livewire as well , go to 0.0.0.0:8081/login , then /dashboard , in dashboard you can click on up right corner( user name) go to Roles page and back for around 14 times , and the permissions with roles will disappear and pages wont become accessible anymore

parallels999 commented 1 month ago

Hi, I don't use octane, and I wouldn't know how to fix it. But, feel free to make a PR fixing it https://github.com/spatie/laravel-permission/blob/80a32a10f7439fe381a5684e78e72cadd8fbd170/src/PermissionServiceProvider.php#L95-L116

jihadismail8 commented 1 month ago

i been trying for two days with no luck )) i tried to ->setPermissionsTeamId(null) and ->clearPermissionsCollection() in octane config listeners but still no luck , same issue . and again this used to work fine on laravel 9 and spatie 5.9

parallels999 commented 1 month ago

this used to work fine on laravel 9 and spatie 5.9

You could check what has changed between those versions, maybe the bug is there

jihadismail8 commented 1 month ago

https://github.com/spatie/laravel-permission/compare/671e46e..80a32a1
could not find anything helpful !!!

jihadismail8 commented 1 month ago

ok an update to the issue , when the teams=>false in permission config file is set there is no problem . so seems octane is losing the team id somewhere and because of it the roles and permissions as well . any idea how to debug it ? where the app would be losing the team id if in controller inside the method i am setting the id ( setPermissionsTeamId('1');) ?

parallels999 commented 1 month ago

seems octane is losing the team id somewhere

It seems that you did not read my initial comment, I specified where in the code that happened(Also #2545)

It seems that you did not follow the documentation, the middleware is supposed to put the team_id again in each request laravel-permission/teams-permissions#content-working-with-teams-permissions