aboutsummaryrefslogtreecommitdiff
path: root/lib/middleware
diff options
context:
space:
mode:
authorMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2025-02-07 12:04:19 +0000
committerMarcus Eibrink-Lunzenauer <lunzenauer@elan-ev.de>2025-02-07 12:04:19 +0000
commit6895c4e14babfce3c3881926e2d9e5ecc0f5c9af (patch)
tree11418a03e43d40723747524babb0ddec30382157 /lib/middleware
parentb6fe480a1f11a7fe260c7a3a04c9c0cbfff3ef90 (diff)
Polish plugins.php/StudIPPlugin routing mechanism.
Merge request studip/studip!3914
Diffstat (limited to 'lib/middleware')
-rw-r--r--lib/middleware/DispatchPluginMiddleware.php63
-rw-r--r--lib/middleware/HandleAccessDeniedMiddleware.php33
-rw-r--r--lib/middleware/TrailingSlash.php48
3 files changed, 144 insertions, 0 deletions
diff --git a/lib/middleware/DispatchPluginMiddleware.php b/lib/middleware/DispatchPluginMiddleware.php
new file mode 100644
index 0000000..a1c86e4
--- /dev/null
+++ b/lib/middleware/DispatchPluginMiddleware.php
@@ -0,0 +1,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);
+ }
+}
diff --git a/lib/middleware/HandleAccessDeniedMiddleware.php b/lib/middleware/HandleAccessDeniedMiddleware.php
new file mode 100644
index 0000000..d27e4b7
--- /dev/null
+++ b/lib/middleware/HandleAccessDeniedMiddleware.php
@@ -0,0 +1,33 @@
+<?php
+namespace Studip\Middleware;
+
+use AccessDeniedException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use Psr\Http\Message\ResponseFactoryInterface;
+use Request;
+use URLHelper;
+
+final class HandleAccessDeniedMiddleware implements MiddlewareInterface
+{
+ public function __construct(private ResponseFactoryInterface $responseFactory)
+ {
+ }
+
+ /**
+ * @SuppressWarnings(StaticAccess)
+ * @SuppressWarnings(SuperGlobals)
+ */
+ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+ {
+ try {
+ return $handler->handle($request);
+ } catch (AccessDeniedException $ade) {
+ $_SESSION['redirect_after_login'] = Request::url();
+ $response = $this->responseFactory->createResponse(302);
+ return $response->withHeader('Location', URLHelper::getURL('dispatch.php/login'));
+ }
+ }
+}
diff --git a/lib/middleware/TrailingSlash.php b/lib/middleware/TrailingSlash.php
new file mode 100644
index 0000000..2cc2292
--- /dev/null
+++ b/lib/middleware/TrailingSlash.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Studip\Middleware;
+
+use Closure;
+use Psr\Http\Message\RequestInterface;
+use Psr\Http\Message\ResponseFactoryInterface;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+class TrailingSlash
+{
+ public function __construct(protected ResponseFactoryInterface $responseFactory)
+ {
+ }
+
+ /**
+ * Handle the incoming request.
+ *
+ * @return ResponseInterface
+ */
+ public function __invoke(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+ {
+ $uri = $request->getUri();
+ $path = $this->normalize($uri->getPath());
+ if ($this->responseFactory && ($uri->getPath() !== $path)) {
+ return $this->responseFactory->createResponse(301)
+ ->withHeader('Location', (string) $uri->withPath($path));
+ }
+
+ $response = $handler->handle($request);
+
+ return $response;
+ }
+
+ private function normalize(string $path): string
+ {
+ if ($path === '') {
+ return '/';
+ }
+
+ if (strlen($path) > 1) {
+ return rtrim($path, '/');
+ }
+
+ return $path;
+ }
+}