mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:14:03 +00:00
Session scoping & tenant() cleanup
This commit is contained in:
parent
c8f9a82745
commit
05d6383b99
6 changed files with 125 additions and 4 deletions
29
src/Middleware/ScopeSessions.php
Normal file
29
src/Middleware/ScopeSessions.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||
|
||||
class ScopeSessions
|
||||
{
|
||||
public static $tenantIdKey = '_tenant_id';
|
||||
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
if (! tenancy()->initialized) {
|
||||
throw new TenancyNotInitializedException('Tenancy needs to be initialized before the session scoping middleware is executed');
|
||||
}
|
||||
|
||||
if (! $request->session()->has(static::$tenantIdKey)) {
|
||||
$request->session()->put(static::$tenantIdKey, tenant()->getTenantKey());
|
||||
} else {
|
||||
if ($request->session()->get(static::$tenantIdKey) !== tenant()->getTenantKey()) {
|
||||
abort(403);
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue