mirror of
				https://github.com/grocy/grocy.git
				synced 2025-10-31 02:36:54 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			45 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace Grocy\Middleware;
 | |
| 
 | |
| use \Grocy\Services\SessionService;
 | |
| 
 | |
| class SessionAuthMiddleware extends BaseMiddleware
 | |
| {
 | |
| 	public function __construct(\Slim\Container $container, string $sessionCookieName)
 | |
| 	{
 | |
| 		parent::__construct($container);
 | |
| 		$this->SessionCookieName = $sessionCookieName;
 | |
| 	}
 | |
| 
 | |
| 	protected $SessionCookieName;
 | |
| 
 | |
| 	public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, callable $next)
 | |
| 	{
 | |
| 		$route = $request->getAttribute('route');
 | |
| 		$routeName = $route->getName();
 | |
| 
 | |
| 		if ($routeName === 'root' || $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation())
 | |
| 		{
 | |
| 			define('AUTHENTICATED', $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation());
 | |
| 			$response = $next($request, $response);
 | |
| 		}
 | |
| 		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);
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		return $response;
 | |
| 	}
 | |
| }
 |