Files
grocy/middleware/SessionAuthMiddleware.php

45 lines
1.3 KiB
PHP
Raw Normal View History

2018-04-11 19:49:35 +02:00
<?php
namespace Grocy\Middleware;
2018-04-12 21:13:38 +02:00
use \Grocy\Services\SessionService;
2018-04-11 19:49:35 +02:00
2018-04-14 11:10:38 +02:00
class SessionAuthMiddleware extends BaseMiddleware
2018-04-11 19:49:35 +02:00
{
public function __construct(\Slim\Container $container, string $sessionCookieName)
{
parent::__construct($container);
$this->SessionCookieName = $sessionCookieName;
}
protected $SessionCookieName;
2018-04-11 19:49:35 +02:00
public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, callable $next)
{
$route = $request->getAttribute('route');
$routeName = $route->getName();
2018-07-16 21:17:32 +02:00
if ($routeName === 'root' || $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation())
2018-04-11 19:49:35 +02:00
{
2018-07-16 21:17:32 +02:00
define('AUTHENTICATED', $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation());
$response = $next($request, $response);
2018-04-11 19:49:35 +02:00
}
else
{
$sessionService = new SessionService();
if ((!isset($_COOKIE[$this->SessionCookieName]) || !$sessionService->IsValidSession($_COOKIE[$this->SessionCookieName])) && $routeName !== 'login')
{
define('AUTHENTICATED', false);
$response = $response->withRedirect($this->AppContainer->UrlManager->ConstructUrl('/login'));
}
else
{
define('AUTHENTICATED', $routeName !== 'login');
$response = $next($request, $response);
}
2018-04-11 19:49:35 +02:00
}
return $response;
}
}