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