-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setPermissionsTeamId() function not preserving team_id when set anywhere but from the middleware #2756
Comments
I imagine that you forgot to reload the relationships already preloaded after changing the team_id |
Nope, I made sure to reload the new relations after team change, when debugging the user has the permission of accessing the new team is loaded, but when excuting hasPermissionTo() or can(), returns false. |
You should give an code/repo example, everything is working as expected on my side and in the tests. |
Yes, it would seem to be an order-of-operations issue, in which wherever you're setting it gets executed in a different timing than how you're expecting it, or has been cleared out/reset by your app. Example code will be needed in order to provide assistance. |
@drbyte @parallels999 Sorry for not responding earlier, First the request points to controller method access : Controller/**
* Access the event's session
*/
public function access(Request $request, Event $event)
{
// Code ....
$this->teamService->setTeamSession($event);
return redirect($this->userService->getUserMainRoute($request->user()));
} The TeamServicepublic function setTeamSession(Organization|Event $teamable): void
{
Session::put('team', $teamable->team);
// Unsetting old loaded relations
request()->user()->unsetRelation('roles')->unsetRelation('permissions');
// Spatie's team session
setPermissionsTeamId($teamable->team->id);
} then the UserService/**
* Get the main route for a given user based on their permissions.
*
* @param User $user The user object.
* @return string The main route for the user.
*/
public function getUserMainRoute(User $user): string
{
$route = null;
if ($user->can('view-dashboard')) {
$route = route('backend.event.dashboard');
} elseif ($user->can('participants.view')) {
$route = route('backend.event.participants.index');
} elseif ($user->can('abstracts.view')) {
$route = route('backend.event.abstracts.index');
} elseif ($user->can('questions.manage')) {
$route = route('backend.event.questions.index');
}
if (!$route) {
alert("Vous n'avez pas l'accés", type: 'info')->persistent();
}
return $route ?? route('home');
}
} Note : But if put a break point on the next redirected route to and check his permissions it works, why cuz the team_id was set from the team permissions middleware. |
so, have you |
I think not, tried to remove the middleware and the user always doesn't have permission, Tried testing in tinker :
I think when I set the teams permission id it still not taking it in considiration |
Description
Using teams feature, the docs says that I can set the team_id from anywhere but better from the middleware, I did set up the middleware to set the team id, but in some areas of my code I set the team_id in the controller, but noticed after setting it then checking permissions against the user returns false though he has that permission.
Then I set a breakpoint in the permissions middleware (before setting the team_id) and checked getPermissionsTeamId() found that it returns null, though I have set it in the controller before the redirect.
Steps To Reproduce
...
Example Application
No response
Version of spatie/laravel-permission package:
6.9.0
Version of laravel/framework package:
8
PHP version:
8.1
Database engine and version:
mysql 8
OS: Windows/Mac/Linux version:
10
The text was updated successfully, but these errors were encountered: