Alternative to deprecated setParty/getParty? [SOLVED]

I am currently setting up a user management area for my application and would like to write my own User class.
This class should extend/relate to the Account to add some additional fields (e.g. last_login etc.) for my user.

Naturally I would have installed the Party package and would have created my own User class extending the AbstractParty.

But as the setParty/getParty methods in Account are marked as deprecated I wonder how my (rather common) use case is designated instead?

Is there a best practice that replaces the usage of the deprecated setParty/getParty methods?
I don’t want to rely on methods that may be abandoned soon but I don’t want to rewrite all of the methods that make use of the \TYPO3\Flow\Security\Account(Repository) classes directly (e.g. PersistedUsernamePasswordProvider) either.

I have found this old forum entry with further urls but all of those either don’t extend the Account or still make use of the getParty method…
https://forum.typo3.org/index.php/t/202180/

Setting the party on account is no longer required as the owning side of the relation was shifted to the abstract party (see https://github.com/neos/party/blob/master/Classes/TYPO3/Party/Domain/Model/AbstractParty.php). Use add/remove account on abstract party instead.

The Party package comes with a PartyService that can be used instead.

1 Like

Thank you for your replies and your hint to use the PartyService (funny class name btw :wink: ).

I have changed my code to set a “last_login” field now from this…:

protected function onAuthenticationSuccess(ActionRequest $originalRequest = null) {
    $myUser = $this->securityContext->getAccount()->getParty();

    if($myUser instanceof MyUser) {
        $myUser->setLastLogin(new \DateTime());
        $this->myUserRepository->update($myUser);
    }
    $this->redirect('...');
}

…to this:

protected function onAuthenticationSuccess(ActionRequest $originalRequest = null) {
   $myUser = $this->partyService->getAssignedPartyOfAccount($this->securityContext->getAccount());

   if($myUser instanceof MyUser) {
      $myUser->setLastLogin(new \DateTime());
      $this->myUserRepository->update($myUser);
   }
   $this->redirect('...');
}

To create a new user/account I do this (perhaps this helps someone):

/**
 * Creates an admin user
 *
 * @param $username
 * @param $password
 * @throws \TYPO3\Flow\Persistence\Exception\IllegalObjectTypeException
 */
public function createAdminUser($username, $password) {
   $roles = array('MyAdminRole');
   $authenticationProviderName = 'BackendProvider';
   $account = $this->accountFactory->createAccountWithPassword($username, $password, $roles, $authenticationProviderName);

   $this->accountRepository->add($account);

   $user = new User();    //class User extends AbstractParty
   $this->partyService->assignAccountToParty($account, $user);
   $this->userRepository->add($user);
}

To complete this topic here is a simple example of my UserRepository that queries both user and account tables:

/**
 * @Flow\Scope("singleton")
 */
class MyUserRepository extends Repository
{
   public function findAllAdminUsers() {
      $query = $this->createQuery();

      /** @var $query Query */
      return $query->getQueryBuilder()
         ->join('\TYPO3\Flow\Security\Account', 'a')
         ->where('a.authenticationProviderName = ?1')
         ->setParameter(1, 'BackendProvider')
         ->getQuery()
         ->execute();
   }
}
1 Like

Hey Katharina,

thanks a lot for your detailed response – I am sure it will help others!

All the best,
Sebastian