1

I have implemented an EventListener in TYPO3 that generates a usergroup for each newly registered user (see my last post here). In addition, a folder is created on the file system for each usergroup during the registration process. I am utilizing the fal_securedownload extension to manage access restrictions within my TYPO3 environment and therefore have set up a non public file storage outside the root.

I am looking for a way to automatically assign a usergroup (or ideally, directly assign a user) to their specific folder upon registration. By doing so, I aim to ensure that users can only view and access files within their own folder.

I think that i would have to add the folder path and group id to the tx_falsecuredownload_folder table. Unfortunately my code i wrote doesn't work. I tried connecting to the table, add an entry after generating md5 hash. But the usergroup doesn't get added to the folder in folder permissions.

I greatly appreciate any insights, suggestions, or code examples you can provide. Thank you in advance!

Here is a snippet of the code I have implemented in the EventListener:

<?php

namespace xxx\yyy\EventListener;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUserGroup;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3\CMS\Core\Database\ConnectionPool;

class FinalCreateEventListener
{
    protected $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function __invoke($event): void
    {
        $feUser = $event->getUser();

        // Load the needed Repos
        $groupRepository = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository::class);
        $frontendUserRepository = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository::class);
        $persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);

        // Create the new Group and set the properties
        $userGroup = $this->objectManager->get(\TYPO3\CMS\Extbase\Domain\Model\FrontendUserGroup::class);
        $userGroup->setTitle($feUser->getUsername());
        $userGroup->setPid(125); // Set the parent ID to the desired folder ID

        // Add the new user group to the repository and persist the changes
        $groupRepository->add($userGroup);
        $persistenceManager->persistAll();

        // Assign the user group to the user, update the FeUser, and persist the changes
        $feUser->addUsergroup($userGroup);
        $frontendUserRepository->update($feUser);
        $persistenceManager->persistAll();

        // Construct the folder name
        $folderName = $feUser->getUsername();

        // Create the folder path
        $folderPath = '../data/' . $folderName;

        // Attempt to create the folder
        if (mkdir($folderPath, 0755)) {
            // Folder created successfully
            // Set folder permissions
            chmod($folderPath, 0755); // Adjust the permissions as needed

            // Get the database connection
            $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
            $queryBuilder = $connectionPool->getQueryBuilderForTable('tx_falsecuredownload_folder');

            // Generate folder hash
            $folderHash = md5($folderPath);

            // Prepare the data to be inserted
            $data = [
                'pid' => 0,
                'tstamp' => time(),
                'crdate' => time(),
                'storage' => 8, // Adjust the storage value as per your requirements
                'folder' => '/'.$folderName.'/', // Adjust the folder name format as needed
                'folder_hash' => $folderHash,
                'fe_groups' => $userGroup->getUid(),
            ];

            // Insert the data into the table
            $queryBuilder->insert('tx_falsecuredownload_folder')->values($data)->execute();
        }
    }
}

weiss
  • 31
  • 6

1 Answers1

1

I solved the problem !

this is the code that worked for me to assign the usergroups to the folders automatically during registration:

// Get the database connection
            $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
            $queryBuilder = $connectionPool->getQueryBuilderForTable('tx_falsecuredownload_folder');

            // Generate folder hash
            $folderHash = sha1('/'.$feUser->getUsername());

            // Prepare the data to be inserted
            $data = [
                'pid' => 0,
                'tstamp' => time(),
                'crdate' => 0,
                'storage' => 8,
                'folder' => '/'.$feUser->getUsername().'/', 
                'folder_hash' => $folderHash,
                'fe_groups' => $userGroup->getUid(),
            ];

            // Insert the data into the table
            $queryBuilder->insert('tx_falsecuredownload_folder')->values($data)->execute();
weiss
  • 31
  • 6