aboutsummaryrefslogtreecommitdiff
path: root/lib/middleware/DispatchPluginMiddleware.php
blob: a1c86e4f15cf6f56f9c0187ff7ccb968d4dc3614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

namespace Studip\Middleware;

use AccessDeniedException;
use PageLayout;
use PluginEngine;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Request;
use Slim\App;
use StudIPPlugin;
use URLHelper;

final class DispatchPluginMiddleware implements MiddlewareInterface
{
    public function __construct(
        private App $app,
        private ContainerInterface $container,
        private ResponseFactoryInterface $responseFactory,
    ) {
    }

    /**
     * @SuppressWarnings(StaticAccess)
     * @SuppressWarnings(SuperGlobals)
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // get plugin class from request
        $dispatchTo = Request::pathInfo();
        [$pluginClass, $unconsumed] = PluginEngine::routeRequest($dispatchTo);

        // handle legacy forum plugin URLs
        if ($pluginClass === 'coreforum') {
            return $this->responseFactory
                ->createResponse(302)
                ->withHeader(
                    'Location',
                    URLHelper::getURL('dispatch.php/course/forum/' . $unconsumed)
                );
        }

        $plugin = PluginEngine::getPlugin($pluginClass);
        $this->container->set(StudIPPlugin::class, $plugin);

        // user is not permitted, show login screen
        if (is_null($plugin)) {
            throw new AccessDeniedException(_('Sie besitzen keine Rechte zum Aufruf dieses Plugins.'));
        }

        // set default page title
        PageLayout::setTitle($plugin->getPluginName());

        $plugin->registerSlimRoutes($this->app, $unconsumed);

        return $handler->handle($request);
    }
}