xref: /webtrees/app/Http/Middleware/AuthMember.php (revision d11be7027e34e3121be11cc025421873364403f9)
101202f80SGreg Roach<?php
201202f80SGreg Roach
301202f80SGreg Roach/**
401202f80SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
601202f80SGreg Roach * This program is free software: you can redistribute it and/or modify
701202f80SGreg Roach * it under the terms of the GNU General Public License as published by
801202f80SGreg Roach * the Free Software Foundation, either version 3 of the License, or
901202f80SGreg Roach * (at your option) any later version.
1001202f80SGreg Roach * This program is distributed in the hope that it will be useful,
1101202f80SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1201202f80SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1301202f80SGreg Roach * GNU General Public License for more details.
1401202f80SGreg 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/>.
1601202f80SGreg Roach */
17fcfa147eSGreg Roach
1801202f80SGreg Roachdeclare(strict_types=1);
1901202f80SGreg Roach
2001202f80SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
2101202f80SGreg Roach
2215958f0eSGreg Roachuse Fig\Http\Message\RequestMethodInterface;
2301202f80SGreg Roachuse Fisharebest\Webtrees\Auth;
2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
2501202f80SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
2601202f80SGreg Roachuse Fisharebest\Webtrees\User;
27b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
2801202f80SGreg Roachuse Psr\Http\Message\ResponseInterface;
2901202f80SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3001202f80SGreg Roachuse Psr\Http\Server\MiddlewareInterface;
3101202f80SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3271378461SGreg Roach
3301202f80SGreg Roachuse function redirect;
340c0910bfSGreg Roachuse function route;
3501202f80SGreg Roach
3601202f80SGreg Roach/**
3701202f80SGreg Roach * Middleware to restrict access to editors.
3801202f80SGreg Roach */
3901202f80SGreg Roachclass AuthMember implements MiddlewareInterface
4001202f80SGreg Roach{
4101202f80SGreg Roach    /**
4201202f80SGreg Roach     * @param ServerRequestInterface  $request
4301202f80SGreg Roach     * @param RequestHandlerInterface $handler
4401202f80SGreg Roach     *
4501202f80SGreg Roach     * @return ResponseInterface
4601202f80SGreg Roach     */
4701202f80SGreg Roach    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
4801202f80SGreg Roach    {
49b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->tree();
50b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
5101202f80SGreg Roach
5201202f80SGreg Roach        // Logged in with the correct role?
5386661454SGreg Roach        if (Auth::isMember($tree, $user)) {
5401202f80SGreg Roach            return $handler->handle($request);
5501202f80SGreg Roach        }
5601202f80SGreg Roach
5701202f80SGreg Roach        // Logged in, but without the correct role?
5815958f0eSGreg Roach        if ($user instanceof User || $request->getMethod() === RequestMethodInterface::METHOD_POST) {
59d501c45dSGreg Roach            throw new HttpAccessDeniedException();
6001202f80SGreg Roach        }
6101202f80SGreg Roach
6201202f80SGreg Roach        // Not logged in.
6309482a55SGreg Roach        return redirect(route(LoginPage::class, ['tree' => $tree->name(), 'url' => (string) $request->getUri()]));
6401202f80SGreg Roach    }
6501202f80SGreg Roach}
66