xref: /webtrees/app/Http/RequestHandlers/ModuleAction.php (revision b19e047d78b7721fa9b530ea3026f960e2398415)
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 */
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;
24a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
25a0801ffbSGreg Roachuse Psr\Http\Message\ResponseInterface;
26a0801ffbSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27a0801ffbSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28a0801ffbSGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
29a0801ffbSGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
303976b470SGreg Roach
31*b19e047dSGreg Roachuse function call_user_func;
32a0801ffbSGreg Roachuse function method_exists;
33a0801ffbSGreg Roachuse function strpos;
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    /** @var UserInterface */
45a0801ffbSGreg Roach    private $user;
46a0801ffbSGreg Roach
47a0801ffbSGreg Roach    /**
48a0801ffbSGreg Roach     * ModuleController constructor.
49a0801ffbSGreg Roach     *
50a0801ffbSGreg Roach     * @param ModuleService $module_service
51a0801ffbSGreg Roach     * @param UserInterface $user
52a0801ffbSGreg Roach     */
53a0801ffbSGreg Roach    public function __construct(ModuleService $module_service, UserInterface $user)
54a0801ffbSGreg Roach    {
55a0801ffbSGreg Roach        $this->module_service = $module_service;
56a0801ffbSGreg Roach        $this->user           = $user;
57a0801ffbSGreg Roach    }
58a0801ffbSGreg Roach
59a0801ffbSGreg Roach    /**
60a0801ffbSGreg Roach     * Perform an HTTP action for one of the modules.
61a0801ffbSGreg Roach     *
62a0801ffbSGreg Roach     * @param ServerRequestInterface $request
63a0801ffbSGreg Roach     *
64a0801ffbSGreg Roach     * @return ResponseInterface
65a0801ffbSGreg Roach     */
66a0801ffbSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
67a0801ffbSGreg Roach    {
68ee4364daSGreg Roach        $module_name = $request->getAttribute('module');
69ee4364daSGreg Roach        $action      = $request->getAttribute('action');
70a0801ffbSGreg Roach
71a0801ffbSGreg Roach        // Check that the module is enabled.
72a0801ffbSGreg Roach        // The module itself will need to check any tree-level access,
73a0801ffbSGreg Roach        // which may be different for each component (tab, menu, etc.) of the module.
74a0801ffbSGreg Roach        $module = $this->module_service->findByName($module_name);
75a0801ffbSGreg Roach
76a0801ffbSGreg Roach        if ($module === null) {
77a0801ffbSGreg Roach            throw new NotFoundHttpException('Module ' . $module_name . ' does not exist');
78a0801ffbSGreg Roach        }
79a0801ffbSGreg Roach
80a0801ffbSGreg Roach        // We'll call a function such as Module::getFooBarAction()
81a0801ffbSGreg Roach        $verb   = strtolower($request->getMethod());
82a0801ffbSGreg Roach        $method = $verb . $action . 'Action';
83a0801ffbSGreg Roach
84a0801ffbSGreg Roach        // Actions with "Admin" in the name are for administrators only.
85a0801ffbSGreg Roach        if (strpos($action, 'Admin') !== false && !Auth::isAdmin($this->user)) {
86a0801ffbSGreg Roach            throw new AccessDeniedHttpException('Admin only action');
87a0801ffbSGreg Roach        }
88a0801ffbSGreg Roach
89a0801ffbSGreg Roach        if (!method_exists($module, $method)) {
90a0801ffbSGreg Roach            throw new NotFoundHttpException('Method ' . $method . '() not found in ' . $module_name);
91a0801ffbSGreg Roach        }
92a0801ffbSGreg Roach
93*b19e047dSGreg Roach        return call_user_func([$module, $method], $request);
94a0801ffbSGreg Roach    }
95a0801ffbSGreg Roach}
96