Implement Multi-tenancy with Bouncer Scope
masterBouncer supports multi-tenant applications by scoping roles and abilities to a specific tenant.
- Publish the scope middleware:
php artisan vendor:publish --tag="bouncer.middleware"- Configure the middleware:
Edit the published middleware (typically
app/Http/Middleware/ScopeBouncer.php) to define how the tenant ID is retrieved from the request:
public function handle($request, Closure $next)
{
$tenantId = $request->user()->account_id;
Bouncer::scope()->to($tenantId);
return $next($request);
}- Register the middleware in your
app/Http/Kernel.phpwithin thewebmiddleware group:
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\ScopeBouncer::class,
]
];