Project

General

Profile

Actions

Bug #92797

closed

Context API - Get the backend.user in TCA not possible. Bug?

Added by Mati Sediqi almost 4 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
-
Target version:
-
Start date:
2020-11-08
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
10
PHP Version:
7.4
Tags:
Complexity:
Is Regression:
Sprint Focus:

Description

Hey there,

I was playing around recently with the new Context API introduced in TYPO3 9.4 (feature #85389) on a 10.4.9 installation.
I've noticed that I can't access the backend.user inside TCA (to be specific, in /Configuration/TCA/Overrides/pages.php) - I guess it's due to caching of it..

The use-case why I need it is because I want to make an own Localization-Service which is inspired by Laravel.
They got like a folder structure as a global lang-directory which looks like:
- /lang/en/tt_content.json
- /lang/de/tt_content.json

and so on - also possible IIRC to leave .php-files - if not, it'd be easy anyways to implement that, which I've done so far.
This way it'd be a, personally, better overview instead of using the .xliff-files which is a mess of unnecessary XML-tags etc. for translations. Also it's more flexible and you can call it using a global-helper function called "__('key')" - of course that can be changed in TYPO3.

So back to the topic of this issue; is it actually at all possible to get the current backend.user - mainly for the purpose to fetch its current language UID in order to get the current language by the UID (e.g. 0 => "english" while "english" is mapped to "en", 1 => "deutsch" while "deutsch" is mapped to "de" etc) - or getting the User-Configuration by the current BE user, both would be helpful.

I hope I've clarified everything so far, feel free to ask anything about it.

Best regards,
Mati

Actions #1

Updated by Mati Sediqi almost 4 years ago

So a small update: I've written a little helper-class which reads the current session-id (accessible via cookie), a SQL query to fetch the "ses_userid"-column and then a findByUid using the BackendUserRepository. Not the best approach but it's honest work.

<?php

declare(strict_types=1);

namespace Vendor\Namespace\Helper;

use TYPO3\CMS\Beuser\Domain\Model\BackendUser;
use TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class BackendUserHelper
{
    /**
     * @var BackendUserRepository
     */
    protected static $backendUserRepository;

    /**
     * Fetch the current logged-in Backend-User by reading the cookie which
     * represents the logged-in BE-User's session-id and via queries it fetches
     * the uid of that one and additionally a findByUid using the BackendUserRepository.
     * 
     * @return BackendUser|null
     */
    public static function getUser()
    {
        $cookieSesId = $_COOKIE["be_typo_user"];

        if(!$cookieSesId) {
            return null;
        }

        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable("be_sessions");
        $res = $queryBuilder
            ->select("ses_userid")
            ->from("be_sessions")
            ->where(
                $queryBuilder->expr()->eq("ses_id", $queryBuilder->createNamedParameter($cookieSesId))
            )
        ->execute()
        ->fetch(0);

        if(!$res) {
            return null;
        }

        self::$backendUserRepository = GeneralUtility::makeInstance(BackendUserRepository::class);

        $sesUserId = $res["ses_userid"];

        $backendUser = self::$backendUserRepository->findByUid($sesUserId);
        return $backendUser;
    }
}
Actions #2

Updated by Benni Mack over 3 years ago

  • Status changed from New to Needs Feedback

Hey Mati,

I don't fully get your use case: You're in the TYPO3 Frontend and you want to get the authenticated Backend User? To get the backend users' language?

Actions #3

Updated by Mati Sediqi over 3 years ago

No, I was in the backend and tried to get the backend.user (via Context api) - primary its current language:
-> "(...) to be specific, in /Configuration/TCA/Overrides/pages.php - I guess it's due to caching of it.."

Honestly, after 7 months, I don't even think the issue is relevant at all no more.
If I remember correctly I've somehow found another way to get it within the "uc" of an user in the BE.

So can be closed, thanks though. :)

Actions #4

Updated by Christian Kuhn over 3 years ago

  • Status changed from Needs Feedback to Closed
Actions

Also available in: Atom PDF