Skip to content

Commit 3a310f3

Browse files
authored
Merge pull request #1066 from cakephp/feature/clear-session-button
Add clear session button to Request panel
2 parents 9f41398 + a6f8904 commit 3a310f3

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-0
lines changed

config/routes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
'/toolbar/clear-cache',
1313
['controller' => 'Toolbar', 'action' => 'clearCache']
1414
);
15+
$routes->connect(
16+
'/toolbar/clear-session',
17+
['controller' => 'Toolbar', 'action' => 'clearSession']
18+
);
1519
$routes->connect(
1620
'/toolbar/*',
1721
['controller' => 'Requests', 'action' => 'view']

src/Controller/ToolbarController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,21 @@ public function clearCache(): void
5353
$this->set(compact('success', 'message'));
5454
$this->viewBuilder()->setOption('serialize', ['success', 'message']);
5555
}
56+
57+
/**
58+
* Clear the session.
59+
*
60+
* @return void
61+
*/
62+
public function clearSession(): void
63+
{
64+
$this->request->allowMethod('post');
65+
$session = $this->request->getSession();
66+
$session->destroy();
67+
68+
$success = true;
69+
$message = 'Session cleared.';
70+
$this->set(compact('success', 'message'));
71+
$this->viewBuilder()->setOption('serialize', ['success', 'message']);
72+
}
5673
}

templates/element/request_panel.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@
9595

9696
<h4>Session</h4>
9797
<?php if (isset($session)) : ?>
98+
<p>
99+
<button
100+
class="o-button js-clear-session"
101+
data-url="<?= $this->Url->build([
102+
'plugin' => 'DebugKit',
103+
'controller' => 'Toolbar',
104+
'action' => 'clearSession',
105+
]) ?>"
106+
data-csrf="<?= $this->getRequest()->getAttribute('csrfToken') ?>"
107+
>
108+
Clear Session
109+
</button>
110+
</p>
111+
<div class="c-request-panel__messages"></div>
98112
<?= $this->Toolbar->dumpNode($session) ?>
99113
<?php else : ?>
100114
<p class="c-flash c-flash--info">No Session data.</p>

tests/TestCase/Controller/ToolbarControllerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,19 @@ public function testClearCache()
7070
$this->assertResponseOk();
7171
$this->assertResponseContains('success');
7272
}
73+
74+
/**
75+
* Test clearing the session.
76+
*
77+
* @return void
78+
*/
79+
public function testClearSession()
80+
{
81+
$this->session(['test' => 'value']);
82+
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
83+
$this->post('/debug-kit/toolbar/clear-session');
84+
$this->assertResponseOk();
85+
$this->assertResponseContains('success');
86+
$this->assertResponseContains('Session cleared');
87+
}
7388
}

webroot/js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import RoutesPanel from './modules/Panels/RoutesPanel.js';
66
import VariablesPanel from './modules/Panels/VariablesPanel.js';
77
import PackagesPanel from './modules/Panels/PackagesPanel.js';
88
import MailPanel from './modules/Panels/MailPanel.js';
9+
import RequestPanel from './modules/Panels/RequestPanel.js';
910

1011
document.addEventListener('DOMContentLoaded', () => {
1112
const toolbar = Start.init();
@@ -14,6 +15,7 @@ document.addEventListener('DOMContentLoaded', () => {
1415

1516
// Init Panels
1617
CachePanel.onEvent();
18+
RequestPanel.onEvent();
1719
RoutesPanel.onEvent();
1820
PackagesPanel.onEvent();
1921
MailPanel.onEvent();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export default (($) => {
2+
const addMessage = (text) => {
3+
$(`<p>${text}</p>`)
4+
.appendTo('.c-request-panel__messages')
5+
.fadeOut(2000);
6+
};
7+
8+
const init = () => {
9+
$('.js-clear-session').on('click', function triggerSessionClear(e) {
10+
const el = $(this);
11+
const baseUrl = el.attr('data-url');
12+
const csrf = el.attr('data-csrf');
13+
14+
$.ajax({
15+
headers: { 'X-CSRF-TOKEN': csrf },
16+
url: baseUrl,
17+
dataType: 'json',
18+
type: 'POST',
19+
success(data) {
20+
addMessage(data.message);
21+
},
22+
error(jqXHR, textStatus, errorThrown) {
23+
addMessage(errorThrown);
24+
},
25+
});
26+
e.preventDefault();
27+
});
28+
};
29+
30+
const onEvent = () => {
31+
document.addEventListener('initPanel', (e) => {
32+
if (e.detail === 'panelrequest') {
33+
init();
34+
}
35+
});
36+
};
37+
38+
return {
39+
onEvent,
40+
};
41+
})(jQuery);

0 commit comments

Comments
 (0)