diff --git a/config/routes.php b/config/routes.php index beca12119..2505a17d7 100644 --- a/config/routes.php +++ b/config/routes.php @@ -12,6 +12,10 @@ '/toolbar/clear-cache', ['controller' => 'Toolbar', 'action' => 'clearCache'] ); + $routes->connect( + '/toolbar/clear-session', + ['controller' => 'Toolbar', 'action' => 'clearSession'] + ); $routes->connect( '/toolbar/*', ['controller' => 'Requests', 'action' => 'view'] diff --git a/src/Controller/ToolbarController.php b/src/Controller/ToolbarController.php index f41e008d4..a236b5e8d 100644 --- a/src/Controller/ToolbarController.php +++ b/src/Controller/ToolbarController.php @@ -53,4 +53,21 @@ public function clearCache(): void $this->set(compact('success', 'message')); $this->viewBuilder()->setOption('serialize', ['success', 'message']); } + + /** + * Clear the session. + * + * @return void + */ + public function clearSession(): void + { + $this->request->allowMethod('post'); + $session = $this->request->getSession(); + $session->destroy(); + + $success = true; + $message = 'Session cleared.'; + $this->set(compact('success', 'message')); + $this->viewBuilder()->setOption('serialize', ['success', 'message']); + } } diff --git a/templates/element/request_panel.php b/templates/element/request_panel.php index d036b01fa..16ae169fa 100644 --- a/templates/element/request_panel.php +++ b/templates/element/request_panel.php @@ -95,6 +95,20 @@

Session

+

+ +

+
Toolbar->dumpNode($session) ?>

No Session data.

diff --git a/tests/TestCase/Controller/ToolbarControllerTest.php b/tests/TestCase/Controller/ToolbarControllerTest.php index dddac6ac2..34722a909 100644 --- a/tests/TestCase/Controller/ToolbarControllerTest.php +++ b/tests/TestCase/Controller/ToolbarControllerTest.php @@ -70,4 +70,19 @@ public function testClearCache() $this->assertResponseOk(); $this->assertResponseContains('success'); } + + /** + * Test clearing the session. + * + * @return void + */ + public function testClearSession() + { + $this->session(['test' => 'value']); + $this->configRequest(['headers' => ['Accept' => 'application/json']]); + $this->post('/debug-kit/toolbar/clear-session'); + $this->assertResponseOk(); + $this->assertResponseContains('success'); + $this->assertResponseContains('Session cleared'); + } } diff --git a/webroot/js/main.js b/webroot/js/main.js index 35ba8273c..760bfb24d 100644 --- a/webroot/js/main.js +++ b/webroot/js/main.js @@ -6,6 +6,7 @@ import RoutesPanel from './modules/Panels/RoutesPanel.js'; import VariablesPanel from './modules/Panels/VariablesPanel.js'; import PackagesPanel from './modules/Panels/PackagesPanel.js'; import MailPanel from './modules/Panels/MailPanel.js'; +import RequestPanel from './modules/Panels/RequestPanel.js'; document.addEventListener('DOMContentLoaded', () => { const toolbar = Start.init(); @@ -14,6 +15,7 @@ document.addEventListener('DOMContentLoaded', () => { // Init Panels CachePanel.onEvent(); + RequestPanel.onEvent(); RoutesPanel.onEvent(); PackagesPanel.onEvent(); MailPanel.onEvent(); diff --git a/webroot/js/modules/Panels/RequestPanel.js b/webroot/js/modules/Panels/RequestPanel.js new file mode 100644 index 000000000..7e5da6145 --- /dev/null +++ b/webroot/js/modules/Panels/RequestPanel.js @@ -0,0 +1,41 @@ +export default (($) => { + const addMessage = (text) => { + $(`

${text}

`) + .appendTo('.c-request-panel__messages') + .fadeOut(2000); + }; + + const init = () => { + $('.js-clear-session').on('click', function triggerSessionClear(e) { + const el = $(this); + const baseUrl = el.attr('data-url'); + const csrf = el.attr('data-csrf'); + + $.ajax({ + headers: { 'X-CSRF-TOKEN': csrf }, + url: baseUrl, + dataType: 'json', + type: 'POST', + success(data) { + addMessage(data.message); + }, + error(jqXHR, textStatus, errorThrown) { + addMessage(errorThrown); + }, + }); + e.preventDefault(); + }); + }; + + const onEvent = () => { + document.addEventListener('initPanel', (e) => { + if (e.detail === 'panelrequest') { + init(); + } + }); + }; + + return { + onEvent, + }; +})(jQuery);