Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website. Co-authored-by: Cursor <cursoragent@cursor.com>
62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Tenancy\TenantContext;
|
|
use App\Themes\Theme;
|
|
use App\Themes\ThemeFactory;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* Determines site, theme and appearance for the website and activates the "theme::" views.
|
|
*/
|
|
class ResolveTheme
|
|
{
|
|
public function __construct(private ThemeFactory $factory) {}
|
|
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$context = app(TenantContext::class);
|
|
$context->forget();
|
|
|
|
$theme = $this->factory->forRequest($request)->activate();
|
|
app()->instance(Theme::class, $theme);
|
|
|
|
$tenant = null;
|
|
|
|
if ($theme->site->exists && $theme->site->tenant_id !== null) {
|
|
$tenant = $theme->site->tenant;
|
|
} elseif (Tenant::query()->count() === 1) {
|
|
$tenant = Tenant::query()->first();
|
|
}
|
|
|
|
if ($tenant !== null) {
|
|
if (! $tenant->is_active && ! $this->operatorMayPass($request)) {
|
|
abort(404);
|
|
}
|
|
|
|
$context->set($tenant);
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
/**
|
|
* A suspended landlord stays reachable for the operator, and the operator panel itself stays open.
|
|
*/
|
|
private function operatorMayPass(Request $request): bool
|
|
{
|
|
if ($request->is('betreiber', 'betreiber/*')) {
|
|
return true;
|
|
}
|
|
|
|
$user = $request->user();
|
|
|
|
return $user instanceof User && $user->is_operator;
|
|
}
|
|
}
|