aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/ShaarliAdminMiddleware.php
blob: 35ce4a3beb3832850217754ce9b11261231c8cbd (plain) (blame)
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
<?php

namespace Shaarli\Front;

use Slim\Http\Request;
use Slim\Http\Response;

/**
 * Middleware used for controller requiring to be authenticated.
 * It extends ShaarliMiddleware, and just make sure that the user is authenticated.
 * Otherwise, it redirects to the login page.
 */
class ShaarliAdminMiddleware extends ShaarliMiddleware
{
    public function __invoke(Request $request, Response $response, callable $next): Response
    {
        $this->initBasePath($request);

        if (true !== $this->container->loginManager->isLoggedIn()) {
            $returnUrl = urlencode($this->container->environment['REQUEST_URI']);

            return $response->withRedirect($this->container->basePath . '/login?returnurl=' . $returnUrl);
        }

        return parent::__invoke($request, $response, $next);
    }
}