Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Show "switch user" button for non-admin users #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
80 changes: 80 additions & 0 deletions Classes/Controller/SwitchUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace KoninklijkeCollective\MyUserManagement\Controller;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Backend\Authentication\Event\SwitchUserEvent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\SysLog\Type;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

/**
* Provide modified version of "switchUserAction" to allow non-admin users to switch users
*/
class SwitchUserController extends \TYPO3\CMS\Backend\Controller\SwitchUserController
{
public function switchUserAction(ServerRequestInterface $request): ResponseInterface
{
$currentUser = $this->getBackendUserAuthentication();
$targetUserId = (int)($request->getParsedBody()['targetUser'] ?? 0);

// Removed condition part "|| !$currentUser->isAdmin()" to allow non-admin users to switch user
if (!$targetUserId
|| $targetUserId === (int)($currentUser->user[$currentUser->userid_column] ?? 0)
|| $currentUser->getOriginalUserIdWhenInSwitchUserMode() !== null
) {
return $this->jsonResponse(['success' => false]);
}

$targetUser = BackendUtility::getRecord('be_users', $targetUserId, '*', BackendUtility::BEenableFields('be_users'));
if ($targetUser === null || $targetUser['admin'] === 1 || $targetUser['disable'] === 1) {
return $this->jsonResponse(['success' => false]);
}

$currentUser->uc['recentSwitchedToUsers'] = $this->generateListOfMostRecentSwitchedUsers($targetUserId);
$currentUser->writeUC();

// Write user switch to log
$currentUser->writelog(Type::LOGIN, 2, 0, 1, 'User %s switched to user %s (be_users:%s)', [
$currentUser->user[$currentUser->username_column] ?? '',
$targetUser['username'] ?? '',
$targetUserId,
]);

$sessionObject = $currentUser->getSession();
$sessionObject->set('backuserid', (int)($currentUser->user[$currentUser->userid_column] ?? 0));
$sessionRecord = $sessionObject->toArray();
$sessionRecord['ses_userid'] = $targetUserId;
$this->sessionBackend->update($sessionObject->getIdentifier(), $sessionRecord);
// We must regenerate the internal session so the new ses_userid is present in the userObject
$currentUser->enforceNewSessionId();

$event = new SwitchUserEvent(
$currentUser->getSession()->getIdentifier(),
$targetUser,
(array)$currentUser->user
);
$this->eventDispatcher->dispatch($event);

return $this->jsonResponse([
'success' => true,
'url' => $this->uriBuilder->buildUriFromRoute('main')
]);
}
}
12 changes: 10 additions & 2 deletions Resources/Private/Partials/Override/BackendUser/IndexListRow.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
xmlns:core="http://typo3.org/ns/TYPO3/CMS/Core/ViewHelpers"
xmlns:be="http://typo3.org/ns/TYPO3/CMS/Backend/ViewHelpers"
xmlns:bu="http://typo3.org/ns/TYPO3/CMS/Beuser/ViewHelpers"
xmlns:um="http://typo3.org/ns/KoninklijkeCollective/MyUserManagement/ViewHelpers"
>

Expand Down Expand Up @@ -112,7 +111,16 @@
</f:else>
</f:if>
<um:security.isActionAllowed action="SWITCH_USER">
<bu:SwitchUser backendUser="{backendUser}" />
<f:if condition="{backendUser.uid} !== {currentUserUid} && !{backendUser.isAdministrator} && !{backendUser.isDisabled}">
<f:then>
<typo3-backend-switch-user targetUser="{backendUser.uid}">
<button type="button" class="btn btn-default" title="{f:translate(key: 'LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:switchBackMode')}">
<core:icon identifier="actions-system-backend-user-switch" size="small" />
</button>
</typo3-switch-user-button>
</f:then>
<f:else><span class="btn btn-default disabled"><core:icon identifier="empty-empty" size="small" /></span></f:else>
</f:if>
</um:security.isActionAllowed>
</div>
</td>
Expand Down
3 changes: 3 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Backend\Template\Components\ButtonBar']['getButtonsHook']['my_user_management'] =
ButtonBarHook::class . '->getButtons';

$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Backend\Controller\SwitchUserController::class] = [
'className' => \KoninklijkeCollective\MyUserManagement\Controller\SwitchUserController::class
];