aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--application/container/ContainerBuilder.php20
-rw-r--r--application/container/ShaarliContainer.php3
-rw-r--r--application/front/controllers/LogoutController.php31
-rw-r--r--index.php7
-rw-r--r--tests/container/ContainerBuilderTest.php10
-rw-r--r--tests/front/controller/LogoutControllerTest.php60
-rw-r--r--tpl/default/page.header.html4
-rw-r--r--tpl/vintage/page.header.html6
8 files changed, 128 insertions, 13 deletions
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php
index 99c12334..c5c4a2c3 100644
--- a/application/container/ContainerBuilder.php
+++ b/application/container/ContainerBuilder.php
@@ -11,6 +11,7 @@ use Shaarli\Formatter\FormatterFactory;
11use Shaarli\History; 11use Shaarli\History;
12use Shaarli\Plugin\PluginManager; 12use Shaarli\Plugin\PluginManager;
13use Shaarli\Render\PageBuilder; 13use Shaarli\Render\PageBuilder;
14use Shaarli\Render\PageCacheManager;
14use Shaarli\Security\LoginManager; 15use Shaarli\Security\LoginManager;
15use Shaarli\Security\SessionManager; 16use Shaarli\Security\SessionManager;
16 17
@@ -34,19 +35,30 @@ class ContainerBuilder
34 /** @var LoginManager */ 35 /** @var LoginManager */
35 protected $login; 36 protected $login;
36 37
37 public function __construct(ConfigManager $conf, SessionManager $session, LoginManager $login) 38 /** @var string */
38 { 39 protected $webPath;
40
41 public function __construct(
42 ConfigManager $conf,
43 SessionManager $session,
44 LoginManager $login,
45 string $webPath
46 ) {
39 $this->conf = $conf; 47 $this->conf = $conf;
40 $this->session = $session; 48 $this->session = $session;
41 $this->login = $login; 49 $this->login = $login;
50 $this->webPath = $webPath;
42 } 51 }
43 52
44 public function build(): ShaarliContainer 53 public function build(): ShaarliContainer
45 { 54 {
46 $container = new ShaarliContainer(); 55 $container = new ShaarliContainer();
56
47 $container['conf'] = $this->conf; 57 $container['conf'] = $this->conf;
48 $container['sessionManager'] = $this->session; 58 $container['sessionManager'] = $this->session;
49 $container['loginManager'] = $this->login; 59 $container['loginManager'] = $this->login;
60 $container['webPath'] = $this->webPath;
61
50 $container['plugins'] = function (ShaarliContainer $container): PluginManager { 62 $container['plugins'] = function (ShaarliContainer $container): PluginManager {
51 return new PluginManager($container->conf); 63 return new PluginManager($container->conf);
52 }; 64 };
@@ -81,6 +93,10 @@ class ContainerBuilder
81 return new FormatterFactory($container->conf, $container->loginManager->isLoggedIn()); 93 return new FormatterFactory($container->conf, $container->loginManager->isLoggedIn());
82 }; 94 };
83 95
96 $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager {
97 return new PageCacheManager($container->conf->get('resource.page_cache'));
98 };
99
84 return $container; 100 return $container;
85 } 101 }
86} 102}
diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php
index fdf2f77f..af62e574 100644
--- a/application/container/ShaarliContainer.php
+++ b/application/container/ShaarliContainer.php
@@ -10,6 +10,7 @@ use Shaarli\Formatter\FormatterFactory;
10use Shaarli\History; 10use Shaarli\History;
11use Shaarli\Plugin\PluginManager; 11use Shaarli\Plugin\PluginManager;
12use Shaarli\Render\PageBuilder; 12use Shaarli\Render\PageBuilder;
13use Shaarli\Render\PageCacheManager;
13use Shaarli\Security\LoginManager; 14use Shaarli\Security\LoginManager;
14use Shaarli\Security\SessionManager; 15use Shaarli\Security\SessionManager;
15use Slim\Container; 16use Slim\Container;
@@ -20,11 +21,13 @@ use Slim\Container;
20 * @property ConfigManager $conf 21 * @property ConfigManager $conf
21 * @property SessionManager $sessionManager 22 * @property SessionManager $sessionManager
22 * @property LoginManager $loginManager 23 * @property LoginManager $loginManager
24 * @property string $webPath
23 * @property History $history 25 * @property History $history
24 * @property BookmarkServiceInterface $bookmarkService 26 * @property BookmarkServiceInterface $bookmarkService
25 * @property PageBuilder $pageBuilder 27 * @property PageBuilder $pageBuilder
26 * @property PluginManager $pluginManager 28 * @property PluginManager $pluginManager
27 * @property FormatterFactory $formatterFactory 29 * @property FormatterFactory $formatterFactory
30 * @property PageCacheManager $pageCacheManager
28 */ 31 */
29class ShaarliContainer extends Container 32class ShaarliContainer extends Container
30{ 33{
diff --git a/application/front/controllers/LogoutController.php b/application/front/controllers/LogoutController.php
new file mode 100644
index 00000000..aba078c3
--- /dev/null
+++ b/application/front/controllers/LogoutController.php
@@ -0,0 +1,31 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Security\LoginManager;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class LogoutController
13 *
14 * Slim controller used to logout the user.
15 * It invalidates page cache and terminate the user session. Then it redirects to the homepage.
16 *
17 * @package Front\Controller
18 */
19class LogoutController extends ShaarliController
20{
21 public function index(Request $request, Response $response): Response
22 {
23 $this->container->pageCacheManager->invalidateCaches();
24 $this->container->sessionManager->logout();
25
26 // TODO: switch to a simple Cookie manager allowing to check the session, and create mocks.
27 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, $this->container->webPath);
28
29 return $response->withRedirect('./');
30 }
31}
diff --git a/index.php b/index.php
index 73d9e022..57e13d4e 100644
--- a/index.php
+++ b/index.php
@@ -604,10 +604,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
604 } 604 }
605 // -------- User wants to logout. 605 // -------- User wants to logout.
606 if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout')) { 606 if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout')) {
607 invalidateCaches($conf->get('resource.page_cache')); 607 header('Location: ./logout');
608 $sessionManager->logout();
609 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, WEB_PATH);
610 header('Location: ?');
611 exit; 608 exit;
612 } 609 }
613 610
@@ -1894,7 +1891,7 @@ if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=
1894 exit; 1891 exit;
1895} 1892}
1896 1893
1897$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager); 1894$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager, WEB_PATH);
1898$container = $containerBuilder->build(); 1895$container = $containerBuilder->build();
1899$app = new App($container); 1896$app = new App($container);
1900 1897
diff --git a/tests/container/ContainerBuilderTest.php b/tests/container/ContainerBuilderTest.php
index cc2eb37f..65647249 100644
--- a/tests/container/ContainerBuilderTest.php
+++ b/tests/container/ContainerBuilderTest.php
@@ -10,6 +10,7 @@ use Shaarli\Config\ConfigManager;
10use Shaarli\Formatter\FormatterFactory; 10use Shaarli\Formatter\FormatterFactory;
11use Shaarli\History; 11use Shaarli\History;
12use Shaarli\Render\PageBuilder; 12use Shaarli\Render\PageBuilder;
13use Shaarli\Render\PageCacheManager;
13use Shaarli\Security\LoginManager; 14use Shaarli\Security\LoginManager;
14use Shaarli\Security\SessionManager; 15use Shaarli\Security\SessionManager;
15 16
@@ -35,7 +36,12 @@ class ContainerBuilderTest extends TestCase
35 $this->loginManager = $this->createMock(LoginManager::class); 36 $this->loginManager = $this->createMock(LoginManager::class);
36 $this->loginManager->method('isLoggedIn')->willReturn(true); 37 $this->loginManager->method('isLoggedIn')->willReturn(true);
37 38
38 $this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager); 39 $this->containerBuilder = new ContainerBuilder(
40 $this->conf,
41 $this->sessionManager,
42 $this->loginManager,
43 'UT web path'
44 );
39 } 45 }
40 46
41 public function testBuildContainer(): void 47 public function testBuildContainer(): void
@@ -45,9 +51,11 @@ class ContainerBuilderTest extends TestCase
45 static::assertInstanceOf(ConfigManager::class, $container->conf); 51 static::assertInstanceOf(ConfigManager::class, $container->conf);
46 static::assertInstanceOf(SessionManager::class, $container->sessionManager); 52 static::assertInstanceOf(SessionManager::class, $container->sessionManager);
47 static::assertInstanceOf(LoginManager::class, $container->loginManager); 53 static::assertInstanceOf(LoginManager::class, $container->loginManager);
54 static::assertSame('UT web path', $container->webPath);
48 static::assertInstanceOf(History::class, $container->history); 55 static::assertInstanceOf(History::class, $container->history);
49 static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService); 56 static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService);
50 static::assertInstanceOf(PageBuilder::class, $container->pageBuilder); 57 static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
51 static::assertInstanceOf(FormatterFactory::class, $container->formatterFactory); 58 static::assertInstanceOf(FormatterFactory::class, $container->formatterFactory);
59 static::assertInstanceOf(PageCacheManager::class, $container->pageCacheManager);
52 } 60 }
53} 61}
diff --git a/tests/front/controller/LogoutControllerTest.php b/tests/front/controller/LogoutControllerTest.php
new file mode 100644
index 00000000..d9ca1c25
--- /dev/null
+++ b/tests/front/controller/LogoutControllerTest.php
@@ -0,0 +1,60 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7/** Override PHP builtin setcookie function in the local namespace to mock it... more or less */
8if (!function_exists('Shaarli\Front\Controller\setcookie')) {
9 function setcookie(string $name, string $value): void {
10 $_COOKIE[$name] = $value;
11 }
12}
13
14use PHPUnit\Framework\TestCase;
15use Shaarli\Container\ShaarliContainer;
16use Shaarli\Render\PageCacheManager;
17use Shaarli\Security\LoginManager;
18use Shaarli\Security\SessionManager;
19use Slim\Http\Request;
20use Slim\Http\Response;
21
22class LogoutControllerTest extends TestCase
23{
24 /** @var ShaarliContainer */
25 protected $container;
26
27 /** @var LogoutController */
28 protected $controller;
29
30 public function setUp(): void
31 {
32 $this->container = $this->createMock(ShaarliContainer::class);
33 $this->controller = new LogoutController($this->container);
34
35 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there');
36 }
37
38 public function testValidControllerInvoke(): void
39 {
40 $request = $this->createMock(Request::class);
41 $response = new Response();
42
43 $pageCacheManager = $this->createMock(PageCacheManager::class);
44 $pageCacheManager->expects(static::once())->method('invalidateCaches');
45 $this->container->pageCacheManager = $pageCacheManager;
46
47 $sessionManager = $this->createMock(SessionManager::class);
48 $sessionManager->expects(static::once())->method('logout');
49 $this->container->sessionManager = $sessionManager;
50
51 static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
52
53 $result = $this->controller->index($request, $response);
54
55 static::assertInstanceOf(Response::class, $result);
56 static::assertSame(302, $result->getStatusCode());
57 static::assertContains('./', $result->getHeader('Location'));
58 static::assertSame('false', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]);
59 }
60}
diff --git a/tpl/default/page.header.html b/tpl/default/page.header.html
index 116265a5..2086aeb0 100644
--- a/tpl/default/page.header.html
+++ b/tpl/default/page.header.html
@@ -56,7 +56,7 @@
56 </li> 56 </li>
57 {if="$is_logged_in"} 57 {if="$is_logged_in"}
58 <li class="pure-menu-item pure-u-lg-0 shaarli-menu-mobile" id="shaarli-menu-mobile-logout"> 58 <li class="pure-menu-item pure-u-lg-0 shaarli-menu-mobile" id="shaarli-menu-mobile-logout">
59 <a href="./?do=logout" class="pure-menu-link">{'Logout'|t}</a> 59 <a href="/logout" class="pure-menu-link">{'Logout'|t}</a>
60 </li> 60 </li>
61 {else} 61 {else}
62 <li class="pure-menu-item pure-u-lg-0 shaarli-menu-mobile" id="shaarli-menu-mobile-login"> 62 <li class="pure-menu-item pure-u-lg-0 shaarli-menu-mobile" id="shaarli-menu-mobile-login">
@@ -88,7 +88,7 @@
88 </li> 88 </li>
89 {else} 89 {else}
90 <li class="pure-menu-item" id="shaarli-menu-desktop-logout"> 90 <li class="pure-menu-item" id="shaarli-menu-desktop-logout">
91 <a href="./?do=logout" class="pure-menu-link" aria-label="{'Logout'|t}" title="{'Logout'|t}"> 91 <a href="/logout" class="pure-menu-link" aria-label="{'Logout'|t}" title="{'Logout'|t}">
92 <i class="fa fa-sign-out" aria-hidden="true"></i> 92 <i class="fa fa-sign-out" aria-hidden="true"></i>
93 </a> 93 </a>
94 </li> 94 </li>
diff --git a/tpl/vintage/page.header.html b/tpl/vintage/page.header.html
index ce938421..8b9db353 100644
--- a/tpl/vintage/page.header.html
+++ b/tpl/vintage/page.header.html
@@ -18,9 +18,9 @@
18{else} 18{else}
19<li><a href="{$titleLink}" class="nomobile">Home</a></li> 19<li><a href="{$titleLink}" class="nomobile">Home</a></li>
20 {if="$is_logged_in"} 20 {if="$is_logged_in"}
21 <li><a href="./?do=logout">Logout</a></li> 21 <li><a href="/logout">Logout</a></li>
22 <li><a href="./?do=tools">Tools</a></li> 22 <li><a href="?do=tools">Tools</a></li>
23 <li><a href="./?do=addlink">Add link</a></li> 23 <li><a href="?do=addlink">Add link</a></li>
24 {elseif="$openshaarli"} 24 {elseif="$openshaarli"}
25 <li><a href="./?do=tools">Tools</a></li> 25 <li><a href="./?do=tools">Tools</a></li>
26 <li><a href="./?do=addlink">Add link</a></li> 26 <li><a href="./?do=addlink">Add link</a></li>