Skip to content

Commit

Permalink
Add "Original User" button to User Switcher. Make user select height …
Browse files Browse the repository at this point in the history
…dynamic relative to panel size.
  • Loading branch information
adrianbj committed Sep 26, 2024
1 parent 3ccfffc commit 647f065
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
22 changes: 20 additions & 2 deletions TracyDebugger.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function getModuleInfo() {
'summary' => __('Tracy debugger from Nette with many PW specific custom tools.', __FILE__),
'author' => 'Adrian Jones',
'href' => 'https://processwire.com/talk/forum/58-tracy-debugger/',
'version' => '4.26.36',
'version' => '4.26.37',
'autoload' => 100000, // in PW 3.0.114+ higher numbers are loaded first - we want Tracy first
'singular' => true,
'requires' => 'ProcessWire>=2.7.2, PHP>=5.4.4',
Expand Down Expand Up @@ -1801,6 +1801,12 @@ public function ready() {
$pass = new Password();
$challenge = $pass->randomBase64String(32);
$this->wire('session')->tracyUserSwitcherId = $challenge;

$configData = $this->wire('modules')->getModuleConfigData("TracyDebugger");
$this->data['originalUserSwitcher'][$this->wire('session')->tracyUserSwitcherId] = $this->wire('user')->name;
$configData['originalUserSwitcher'] = $this->data['originalUserSwitcher'];
$this->wire('modules')->saveModuleConfigData($this, $configData);

}
// save session ID and expiry time in module config settings
$configData = $this->wire('modules')->getModuleConfigData("TracyDebugger");
Expand Down Expand Up @@ -1831,14 +1837,26 @@ public function ready() {
$this->wire('modules')->saveModuleConfigData($this, $configData);
$this->wire('session')->redirect($this->httpReferer);
}
// if session not expired, switch to original user
elseif($this->wire('input')->post->revertOriginalUserSwitcher && $this->wire('session')->CSRF->validate()) {
if(isset($this->data['userSwitchSession'][$this->wire('session')->tracyUserSwitcherId]) && $this->data['userSwitchSession'][$this->wire('session')->tracyUserSwitcherId] > time() && $this->wire('session')->tracyUserSwitcherId) {
// if session variable exists, grab it and add to the new session after logging out
// and forceLogin the original user
$tracyUserSwitcherId = $this->wire('session')->tracyUserSwitcherId;
if($this->wire('user')->isLoggedin()) $this->wire('session')->logout();
$this->wire('session')->forceLogin($this->data['originalUserSwitcher'][$tracyUserSwitcherId]);
$this->wire('session')->tracyUserSwitcherId = $tracyUserSwitcherId;
}
$this->wire('session')->redirect($this->httpReferer);
}
// if session not expired, switch to requested user
elseif($this->wire('input')->post->userSwitcher && $this->wire('session')->CSRF->validate()) {
if(isset($this->data['userSwitchSession'][$this->wire('session')->tracyUserSwitcherId]) && $this->data['userSwitchSession'][$this->wire('session')->tracyUserSwitcherId] > time() && $this->wire('session')->tracyUserSwitcherId) {
// if session variable exists, grab it and add to the new session after logging out
// and forceLogin the new switched user
$tracyUserSwitcherId = $this->wire('session')->tracyUserSwitcherId;
if($this->wire('user')->isLoggedin()) $this->wire('session')->logout();
$user = $this->wire('session')->forceLogin($this->wire('input')->post->userSwitcher);
$this->wire('session')->forceLogin($this->wire('input')->post->userSwitcher);
$this->wire('session')->tracyUserSwitcherId = $tracyUserSwitcherId;
}
$this->wire('session')->redirect($this->httpReferer);
Expand Down
12 changes: 8 additions & 4 deletions panels/RequestInfoPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public function getPanel() {
</tr>
<tr>
<td>process</td>
<td>'.$this->wire('page')->process.'</td>
<td>'.(isset($this->wire('page')->process) ? $this->wire('page')->process : $this->wire('process')).'</td>
</tr>
<tr>
<td>class</td>
Expand Down Expand Up @@ -511,15 +511,19 @@ public function getPanel() {
<td>'.date("Y-m-d H:i:s", $p->modified).'</td>
</tr>
<tr>
<td>Hidden (status)</td>
<td>Hidden</td>
<td>'. ($p->isHidden() ? "" : "") .'</td>
</tr>
<tr>
<td>Unpublished (status)</td>
<td>Unpublished</td>
<td>'. ($p->isUnpublished() ? "" : "") .'</td>
</tr>
<tr>
<td>Locked (status)</td>
<td>Trashed</td>
<td>'. ($p->isTrash() ? "" : "") .'</td>
</tr>
<tr>
<td>Locked</td>
<td>'. ($p->is(Page::statusLocked) ? "" : "") .'</td>
</tr>
</table>';
Expand Down
13 changes: 10 additions & 3 deletions panels/UserSwitcherPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getPanel() {

$out = '
<h1>' . $this->icon . ' User Switcher</h1>
<div class="tracy-inner">
<div id="user-switcher-wrapper" class="tracy-inner">
<h2>' . $this->wire('user')->name . '</h2>
<p>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="16px" height="16px" viewBox="0 0 548.172 548.172" style="enable-background:new 0 0 548.172 548.172;" xml:space="preserve" style="height:16px">
Expand Down Expand Up @@ -81,7 +81,7 @@ public function getPanel() {

if(\TracyDebugger::$allowedSuperuser || $remainingSessionLength > 0) {
$out .= '
<select onchange="this.form.submit()" name="userSwitcher" size="5" style="width:100% !important; height:90px !important">';
<select id="userSwitcher" onchange="this.form.submit()" name="userSwitcher" size="5" style="width:100% !important; min-height:105px !important">';
if(!$this->wire('user')->isLoggedin()) $out .= '<option value="guest" style="padding: 2px; background:'.TracyDebugger::COLOR_WARN.'; color: #FFFFFF;" selected="selected">guest</option>';

if(\TracyDebugger::getDataValue('userSwitcherSelector')) {
Expand Down Expand Up @@ -114,10 +114,17 @@ public function getPanel() {
}
$out .= '
</select>
</p>';
</p>
<script>
(new MutationObserver(() => {
document.getElementById("userSwitcher").setAttribute("style","width:100% !important; min-height:105px !important; height:" + (document.getElementById("user-switcher-wrapper").clientHeight - 175) + "px !important");
})).observe(document.getElementById("tracy-debug-panel-UserSwitcherPanel"), { attributes: true, attributeFilter: ["style"] });
</script>
';
}

if($this->wire('user')->isLoggedin()) $out .= '<input type="submit" name="logoutUserSwitcher" value="Logout to Guest" />&nbsp;';
if($this->switchedUser && \TracyDebugger::getDataValue('originalUserSwitcher')) $out .= '<input type="submit" name="revertOriginalUserSwitcher" value="Original User" />&nbsp;';
if($this->switchedUser) $out .= '<input type="submit" name="endSessionUserSwitcher" value="End Session" />';

$out .= '
Expand Down

0 comments on commit 647f065

Please sign in to comment.