shopwareshopware5

Get loggedin userinformation in extjs shopware backend


I know that I can access the javascript var userName everywhere in the extjs backend, cause its defined globally in the themes/Backend/ExtJs/backend/index/header.tpl, to get information about the logged in user. But is there a way to get userId or role of the user in some way (without ajax requests)?

//{block name="backend/order/view/list/list"}
//{$smarty.block.parent}
Ext.define('Shopware.apps.Test.view.List', {

    /**
     * Defines an override applied to a class.
     * @string
     */
    override: 'Shopware.apps.Order.view.list.List',

    getToolbar: function() {

        // get this
        var me = this;

        // get parent action column
        var result = me.callParent( arguments );

        console.log(userName);
    },
});
//{/block}
{block name="backend/base/header/javascript" append}
<script type="text/javascript">

    var currentTabState = 'active';
    window.addEventListener('blur', function () {
        currentTabState = 'inactive';
    });

    window.addEventListener('focus', function () {
        currentTabState = 'active';
    });

    var userName = '{$user->name}',
        maxParameterLength = '{$maxParameterLength}';


Solution

  • You need to make the user available as Smarty variable, so you can simply access the data from this object. For this you can subscribe to the PostDispatch event of the order backend controller Enlight_Controller_Action_PostDispatchSecure_Backend_Order in a subscriber and then you use the same code as in the Shopware_Controllers_Backend_Index to add the $user variable.

    //{block name="backend/order/view/list/list"}
    //{$smarty.block.parent}
    Ext.define('Shopware.apps.Test.view.List', {
    
        /**
         * Defines an override applied to a class.
         * @string
         */
        override: 'Shopware.apps.Order.view.list.List',
    
        getToolbar: function() {
    
            // get this
            var me = this;
    
            // get parent action column
            var result = me.callParent( arguments );
            var userId = '{$user->id}';
            var roleId = '{$user->role->getId()}';
    
            console.log(userName);
        },
    });
    //{/block}
    

    {$user->role} contains the Shopware\Models\User\Role model, so you can access all data from this object with the corresponding getters.

    If you want to know, which data you have available, just make a debug output in the \Shopware_Controllers_Backend_Index::indexAction method, right after the call $identity = $auth->getIdentity();

    echo '<pre>';
    \Doctrine\Common\Util\Debug::dump($identity);
    echo '</pre>';
    exit();