]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Middleware/PublicPages.php
8ebbcf1b6f4a304b4888bd033e4005c17735813a
[galette.git] / galette / lib / Galette / Middleware / PublicPages.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\Middleware;
23
24 use Galette\Core\Login;
25 use Galette\Core\Preferences;
26 use Psr\Http\Message\ServerRequestInterface as Request;
27 use Psr\Http\Message\ResponseInterface as Response;
28 use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
29 use DI\Container;
30 use Slim\Flash\Messages;
31 use Slim\Routing\RouteParser;
32
33 /**
34 * Galette Slim middleware for public pages access
35 *
36 * @author Johan Cwiklinski <johan@x-tnd.be>
37 */
38 class PublicPages
39 {
40 /**
41 * @var Messages
42 */
43 protected Messages $flash;
44
45 /**
46 * @var Login
47 */
48 private Login $login;
49
50 /**
51 * @var RouteParser
52 */
53 private RouteParser $routeparser;
54
55 /**
56 * @var Preferences
57 */
58 private Preferences $preferences;
59
60 /**
61 * Constructor
62 *
63 * @param Container $container Container instance
64 */
65 public function __construct(Container $container)
66 {
67 $this->login = $container->get('login');
68 $this->flash = $container->get('flash');
69 $this->routeparser = $container->get(RouteParser::class);
70 $this->preferences = $container->get('preferences');
71 }
72
73 /**
74 * Middleware invokable class
75 *
76 * @param Request $request PSR7 request
77 * @param RequestHandler $handler PSR7 request handler
78 *
79 * @return Response
80 */
81 public function __invoke(Request $request, RequestHandler $handler): Response
82 {
83 $response = new \Slim\Psr7\Response();
84
85 if (!$this->preferences->showPublicPages($this->login)) {
86 $this->flash->addMessage('error_detected', _T("Unauthorized"));
87 return $response
88 ->withHeader(
89 'Location',
90 $this->routeparser->urlFor('slash')
91 )->withStatus(302);
92 }
93
94 return $handler->handle($request);
95 }
96 }