Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
17 changes: 17 additions & 0 deletions src/Controller/ToolbarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
14 changes: 14 additions & 0 deletions templates/element/request_panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@

<h4>Session</h4>
<?php if (isset($session)) : ?>
<p>
<button
class="o-button js-clear-session"
data-url="<?= $this->Url->build([
'plugin' => 'DebugKit',
'controller' => 'Toolbar',
'action' => 'clearSession',
]) ?>"
data-csrf="<?= $this->getRequest()->getAttribute('csrfToken') ?>"
>
Clear Session
</button>
</p>
<div class="c-request-panel__messages"></div>
<?= $this->Toolbar->dumpNode($session) ?>
<?php else : ?>
<p class="c-flash c-flash--info">No Session data.</p>
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Controller/ToolbarControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 2 additions & 0 deletions webroot/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -14,6 +15,7 @@ document.addEventListener('DOMContentLoaded', () => {

// Init Panels
CachePanel.onEvent();
RequestPanel.onEvent();
RoutesPanel.onEvent();
PackagesPanel.onEvent();
MailPanel.onEvent();
Expand Down
41 changes: 41 additions & 0 deletions webroot/js/modules/Panels/RequestPanel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export default (($) => {
const addMessage = (text) => {
$(`<p>${text}</p>`)
.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);
Loading