Search code examples
typo3typo3-extensions

Typo3 FeUser addUserGroup not adding groups


I have a Typo3 v11 with feuser and feusermanager running on a Nginx with PHP 8.1. I have written my own finisher for the feusermanager. The finisher should look for an ID in a database table and assign the user to a certain group accordingly. The query on the table works. Resolving the groups also works (the groups are definitely objects). The user is an object. But nothing happens with the function $user->addUserGroup($GROUP). The IF query is passed through, I tested this with var_dumps. But the users are not assigned to the group.

Does anyone have any advice for me on what I am doing wrong? I'm honestly lost and very thankful for any advice.

Here is the code im running:

<?php

namespace MyVendor\MyExtension\Finisher;
use MyVendor\MyExtension\Domain\Repository\SpecialidteRepository;

use In2code\Femanager\Domain\Model\User;
use In2code\Femanager\Domain\Model\UserGroup;
use In2code\Femanager\Finisher\AbstractFinisher;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository;

class AssignToGroupsFinisher extends AbstractFinisher {
    private $specialidteRepository;

    /**
     * Inject the SpecialidTe repository
     *
     * @param \MyVendor\MyExtension\Domain\Repository\SpecialidteRepository $specialidteRepository
     */
    public function injectSpecialidteRepository(SpecialidteRepository $specialidteRepository)
    {
        $this->specialidteRepository = $specialidteRepository;
    }

    /**
     * Inject the FeUserGroup repository
     *
     * @param In2code\Femanager\Domain\Model\UserGroup $frontendUserGroupRepository
     */
    public function injectFrontendUserGroupRepository(FrontendUserGroupRepository $frontendUserGroupRepository)
    {
        $this->frontendUserGroupRepository = $frontendUserGroupRepository;
    }


    /**
     * @var User
     */
    protected $user;

    /**
     * @var array
     */
    protected $configuration;

    /**
     * @var array
     */
    protected $settings;

    /**
     * SpecialidFinisher
     *
     */
    public function SpecialidFinisher() 
    {
        /** @var User $user */
        $user = $this->getUser();

        /** @var UserGroup $noTe */
        $noTe = $this->frontendUserGroupRepository->findByUid(intval($this->settings['groupmapping']['noTE']));
        /** @var UserGroup $yesTe */
        $yesTe = $this->frontendUserGroupRepository->findByUid(intval($this->settings['groupmapping']['yesTE']));
        /** @var UserGroup $invalidSpecialid */
        $invalidSpecialid = $this->frontendUserGroupRepository->findByUid(intval($this->settings['groupmapping']['invalidSpecialid']));
        /** @var UserGroup $validSpecialid */
        $validSpecialid = $this->frontendUserGroupRepository->findByUid(intval($this->settings['groupmapping']['validSpecialid']));
        /** @var SpecialidRepository specialidCheck */
        $specialidCheck = $this->specialidteRepository->findBySpecialid($user->getSpecialid());

        if ( $specialidCheck ) {
            $user->addUsergroup($validSpecialid);
            //var_dump("1. if");
            if ( $specialidCheck['te'] == 1 ) {
                $user->addUsergroup($yesTe);
                //var_dump("2. if");
            }
            else {
                $user->addUsergroup($noTe);
                //var_dump("2. else");
            }
        } else {
            $user->addUsergroup($invalidSpecialid);
            $user->addUsergroup($noTe);
            //var_dump("1. else");
        }
       // return $user;
    }
}

Solution

  • You forgot to update the changed $user object in the repository. The repository needs to be informed about any new or changed object to persist.

    You need to:

    1. Get the FrontendUserRepository (isn't in your code)
    2. Inform the FrontendUserRepository that there was a change in the $user object

    Example:

    // Of course you need to use the correct FQCN
    $frontendUserRepository = GeneralUtility::makeInstance(FrontendUserRepository::class);
    $frontendUserRepository->update($user);
    
    // In some cases you need to persist actively with:
    // GeneralUtility::makeInstance(PersistenceManager::class)->persistAll();
    

    Documentation: https://docs.typo3.org/m/typo3/book-extbasefluid/10.4/en-us/6-Persistence/2a-creating-the-repositories.html