Skip to content

Instantly share code, notes, and snippets.

@sorenmalling
Created April 23, 2015 15:26
Show Gist options
  • Select an option

  • Save sorenmalling/9874b39d21f05d003601 to your computer and use it in GitHub Desktop.

Select an option

Save sorenmalling/9874b39d21f05d003601 to your computer and use it in GitHub Desktop.
<?php
namespace Vendor\Project\Domain\Factory;
use Vendor\Project\Exception\AccountIdentifierAlreadyExistsException;
use TYPO3\Flow\Annotations as Flow;
use Vendor\Project\Domain\Model\User;
use TYPO3\Party\Domain\Model\ElectronicAddress;
class UserFactory {
/**
* Account factory
*
* @var \TYPO3\Flow\Security\AccountFactory
* @Flow\Inject
*/
protected $accountFactory;
/**
* Account repository
*
* @var \TYPO3\Flow\Security\AccountRepository
* @Flow\Inject
*/
protected $accountRepository;
/**
* Party repository
*
* @var \TYPO3\Party\Domain\Repository\PartyRepository
* @Flow\Inject
*/
protected $partyRepository;
/**
* Create a new account
*
* @param string $identifier The identifier (usually e-mail) of the user to be created.
* @param string $password Password of the user to be created
* @param string $firstName First name of the user to be created
* @param string $lastName Last name of the user to be created
* @param string $email Account email
* @param array $roleIdentifiers A list of role identifiers to assign
*
* @throws \Vendor\Project\Exception\AccountIdentifierAlreadyExistsException If a account with same identifier already exists
*
* @return \Vendor\Project\Domain\Model\User The created user instance
*/
public function create($identifier, $password, $firstName, $lastName, $email, array $roleIdentifiers = NULL) {
$existingAccount = $this->accountRepository->findActiveByAccountIdentifierAndAuthenticationProviderName($identifier, 'DefaultProvider');
if ($existingAccount !== NULL) {
throw new AccountIdentifierAlreadyExistsException(sprintf('The account identifier "%s" already exists', $identifier));
}
$user = new User();
//$name = new PersonName('', $firstName, '', $lastName, '', $identifier);
$user->setName($firstName . ' ' . $lastName);
$user->setEmail($email);
if ($roleIdentifiers === NULL) {
$roleIdentifiers = array('Vendoer.Project:User');
}
$account = $this->accountFactory->createAccountWithPassword($identifier, $password, $roleIdentifiers);
$user->addAccount($account);
$this->partyRepository->add($user);
$accounts = $user->getAccounts();
foreach ($accounts as $account) {
$this->accountRepository->add($account);
}
return $user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment