Skip to content

Commit

Permalink
[BUGFIX] Avoid unnecessary nesting in typo3-backend-switch-user
Browse files Browse the repository at this point in the history
The web component should behave like a button, there is no need
to have another button nested inside.

Resolves: #100513
Releases: main
Change-Id: I9ff0f64f751cdbe5968788515cff324777957dbc
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78521
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Oliver Bartsch <bo@cedev.de>
  • Loading branch information
benjaminkott authored and o-ba committed Apr 6, 2023
1 parent 918d0b4 commit 216607b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 64 deletions.
1 change: 0 additions & 1 deletion Build/Sources/Sass/backend.scss
Expand Up @@ -57,7 +57,6 @@
@import "component/recordlist";
@import "component/resources";
@import "component/recordsearchbox";
@import "component/switchuser";

//
// Elements
Expand Down
7 changes: 0 additions & 7 deletions Build/Sources/Sass/component/_switchuser.scss

This file was deleted.

31 changes: 26 additions & 5 deletions Build/Sources/TypeScript/backend/switch-user.ts
Expand Up @@ -11,7 +11,7 @@
* The TYPO3 project - inspiring people to share!
*/

import { html, TemplateResult, LitElement } from 'lit';
import { html, css, TemplateResult, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators';
import AjaxRequest from '@typo3/core/ajax/ajax-request';
import { AjaxResponse } from '@typo3/core/ajax/ajax-response';
Expand All @@ -26,25 +26,46 @@ enum Modes {
* Module: @typo3/backend/switch-user
*
* @example
* <typo3-switch-user targetUser="123" mode="switch">
* <button>Switch user</button>
* <typo3-switch-user class="some" targetUser="123" mode="switch">
* Switch user
* </typo3-switch-user>
*/
@customElement('typo3-backend-switch-user')
export class SwitchUser extends LitElement {
static styles = [css`:host { cursor: pointer; appearance: button; }`];

@property({ type: String }) targetUser: string;
@property({ type: Modes }) mode: Modes = Modes.switch;

public constructor() {
super();
this.addEventListener('click', (e: Event): void => {
e.preventDefault();
this.addEventListener('click', (event: Event): void => {
event.preventDefault();
if (this.mode === Modes.switch) {
this.handleSwitchUser();
} else if (this.mode === Modes.exit) {
this.handleExitSwitchUser();
}
});
this.addEventListener('keydown', (event: KeyboardEvent): void => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
if (this.mode === Modes.switch) {
this.handleSwitchUser();
} else if (this.mode === Modes.exit) {
this.handleExitSwitchUser();
}
}
});
}

public connectedCallback(): void {
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'button');
}
if (!this.hasAttribute('tabindex')) {
this.setAttribute('tabindex', '0');
}
}

protected render(): TemplateResult {
Expand Down
Expand Up @@ -39,21 +39,20 @@ <h3 class="dropdown-headline"><f:translate key="LLL:EXT:backend/Resources/Privat
<ul class="modulemenu-group-container">
<f:for each="{recentUsers}" as="user">
<li>
<typo3-backend-switch-user mode="switch" targetUser="{user.uid}">
<button
type="button"
class="dropdown-item"
title="{f:translate(key: 'LLL:EXT:backend/Resources/Private/Language/locallang.xlf:usermodule.su.tooltip', arguments: '{0: user.username}')}"
>
<span class="dropdown-item-columns">
<span class="dropdown-item-column dropdown-item-column-icon" aria-hidden="true">
<be:avatar backendUser="{user.uid}" size="16" />
</span>
<span class="dropdown-item-column dropdown-item-column-title">
{f:if(condition: user.realName, then: user.realName, else: user.username)}
</span>
<typo3-backend-switch-user
class="dropdown-item"
mode="switch"
targetUser="{user.uid}"
title="{f:translate(key: 'LLL:EXT:backend/Resources/Private/Language/locallang.xlf:usermodule.su.tooltip', arguments: '{0: user.username}')}"
>
<span class="dropdown-item-columns">
<span class="dropdown-item-column dropdown-item-column-icon" aria-hidden="true">
<be:avatar backendUser="{user.uid}" size="16" />
</span>
</button>
<span class="dropdown-item-column dropdown-item-column-title">
{f:if(condition: user.realName, then: user.realName, else: user.username)}
</span>
</span>
</typo3-backend-switch-user>
</li>
</f:for>
Expand All @@ -62,17 +61,17 @@ <h3 class="dropdown-headline"><f:translate key="LLL:EXT:backend/Resources/Privat
</f:if>
<f:if condition="{switchUserMode}">
<f:then>
<typo3-backend-switch-user mode="exit">
<button type="button" class="dropdown-item">
<span class="dropdown-item-columns">
<span class="dropdown-item-column dropdown-item-column-icon" aria-hidden="true">
<core:icon identifier="actions-logout" size="small" alternativeMarkupIdentifier="inline"/>
</span>
<span class="dropdown-item-column dropdown-item-column-title">
{f:translate(key: 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.exitSwitchUser')}
</span>
<typo3-backend-switch-user
class="dropdown-item"
mode="exit">
<span class="dropdown-item-columns">
<span class="dropdown-item-column dropdown-item-column-icon" aria-hidden="true">
<core:icon identifier="actions-logout" size="small" alternativeMarkupIdentifier="inline"/>
</span>
</button>
<span class="dropdown-item-column dropdown-item-column-title">
{f:translate(key: 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.exitSwitchUser')}
</span>
</span>
</typo3-backend-switch-user>
</f:then>
<f:else>
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/backend/Resources/Public/Css/backend.css

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 18 additions & 24 deletions typo3/sysext/beuser/Classes/ViewHelpers/SwitchUserViewHelper.php
Expand Up @@ -23,39 +23,30 @@
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;

/**
* Displays 'SwitchUser' button to change current backend user to target backend user.
*
* @internal
*/
final class SwitchUserViewHelper extends AbstractViewHelper
final class SwitchUserViewHelper extends AbstractTagBasedViewHelper
{
use CompileWithRenderStatic;

/**
* As this ViewHelper renders HTML, the output must not be escaped.
*
* @var bool
* @var string
*/
protected $escapeOutput = false;
protected $tagName = 'typo3-backend-switch-user';

public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('backendUser', BackendUser::class, 'Target backendUser to switch active session to', true);
$this->registerUniversalTagAttributes();
}

/**
* Render link with sprite icon to change current backend user to target
*
* @param array{backendUser: BackendUser} $arguments
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
public function render(): string
{
$targetUser = $arguments['backendUser'];
$targetUser = $this->arguments['backendUser'];
$currentUser = self::getBackendUserAuthentication();
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);

Expand All @@ -64,15 +55,18 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
|| !$currentUser->isAdmin()
|| $currentUser->getOriginalUserIdWhenInSwitchUserMode() !== null
) {
return '<span class="btn btn-default disabled">' . $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render() . '</span>';
$this->tag->setTagName('span');
$this->tag->addAttribute('class', $this->tag->getAttribute('class') . ' disabled');
$this->tag->addAttribute('disabled', 'disabled');
$this->tag->setContent($iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render());
} else {
$this->tag->addAttribute('title', self::getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:switchBackMode'));
$this->tag->addAttribute('targetUser', (string)$targetUser->getUid());
$this->tag->setContent($iconFactory->getIcon('actions-system-backend-user-switch', Icon::SIZE_SMALL)->render());
}

return '
<typo3-backend-switch-user targetUser="' . htmlspecialchars((string)$targetUser->getUid()) . '">
<button type="button" class="btn btn-default" title="' . htmlspecialchars(self::getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:switchBackMode')) . '">'
. $iconFactory->getIcon('actions-system-backend-user-switch', Icon::SIZE_SMALL)->render() .
'</button>
</typo3-switch-user-button>';
$this->tag->forceClosingTag(true);
return $this->tag->render();
}

protected static function getBackendUserAuthentication(): BackendUserAuthentication
Expand Down
Expand Up @@ -121,7 +121,7 @@
</f:link.action>
</f:else>
</f:if>
<bu:SwitchUser backendUser="{backendUser}" />
<bu:SwitchUser class="btn btn-default" backendUser="{backendUser}" />
</div>
</td>
</tr>
Expand Down

0 comments on commit 216607b

Please sign in to comment.