1a0801ffbSGreg Roach<?php 23976b470SGreg Roach 3a0801ffbSGreg Roach/** 4a0801ffbSGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6a0801ffbSGreg Roach * This program is free software: you can redistribute it and/or modify 7a0801ffbSGreg Roach * it under the terms of the GNU General Public License as published by 8a0801ffbSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a0801ffbSGreg Roach * (at your option) any later version. 10a0801ffbSGreg Roach * This program is distributed in the hope that it will be useful, 11a0801ffbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a0801ffbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a0801ffbSGreg Roach * GNU General Public License for more details. 14a0801ffbSGreg 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/>. 16a0801ffbSGreg Roach */ 17fcfa147eSGreg Roach 18a0801ffbSGreg Roachdeclare(strict_types=1); 19a0801ffbSGreg Roach 20a0801ffbSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21a0801ffbSGreg Roach 22a0801ffbSGreg Roachuse Fisharebest\Webtrees\Auth; 2381b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 25a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 26b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 27a0801ffbSGreg Roachuse Psr\Http\Message\ResponseInterface; 28a0801ffbSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 29a0801ffbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 303976b470SGreg Roach 31a0801ffbSGreg Roachuse function method_exists; 32dec352c1SGreg Roachuse function str_contains; 33a0801ffbSGreg Roachuse function strtolower; 34a0801ffbSGreg Roach 35a0801ffbSGreg Roach/** 36a0801ffbSGreg Roach * Controller for module actions. 37a0801ffbSGreg Roach */ 38a0801ffbSGreg Roachclass ModuleAction implements RequestHandlerInterface 39a0801ffbSGreg Roach{ 40c4943cffSGreg Roach private ModuleService $module_service; 41a0801ffbSGreg Roach 42a0801ffbSGreg Roach /** 43a0801ffbSGreg Roach * @param ModuleService $module_service 44a0801ffbSGreg Roach */ 45f4917837SGreg Roach public function __construct(ModuleService $module_service) 46a0801ffbSGreg Roach { 47a0801ffbSGreg Roach $this->module_service = $module_service; 48a0801ffbSGreg Roach } 49a0801ffbSGreg Roach 50a0801ffbSGreg Roach /** 51a0801ffbSGreg Roach * Perform an HTTP action for one of the modules. 52a0801ffbSGreg Roach * 53a0801ffbSGreg Roach * @param ServerRequestInterface $request 54a0801ffbSGreg Roach * 55a0801ffbSGreg Roach * @return ResponseInterface 56a0801ffbSGreg Roach */ 57a0801ffbSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 58a0801ffbSGreg Roach { 59ee4364daSGreg Roach $module_name = $request->getAttribute('module'); 60ee4364daSGreg Roach $action = $request->getAttribute('action'); 61b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 62a0801ffbSGreg Roach 63a0801ffbSGreg Roach // Check that the module is enabled. 64a0801ffbSGreg Roach // The module itself will need to check any tree-level access, 65a0801ffbSGreg Roach // which may be different for each component (tab, menu, etc.) of the module. 66a0801ffbSGreg Roach $module = $this->module_service->findByName($module_name); 67a0801ffbSGreg Roach 68a0801ffbSGreg Roach if ($module === null) { 69d501c45dSGreg Roach throw new HttpNotFoundException('Module ' . $module_name . ' does not exist'); 70a0801ffbSGreg Roach } 71a0801ffbSGreg Roach 72a0801ffbSGreg Roach // We'll call a function such as Module::getFooBarAction() 73a0801ffbSGreg Roach $verb = strtolower($request->getMethod()); 74a0801ffbSGreg Roach $method = $verb . $action . 'Action'; 75a0801ffbSGreg Roach 76a0801ffbSGreg Roach // Actions with "Admin" in the name are for administrators only. 77dec352c1SGreg Roach if (str_contains($action, 'Admin') && !Auth::isAdmin($user)) { 78d501c45dSGreg Roach throw new HttpAccessDeniedException('Admin only action'); 79a0801ffbSGreg Roach } 80a0801ffbSGreg Roach 81a0801ffbSGreg Roach if (!method_exists($module, $method)) { 82d501c45dSGreg Roach throw new HttpNotFoundException('Method ' . $method . '() not found in ' . $module_name); 83a0801ffbSGreg Roach } 84a0801ffbSGreg Roach 8579a908a7SGreg Roach return $module->$method($request); 86a0801ffbSGreg Roach } 87a0801ffbSGreg Roach} 88