. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fisharebest\Webtrees\Auth; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use function redirect; use function route; /** * Middleware to generate a response when no route was matched. */ class NoRouteFound implements MiddlewareInterface { use ViewResponseTrait; /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { /** @var Tree|null $tree */ $tree = app(Tree::class); // The tree exists, we have access to it, and it is fully imported. if ($tree instanceof Tree && $tree->getPreference('imported') === '1') { return redirect(route('tree-page', ['ged' => $tree->name()])); } // Not logged in? if (!Auth::check()) { return redirect(route('login', ['url' => $request->getAttribute('request_uri')])); } // No tree or tree not imported? if (Auth::isAdmin()) { return redirect(route('admin-trees')); } return $this->viewResponse('errors/no-tree-access', ['title' => '']); } }