Landlord: Scope Not being applied in middleware
I added a ScopeRequestByUser middleware like so
public function handle($request, Closure $next)
{
if ($request->ajax() || $request->wantsJson()) {
Landlord::addTenant('user_id', Auth::guard('api')->id());
}
return $next($request);
}
Now this works perfectly when I add this to my global middleware stack like this.
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\ScopeRequestByUser::class,
];
However, I need it to be applied only on the api middleware group. When I add to the group in the middleware file it does not work.
protected $middlewareGroups = [
'web' => [
...
],
'api' => [
'throttle:60,1',
'bindings',
\App\Http\Middleware\ScopeRequestByUser::class,
],
];
I even tried adding it ot the route middleware and then calling it both in the routes file and in the api middleware group. Both of them are not working.
protected $routeMiddleware = [
...
'scope' => \App\Http\Middleware\ScopeRequestByUser::class,
];
Not sure, what is going wrong here.
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 4
- Comments: 22 (10 by maintainers)
It’s not only using model observers that cause the model’s boot sequence to happen before tenants have been added - I was using dependency injection to instantiate a model repository, and that also caused the model to boot before the route middleware could run and add a tenant. If I added the middleware to the global list (as well as the session and cookie middlewares so I could access the logged in user) then the tenant would be set first. However, I don’t want it set on all routes.
The solution was using the suggestion in #61. My middleware uses
clearBootedModelsjust before setting the tenant id:My temporary solution (not the best) was to extend trait, and then I added the tenant after getting the TenantManager from laravel container.
We changed our approach a bit. The issue is with booting models before the middleware has run. Registering model observers will boot the model, since
Model::observewill donew static;, which calls the constructor and boots the model.So now I am registering observers like this:
This ensures that the observer isn’t added until after the User model has been booted by some other process.
Also added an issue for this, can hopefully spark a discussion: https://github.com/laravel/framework/issues/16715
I haven’t dug completely down through the call stack to see in what order things are happening in here, but the Kernel seems to be handling running the request through the global
$middlewareand then the router is responsible for running the request through the$routeMiddlewareand$middlewareGroups.The whole point here is to have the middleware run (and tenant ID set) BEFORE we attempt to set the global scope in models (meaning before any model is booted).
In my case, that meant having to move the middleware to the global
$middleware. Having it in the middleware handled by the router fails.I am having a similar issue. My problem seems to be the timing of when the tenant is being registered, because some models are being booted before the request is run through the middleware where the tenant is set.
From what I can tell, this is the order of things:
sendRequestThroughRouterbootstrap()is run