1a0801ffbSGreg Roach<?php 23976b470SGreg Roach 3a0801ffbSGreg Roach/** 4a0801ffbSGreg Roach * webtrees: online genealogy 5*dec352c1SGreg Roach * Copyright (C) 2020 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 15a0801ffbSGreg Roach * along with this program. If not, see <http://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; 23a0801ffbSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 24d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException; 25d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 26a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 27a0801ffbSGreg Roachuse Psr\Http\Message\ResponseInterface; 28a0801ffbSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 29a0801ffbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 303976b470SGreg Roach 31b19e047dSGreg Roachuse function call_user_func; 32a0801ffbSGreg Roachuse function method_exists; 33*dec352c1SGreg Roachuse function str_contains; 34a0801ffbSGreg Roachuse function strtolower; 35a0801ffbSGreg Roach 36a0801ffbSGreg Roach/** 37a0801ffbSGreg Roach * Controller for module actions. 38a0801ffbSGreg Roach */ 39a0801ffbSGreg Roachclass ModuleAction implements RequestHandlerInterface 40a0801ffbSGreg Roach{ 41a0801ffbSGreg Roach /** @var ModuleService */ 42a0801ffbSGreg Roach private $module_service; 43a0801ffbSGreg Roach 44a0801ffbSGreg Roach /** 45a0801ffbSGreg Roach * ModuleController constructor. 46a0801ffbSGreg Roach * 47a0801ffbSGreg Roach * @param ModuleService $module_service 48a0801ffbSGreg Roach */ 49f4917837SGreg Roach public function __construct(ModuleService $module_service) 50a0801ffbSGreg Roach { 51a0801ffbSGreg Roach $this->module_service = $module_service; 52a0801ffbSGreg Roach } 53a0801ffbSGreg Roach 54a0801ffbSGreg Roach /** 55a0801ffbSGreg Roach * Perform an HTTP action for one of the modules. 56a0801ffbSGreg Roach * 57a0801ffbSGreg Roach * @param ServerRequestInterface $request 58a0801ffbSGreg Roach * 59a0801ffbSGreg Roach * @return ResponseInterface 60a0801ffbSGreg Roach */ 61a0801ffbSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 62a0801ffbSGreg Roach { 63ee4364daSGreg Roach $module_name = $request->getAttribute('module'); 64ee4364daSGreg Roach $action = $request->getAttribute('action'); 65f4917837SGreg Roach $user = $request->getAttribute('user'); 66f4917837SGreg Roach assert($user instanceof UserInterface); 67a0801ffbSGreg Roach 68a0801ffbSGreg Roach // Check that the module is enabled. 69a0801ffbSGreg Roach // The module itself will need to check any tree-level access, 70a0801ffbSGreg Roach // which may be different for each component (tab, menu, etc.) of the module. 71a0801ffbSGreg Roach $module = $this->module_service->findByName($module_name); 72a0801ffbSGreg Roach 73a0801ffbSGreg Roach if ($module === null) { 74d501c45dSGreg Roach throw new HttpNotFoundException('Module ' . $module_name . ' does not exist'); 75a0801ffbSGreg Roach } 76a0801ffbSGreg Roach 77a0801ffbSGreg Roach // We'll call a function such as Module::getFooBarAction() 78a0801ffbSGreg Roach $verb = strtolower($request->getMethod()); 79a0801ffbSGreg Roach $method = $verb . $action . 'Action'; 80a0801ffbSGreg Roach 81a0801ffbSGreg Roach // Actions with "Admin" in the name are for administrators only. 82*dec352c1SGreg Roach if (str_contains($action, 'Admin') && !Auth::isAdmin($user)) { 83d501c45dSGreg Roach throw new HttpAccessDeniedException('Admin only action'); 84a0801ffbSGreg Roach } 85a0801ffbSGreg Roach 86a0801ffbSGreg Roach if (!method_exists($module, $method)) { 87d501c45dSGreg Roach throw new HttpNotFoundException('Method ' . $method . '() not found in ' . $module_name); 88a0801ffbSGreg Roach } 89a0801ffbSGreg Roach 90b19e047dSGreg Roach return call_user_func([$module, $method], $request); 91a0801ffbSGreg Roach } 92a0801ffbSGreg Roach} 93