Mit dem folgenden Beispiel ist der Wechsel zwischen unterschiedlichen Benutzern im Frontend (fe_users) möglich. Ähnlich der Funktion "Benutzer simulieren" im Backend. Hierbei wird die uid des ursprünglichen (initial eingeloggten) Benutzers über sämtliche Wechselvorgänge hinweg in der Session gehalten (tx_ext_originuser), um ein späteres Rückschalten zum ursprünglichen Benutzer zu ermöglichen. Zu beachten ist hierbei, dass dieser Wert daher nur in Session Daten vom Typ "ses" über die Wechselvorgänge hinweg erhalten bleibt (Session Daten vom Typ "user" werden bei Wechsel jeweils überschrieben). Der initiale Wechsel (sowie weitere nachfolgende) ist in diesem Beispiel nur für Benutzer einer Gruppe "Administratoren" möglich.

Extbase Controller

<?php

namespace Vendor\Ext\Controller;

use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use Vendor\Ext\Exception\NotAuthorizedException;

/**
 * FrontendUserController
 */
class FrontendUserController extends ActionController {

    /**
     * action switch
     *
     * @param integer $uid
     * @return void
     */
    public function switchAction(int $uid = 0) {

        if(!$this->settings['enableFrontendUserSwitch'] == 1) {
            throw new NotAuthorizedException('Not authorized to switch user.', 1574892512 );
        }

        /* @var $frontendUserAuthentication FrontendUserAuthentication */
        $frontendUserAuthentication = $GLOBALS['TSFE']->fe_user;
        $frontendUserGroups = array_values($frontendUserAuthentication->groupData['uid']);

        // Only switch user when originally had logged in as member of "administrators" or else if user had already been switched before
        if(in_array($this->settings['uidUserGroupAdministrators'], $frontendUserGroups) || $frontendUserAuthentication->getKey('ses', 'tx_ext_originuser') > 0) {

            // Store uid of origin user in session to be able to switch back to it later or to do any additional switching
            if(!$frontendUserAuthentication->getKey('ses', 'tx_ext_originuser')) {
                $frontendUserAuthentication->setKey('ses', 'tx_ext_originuser', $frontendUserAuthentication->user['uid']);
                $frontendUserAuthentication->storeSessionData();
            }

            // Set new user
            $frontendUserAuthentication->checkPid = false;
            $frontendUserDataRaw = $this->fetchFrontendUserDataRaw((int) $uid);
            $frontendUserAuthentication->user = $frontendUserDataRaw;
            $frontendUserAuthentication->createUserSession($frontendUserDataRaw);
            $frontendUserAuthentication->storeSessionData();

            $this->addFlashMessage('FrontendUser has successfully been switched.', '', AbstractMessage::OK);

        }

        $this->redirect(null, null, null, null, 1);
    }

    /**
     * action showFrontendUserSwitch
     *
     * @return void
     */
    public function showFrontendUserSwitchAction() {

        $showFrontendUserSwitch = false;
        /* @var $frontendUserAuthentication FrontendUserAuthentication */
        $frontendUserAuthentication = $GLOBALS['TSFE']->fe_user;
        $frontendUserGroups = array_values($frontendUserAuthentication->groupData['uid']);

        // Set flag for fluid view to show or not to show frontend user switch
        if($this->settings['enableFrontendUserSwitch'] == 1 && (in_array($this->settings['uidUserGroupAdministrators'], $frontendUserGroups) || $frontendUserAuthentication->getKey('ses', 'tx_ext_originuser') > 0)) {
            $showFrontendUserSwitch = true;
        }

        // Fetch some users for fluid view to generate a switching navigation or similar based on them
        $frontendUsers['administrators'] = $this->frontendUsersRepository->findByUserGroupAdministrators();
        $frontendUsers['sales'] = $this->frontendUsersRepository->findByUserGroupSales();
        $frontendUsers['support'] = $this->frontendUsersRepository->findByUserGroupSupport();

        $this->view->assign('frontendUsers', $frontendUsers);
        $this->view->assign('showFrontendUserSwitch', $showFrontendUserSwitch);
    }

    /**
     * fetch frontend user data raw
     *
     * @param integer $uid
     * @return array
     */
    private function fetchFrontendUserDataRaw(int $uid) {
        /* @var $query QueryBuilder */
        $query = $this->objectManager->get(ConnectionPool::class)->getQueryBuilderForTable('fe_users');
        $query->getRestrictions()->removeAll()->add($this->objectManager->get(DeletedRestriction::class));
        $query->select('*')->from('fe_users')->andWhere($query->expr()->eq('uid', $query->createNamedParameter((int) $uid)));
        return $query->execute()->fetch();
    }

}




Kommentare