Commit 673cc4be authored by Karamel's avatar Karamel
Browse files

Add logout button on fiscal access. Fix redirection to login with an outdated token.

parent 87cf3bda
......@@ -25,7 +25,7 @@ $loginMiddleware = function($request, $response, $next) {
if (substr($path, 0, 1) == '/') {
$path = substr($path, 1);
}
$mustLogin = ('fiscal/' !== $path && 'api/login' !== $path
$mustLogin = ('fiscal/' !== substr($path, 0, 7) && 'api/login' !== $path
&& !$request->isOptions());
if ($userId === null && $mustLogin) {
......@@ -34,20 +34,33 @@ $loginMiddleware = function($request, $response, $next) {
} else {
// Pass the call to the regular route
$response = $next($request, $response);
$basePath = $request->getUri()->getBasePath();
if ($basePath == '') {
$basePath = '/';
}
// Inject fresh token
if ($userId !== null) {
$newToken = Login::issueAppToken($ptApp);
$response = $response->withHeader(Login::TOKEN_HEADER, $newToken);
$cookie = SetCookie::create(Login::TOKEN_HEADER)
->withValue($newToken)
->withMaxAge($ptApp->getJwtTimeout())
->withPath($request->getUri()->getPath())
->withDomain($request->getUri()->getHost());
$response = FigResponseCookies::set($response, $cookie);
if ($path != 'fiscal/disconnect') {
$newToken = Login::issueAppToken($ptApp);
$response = $response->withHeader(Login::TOKEN_HEADER, $newToken);
$cookie = SetCookie::create(Login::TOKEN_HEADER)
->withValue($newToken)
->withMaxAge($ptApp->getJwtTimeout())
->withPath($basePath)
->withDomain($request->getUri()->getHost());
$response = FigResponseCookies::set($response, $cookie);
} else {
$cookie = SetCookie::create(Login::TOKEN_HEADER)
->withValue("")
->withExpires(1)
->withPath($basePath)
->withDomain($request->getUri()->getHost());
$response = FigResponseCookies::set($response, $cookie);
}
} else {
// Case of login without prior token. See login route which sets the cookie.
// Nothing to do here.
}
}
return $response;
};
\ No newline at end of file
};
......@@ -18,13 +18,17 @@ function fiscalLogin($app, $ptApp, $request, $response) {
['login' => $data['user'], 'password' => $data['password']]);
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
// Set the cookie
$basePath = $request->getUri()->getBasePath();
if ($basePath == '') {
$basePath = '/';
}
$newToken = $apiResult->getContent();
if ($newToken != null) {
$response = $response->withHeader(Login::TOKEN_HEADER, $newToken);
$cookie = SetCookie::create(Login::TOKEN_HEADER)
->withValue($newToken)
->withMaxAge($ptApp->getJwtTimeout())
->withPath($request->getUri()->getPath())
->withPath($basePath)
->withDomain($request->getUri()->getHost());
$response = FigResponseCookies::set($response, $cookie);
$user = $ptApp->getIdentModule()->getUser($data['user']);
......@@ -165,7 +169,7 @@ $app->any('/fiscal/sequence/{sequence}/tickets/',
});
function fiscalHomePage($ptApp, $response) {
$data = [];
$data = ['user' => $ptApp->getCurrentUser()['id']];
$apiResult = APICaller::run($ptApp, 'fiscal', 'getSequences');
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
$data['sequences'] = $apiResult->getContent();
......@@ -207,6 +211,13 @@ $app->any('/fiscal/',
}
});
$app->any('/fiscal/disconnect',
function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
fiscalTpl($ptApp, $response, 'login.php');
return $response; // See login middleware for the auth destruction
});
$app->POST('/fiscal/createarchive',
function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
......
......@@ -24,12 +24,16 @@ $app->POST('/api/login', function ($request, $response, $args) {
$apiResult = APICaller::run($ptApp, 'login', 'login', $argsApi);
if ($apiResult->getStatus() == APIResult::STATUS_CALL_OK) {
// Set the cookie
$basePath = $request->getUri()->getBasePath();
if ($basePath == '') {
$basePath = '/';
}
$newToken = $apiResult->getContent();
$response = $response->withHeader(Login::TOKEN_HEADER, $newToken);
$cookie = SetCookie::create(Login::TOKEN_HEADER)
->withValue($newToken)
->withMaxAge($ptApp->getJwtTimeout())
->withPath($request->getUri()->getPath())
->withPath($basePath)
->withDomain($request->getUri()->getHost());
$response = FigResponseCookies::set($response, $cookie);
}
......
<?php
function render($ptApp, $data) {
$ret = '<h1>Consultation des données fiscale</h1>';
$ret = sprintf("<p>Connecté·e en tant que %s. <a href=\"./disconnect\">Déconnexion</a></p>", $data['user']);
$ret .= '<h1>Consultation des données fiscale</h1>';
$ret .= '<h2>Lister tous les tickets par caisse</h2>';
$ret .= '<ul>';
foreach ($data['sequences'] as $sequence) {
......
......@@ -176,6 +176,7 @@ class AppContext {
$this->dao = DAOFactory::getDAO($this->getDbInfo(),
array('debug' => $this->isDebugMode()));
}
public function getDbInfo() { return $this->dbInfo; }
public function getDao() { return $this->dao; }
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment