Skip to content

Commit

Permalink
Disable the "switch user" button if it would impersonate the original…
Browse files Browse the repository at this point in the history
… user (see #1581)

Description
-----------

| Q                | A
| -----------------| ---
| Fixed issues     | Fixes #1409
| Docs PR or issue | -

Commits
-------

05eab41 Disable the "switch user" button if it would impersonate the original user
  • Loading branch information
leofeyer committed Mar 27, 2020
1 parent 0f1f735 commit 3988b54
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -138,6 +138,7 @@
"contao/manager-plugin": "<2.0 || >=3.0",
"doctrine/persistence": "1.3.2",
"symfony/config": "<4.4.2",
"symfony/security-bundle": "4.4.* <4.4.5",
"terminal42/contao-ce-access": "<3.0",
"zendframework/zend-code": "<3.3.1"
},
Expand Down
1 change: 1 addition & 0 deletions core-bundle/composer.json
Expand Up @@ -110,6 +110,7 @@
"contao/core": "*",
"contao/manager-bundle": "4.5.* <4.5.2",
"contao/manager-plugin": "<2.0 || >=3.0",
"symfony/security-bundle": "4.4.* <4.4.5",
"terminal42/contao-ce-access": "<3.0"
},
"require-dev": {
Expand Down
35 changes: 33 additions & 2 deletions core-bundle/src/Resources/contao/dca/tl_user.php
Expand Up @@ -470,6 +470,11 @@
*/
class tl_user extends Contao\Backend
{
/**
* @var int
*/
private static $origUserId;

/**
* Import the back end user object
*/
Expand Down Expand Up @@ -700,14 +705,40 @@ public function deleteUser($row, $href, $label, $title, $icon, $attributes)
*/
public function switchUser($row, $href, $label, $title, $icon)
{
$authorizationChecker = Contao\System::getContainer()->get('security.authorization_checker');
$security = Contao\System::getContainer()->get('security.helper');

if (!$authorizationChecker->isGranted('ROLE_ALLOWED_TO_SWITCH') || $authorizationChecker->isGranted('ROLE_PREVIOUS_ADMIN'))
if (!$security->isGranted('ROLE_ALLOWED_TO_SWITCH'))
{
return '';
}

$disabled = false;

if ($this->User->id == $row['id'])
{
$disabled = true;
}
elseif ($security->isGranted('ROLE_PREVIOUS_ADMIN'))
{
if (self::$origUserId === null)
{
/** @var Symfony\Component\Security\Core\Authentication\Token\TokenInterface $origToken */
$origToken = $security->getToken()->getOriginalToken();
$origUser = $origToken->getUser();

if ($origUser instanceof Contao\BackendUser)
{
self::$origUserId = $origUser->id;
}
}

if (self::$origUserId == $row['id'])
{
$disabled = true;
}
}

if ($disabled)
{
return Contao\Image::getHtml(preg_replace('/\.svg$/i', '_.svg', $icon)) . ' ';
}
Expand Down
3 changes: 2 additions & 1 deletion manager-bundle/composer.json
Expand Up @@ -63,7 +63,8 @@
"symfony/phpunit-bridge": "4.4.*"
},
"conflict": {
"symfony/config": "<4.4.2"
"symfony/config": "<4.4.2",
"symfony/security-bundle": "4.4.* <4.4.5"
},
"suggest": {
"contao/tcpdf-bundle": "To export articles as PDF files"
Expand Down

0 comments on commit 3988b54

Please sign in to comment.