use Shaarli\History;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
use Shaarli\Security\LoginManager;
use Shaarli\Security\SessionManager;
/** @var LoginManager */
protected $login;
- public function __construct(ConfigManager $conf, SessionManager $session, LoginManager $login)
- {
+ /** @var string */
+ protected $webPath;
+
+ public function __construct(
+ ConfigManager $conf,
+ SessionManager $session,
+ LoginManager $login,
+ string $webPath
+ ) {
$this->conf = $conf;
$this->session = $session;
$this->login = $login;
+ $this->webPath = $webPath;
}
public function build(): ShaarliContainer
{
$container = new ShaarliContainer();
+
$container['conf'] = $this->conf;
$container['sessionManager'] = $this->session;
$container['loginManager'] = $this->login;
+ $container['webPath'] = $this->webPath;
+
$container['plugins'] = function (ShaarliContainer $container): PluginManager {
return new PluginManager($container->conf);
};
return new FormatterFactory($container->conf, $container->loginManager->isLoggedIn());
};
+ $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager {
+ return new PageCacheManager($container->conf->get('resource.page_cache'));
+ };
+
return $container;
}
}
use Shaarli\History;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
use Shaarli\Security\LoginManager;
use Shaarli\Security\SessionManager;
use Slim\Container;
* @property ConfigManager $conf
* @property SessionManager $sessionManager
* @property LoginManager $loginManager
+ * @property string $webPath
* @property History $history
* @property BookmarkServiceInterface $bookmarkService
* @property PageBuilder $pageBuilder
* @property PluginManager $pluginManager
* @property FormatterFactory $formatterFactory
+ * @property PageCacheManager $pageCacheManager
*/
class ShaarliContainer extends Container
{
--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller;
+
+use Shaarli\Security\LoginManager;
+use Slim\Http\Request;
+use Slim\Http\Response;
+
+/**
+ * Class LogoutController
+ *
+ * Slim controller used to logout the user.
+ * It invalidates page cache and terminate the user session. Then it redirects to the homepage.
+ *
+ * @package Front\Controller
+ */
+class LogoutController extends ShaarliController
+{
+ public function index(Request $request, Response $response): Response
+ {
+ $this->container->pageCacheManager->invalidateCaches();
+ $this->container->sessionManager->logout();
+
+ // TODO: switch to a simple Cookie manager allowing to check the session, and create mocks.
+ setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, $this->container->webPath);
+
+ return $response->withRedirect('./');
+ }
+}
}
// -------- User wants to logout.
if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout')) {
- invalidateCaches($conf->get('resource.page_cache'));
- $sessionManager->logout();
- setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, WEB_PATH);
- header('Location: ?');
+ header('Location: ./logout');
exit;
}
exit;
}
-$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager);
+$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager, WEB_PATH);
$container = $containerBuilder->build();
$app = new App($container);
use Shaarli\Formatter\FormatterFactory;
use Shaarli\History;
use Shaarli\Render\PageBuilder;
+use Shaarli\Render\PageCacheManager;
use Shaarli\Security\LoginManager;
use Shaarli\Security\SessionManager;
$this->loginManager = $this->createMock(LoginManager::class);
$this->loginManager->method('isLoggedIn')->willReturn(true);
- $this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager);
+ $this->containerBuilder = new ContainerBuilder(
+ $this->conf,
+ $this->sessionManager,
+ $this->loginManager,
+ 'UT web path'
+ );
}
public function testBuildContainer(): void
static::assertInstanceOf(ConfigManager::class, $container->conf);
static::assertInstanceOf(SessionManager::class, $container->sessionManager);
static::assertInstanceOf(LoginManager::class, $container->loginManager);
+ static::assertSame('UT web path', $container->webPath);
static::assertInstanceOf(History::class, $container->history);
static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService);
static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
static::assertInstanceOf(FormatterFactory::class, $container->formatterFactory);
+ static::assertInstanceOf(PageCacheManager::class, $container->pageCacheManager);
}
}
--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller;
+
+/** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
+if (!function_exists('Shaarli\Front\Controller\setcookie')) {
+ function setcookie(string $name, string $value): void {
+ $_COOKIE[$name] = $value;
+ }
+}
+
+use PHPUnit\Framework\TestCase;
+use Shaarli\Container\ShaarliContainer;
+use Shaarli\Render\PageCacheManager;
+use Shaarli\Security\LoginManager;
+use Shaarli\Security\SessionManager;
+use Slim\Http\Request;
+use Slim\Http\Response;
+
+class LogoutControllerTest extends TestCase
+{
+ /** @var ShaarliContainer */
+ protected $container;
+
+ /** @var LogoutController */
+ protected $controller;
+
+ public function setUp(): void
+ {
+ $this->container = $this->createMock(ShaarliContainer::class);
+ $this->controller = new LogoutController($this->container);
+
+ setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
+ }
+
+ public function testValidControllerInvoke(): void
+ {
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $pageCacheManager = $this->createMock(PageCacheManager::class);
+ $pageCacheManager->expects(static::once())->method('invalidateCaches');
+ $this->container->pageCacheManager = $pageCacheManager;
+
+ $sessionManager = $this->createMock(SessionManager::class);
+ $sessionManager->expects(static::once())->method('logout');
+ $this->container->sessionManager = $sessionManager;
+
+ static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
+
+ $result = $this->controller->index($request, $response);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertContains('./', $result->getHeader('Location'));
+ static::assertSame('false', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
+ }
+}
</li>
{if="$is_logged_in"}
<li class="pure-menu-item pure-u-lg-0 shaarli-menu-mobile" id="shaarli-menu-mobile-logout">
- <a href="./?do=logout" class="pure-menu-link">{'Logout'|t}</a>
+ <a href="/logout" class="pure-menu-link">{'Logout'|t}</a>
</li>
{else}
<li class="pure-menu-item pure-u-lg-0 shaarli-menu-mobile" id="shaarli-menu-mobile-login">
</li>
{else}
<li class="pure-menu-item" id="shaarli-menu-desktop-logout">
- <a href="./?do=logout" class="pure-menu-link" aria-label="{'Logout'|t}" title="{'Logout'|t}">
+ <a href="/logout" class="pure-menu-link" aria-label="{'Logout'|t}" title="{'Logout'|t}">
<i class="fa fa-sign-out" aria-hidden="true"></i>
</a>
</li>
{else}
<li><a href="{$titleLink}" class="nomobile">Home</a></li>
{if="$is_logged_in"}
- <li><a href="./?do=logout">Logout</a></li>
- <li><a href="./?do=tools">Tools</a></li>
- <li><a href="./?do=addlink">Add link</a></li>
+ <li><a href="/logout">Logout</a></li>
+ <li><a href="?do=tools">Tools</a></li>
+ <li><a href="?do=addlink">Add link</a></li>
{elseif="$openshaarli"}
<li><a href="./?do=tools">Tools</a></li>
<li><a href="./?do=addlink">Add link</a></li>