1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00
kanboard_ynh/sources/app/Middleware/AuthenticationMiddleware.php
2016-07-23 14:11:39 +02:00

56 lines
1.5 KiB
PHP

<?php
namespace Kanboard\Middleware;
use Kanboard\Core\Controller\AccessForbiddenException;
use Kanboard\Core\Controller\BaseMiddleware;
use Kanboard\Core\Security\Role;
/**
* Class AuthenticationMiddleware
*
* @package Kanboard\Middleware
* @author Frederic Guillot
*/
class AuthenticationMiddleware extends BaseMiddleware
{
/**
* Execute middleware
*/
public function execute()
{
if (! $this->authenticationManager->checkCurrentSession()) {
throw AccessForbiddenException::getInstance()->withoutLayout();
}
if (! $this->isPublicAccess()) {
$this->handleAuthentication();
}
$this->next();
}
protected function handleAuthentication()
{
if (! $this->userSession->isLogged() && ! $this->authenticationManager->preAuthentication()) {
$this->nextMiddleware = null;
if ($this->request->isAjax()) {
$this->response->text('Not Authorized', 401);
} else {
$this->sessionStorage->redirectAfterLogin = $this->request->getUri();
$this->response->redirect($this->helper->url->to('AuthController', 'login'));
}
}
}
protected function isPublicAccess()
{
if ($this->applicationAuthorization->isAllowed($this->router->getController(), $this->router->getAction(), Role::APP_PUBLIC)) {
$this->nextMiddleware = null;
return true;
}
return false;
}
}