kuvia/app/Http/Middleware/TenanMiddleware.php

37 lines
1,022 B
PHP
Raw Normal View History

2021-01-12 14:49:45 +00:00
<?php
namespace App\Http\Middleware;
use App\Models\Tenant;
2021-01-21 17:56:30 +00:00
use App\Models\User;
2021-01-12 14:49:45 +00:00
use Closure;
use Illuminate\Support\Facades\Auth;
2021-01-12 21:46:12 +00:00
use Illuminate\Support\Facades\Log;
2021-01-12 14:49:45 +00:00
class TenanMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check()) {
2021-01-12 21:46:12 +00:00
if(empty(session("current_tenant_id"))) {
Log::info("Set Tenant in Middleware");
$tenant = Tenant::query()->where("url", "=", Auth::user()->username)->first();
session(["current_tenant_id" => $tenant->id]);
}
2021-01-12 14:49:45 +00:00
view()->share('user_tenants', Tenant::getTenantPerUser(Auth::id()));
2021-01-12 21:46:12 +00:00
view()->share('current_tenant', Tenant::query()->where("id", "=", session("current_tenant_id"))->first());
2021-01-21 17:56:30 +00:00
view()->share('user', Auth::user());
2021-01-12 14:49:45 +00:00
}
return $next($request);
}
}