xref: /webtrees/app/Http/Middleware/AuthLoggedIn.php (revision d11be7027e34e3121be11cc025421873364403f9)
10c0910bfSGreg Roach<?php
20c0910bfSGreg Roach
30c0910bfSGreg Roach/**
40c0910bfSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
60c0910bfSGreg Roach * This program is free software: you can redistribute it and/or modify
70c0910bfSGreg Roach * it under the terms of the GNU General Public License as published by
80c0910bfSGreg Roach * the Free Software Foundation, either version 3 of the License, or
90c0910bfSGreg Roach * (at your option) any later version.
100c0910bfSGreg Roach * This program is distributed in the hope that it will be useful,
110c0910bfSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
120c0910bfSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130c0910bfSGreg Roach * GNU General Public License for more details.
140c0910bfSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
160c0910bfSGreg Roach */
17fcfa147eSGreg Roach
180c0910bfSGreg Roachdeclare(strict_types=1);
190c0910bfSGreg Roach
200c0910bfSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
210c0910bfSGreg Roach
220c0910bfSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
230c0910bfSGreg Roachuse Fisharebest\Webtrees\Tree;
240c0910bfSGreg Roachuse Fisharebest\Webtrees\User;
25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
260c0910bfSGreg Roachuse Psr\Http\Message\ResponseInterface;
270c0910bfSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
280c0910bfSGreg Roachuse Psr\Http\Server\MiddlewareInterface;
290c0910bfSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
300c0910bfSGreg Roach
310c0910bfSGreg Roachuse function redirect;
320c0910bfSGreg Roach
330c0910bfSGreg Roach/**
340c0910bfSGreg Roach * Middleware to restrict access to logged-in users.
350c0910bfSGreg Roach */
360c0910bfSGreg Roachclass AuthLoggedIn implements MiddlewareInterface
370c0910bfSGreg Roach{
380c0910bfSGreg Roach    /**
390c0910bfSGreg Roach     * @param ServerRequestInterface  $request
400c0910bfSGreg Roach     * @param RequestHandlerInterface $handler
410c0910bfSGreg Roach     *
420c0910bfSGreg Roach     * @return ResponseInterface
430c0910bfSGreg Roach     */
440c0910bfSGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
450c0910bfSGreg Roach    {
46b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
47b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
480c0910bfSGreg Roach
490c0910bfSGreg Roach        // Logged in?
500c0910bfSGreg Roach        if ($user instanceof User) {
510c0910bfSGreg Roach            return $handler->handle($request);
520c0910bfSGreg Roach        }
530c0910bfSGreg Roach
540c0910bfSGreg Roach        // Not logged in.
55a49d0e3fSGreg Roach        return redirect(route(LoginPage::class, [
5681bf3221SGreg Roach            'tree' => $tree?->name(),
5709482a55SGreg Roach            'url'  => (string) $request->getUri(),
58a49d0e3fSGreg Roach        ]));
590c0910bfSGreg Roach    }
600c0910bfSGreg Roach}
61