/**
  * In this method, actually create the user / account.
  *
  * NOTE: After this method is called, the $registrationFlow is DESTROYED, so you need to store all attributes
  * in your object as you need them.
  *
  * @param RegistrationFlow $registrationFlow
  * @return void
  */
 public function createUserAndAccount(RegistrationFlow $registrationFlow)
 {
     // Create the account
     $account = new Account();
     $account->setAccountIdentifier($registrationFlow->getEmail());
     $account->setCredentialsSource($registrationFlow->getEncryptedPassword());
     $account->setAuthenticationProviderName('Sandstorm.UserManagement:Login');
     // Assign pre-configured roles
     foreach ($this->rolesForNewUsers as $roleString) {
         $account->addRole(new Role($roleString));
     }
     // Create the user
     $user = new User();
     $user->setAccount($account);
     $user->setEmail($registrationFlow->getEmail());
     if (array_key_exists('salutation', $registrationFlow->getAttributes())) {
         $user->setGender($registrationFlow->getAttributes()['salutation']);
     }
     if (array_key_exists('firstName', $registrationFlow->getAttributes())) {
         $user->setFirstName($registrationFlow->getAttributes()['firstName']);
     }
     if (array_key_exists('lastName', $registrationFlow->getAttributes())) {
         $user->setLastName($registrationFlow->getAttributes()['lastName']);
     }
     // Persist user
     $this->userRepository->add($user);
     $this->persistenceManager->whitelistObject($user);
     $this->persistenceManager->whitelistObject($account);
 }
 /**
  * @param RegistrationFlow $value The value that should be validated
  * @return void
  * @throws InvalidValidationOptionsException
  */
 protected function isValid($value)
 {
     /** @noinspection PhpUndefinedMethodInspection */
     $existingAccount = $this->accountRepository->findOneByAccountIdentifier($value->getEmail());
     if ($existingAccount) {
         // todo: error message translatable
         $this->result->forProperty('email')->addError(new Error('Die Email-Adresse %s wird bereits verwendet!', 1336499566, [$value->getEmail()]));
     }
     // If a custom validation service is registered, call its validate method to allow custom validations during registration
     if ($this->objectManager->isRegistered(RegistrationFlowValidationServiceInterface::class)) {
         $instance = $this->objectManager->get(RegistrationFlowValidationServiceInterface::class);
         $instance->validateRegistrationFlow($value, $this);
     }
 }
 /**
  * @param RegistrationFlow $registrationFlow
  */
 public function registerAction(RegistrationFlow $registrationFlow)
 {
     // We remove already existing flows
     $alreadyExistingFlows = $this->registrationFlowRepository->findByEmail($registrationFlow->getEmail());
     if (count($alreadyExistingFlows) > 0) {
         foreach ($alreadyExistingFlows as $alreadyExistingFlow) {
             $this->registrationFlowRepository->remove($alreadyExistingFlow);
         }
     }
     $registrationFlow->storeEncryptedPassword();
     // Send out a confirmation mail
     $activationLink = $this->uriBuilder->reset()->setCreateAbsoluteUri(true)->uriFor('activateAccount', ['token' => $registrationFlow->getActivationToken()], 'Registration');
     $this->emailService->sendTemplateBasedEmail('ActivationToken', $this->subjectActivation, [$this->emailSenderAddress => $this->emailSenderName], [$registrationFlow->getEmail()], ['activationLink' => $activationLink, 'applicationName' => $this->emailSenderName, 'registrationFlow' => $registrationFlow, 'baseUri' => htmlspecialchars($this->controllerContext->getRequest()->getHttpRequest()->getBaseUri())]);
     $this->registrationFlowRepository->add($registrationFlow);
     $this->view->assign('email', $registrationFlow->getEmail());
 }
 /**
  * In this method, actually create the user / account.
  *
  * NOTE: After this method is called, the $registrationFlow is DESTROYED, so you need to store all attributes
  * in your object as you need them.
  *
  * @param RegistrationFlow $registrationFlow
  * @return void
  */
 public function createUserAndAccount(RegistrationFlow $registrationFlow)
 {
     // Create the account
     $account = new Account();
     $account->setAccountIdentifier($registrationFlow->getEmail());
     $account->setCredentialsSource($registrationFlow->getEncryptedPassword());
     $account->setAuthenticationProviderName('Sandstorm.UserManagement:Login');
     // Assign preconfigured roles
     foreach ($this->rolesForNewUsers as $roleString) {
         $account->addRole(new Role($roleString));
     }
     // Create the user
     $user = new User();
     $name = new PersonName('', $registrationFlow->getAttributes()['firstName'], '', $registrationFlow->getAttributes()['lastName'], '', $registrationFlow->getEmail());
     $user->setName($name);
     // Assign them to each other and persist
     $this->getPartyService()->assignAccountToParty($account, $user);
     $this->getPartyRepository()->add($user);
     $this->accountRepository->add($account);
     $this->persistenceManager->whitelistObject($user);
     $this->persistenceManager->whitelistObject($user->getPreferences());
     $this->persistenceManager->whitelistObject($name);
     $this->persistenceManager->whitelistObject($account);
 }
 /**
  * Create User on the Command Line
  *
  * @param string $username The email address, which also serves as the username.
  * @param string $password This user's password.
  * @param string $additionalAttributes Additional attributes to pass to the registrationFlow as semicolon-separated list. Example: ./flow sandstormuser:create ... --additionalAttributes="customerType:CUSTOMER;color:blue"
  */
 public function createCommand($username, $password, $additionalAttributes = '')
 {
     // Parse additionalAttributes if they exist
     $attributes = [];
     if (strlen($additionalAttributes) > 0) {
         $attributesSplitBySeparator = explode(';', $additionalAttributes);
         array_map(function ($singleAttribute) use(&$attributes) {
             $splitAttribute = explode(':', $singleAttribute);
             $attributes[$splitAttribute[0]] = $splitAttribute[1];
         }, $attributesSplitBySeparator);
     }
     $passwordDto = new PasswordDto();
     $passwordDto->setPassword($password);
     $passwordDto->setPasswordConfirmation($password);
     $registrationFlow = new RegistrationFlow();
     $registrationFlow->setPasswordDto($passwordDto);
     $registrationFlow->setEmail($username);
     $registrationFlow->setAttributes($attributes);
     // Remove existing registration flows
     $alreadyExistingFlows = $this->registrationFlowRepository->findByEmail($registrationFlow->getEmail());
     if (count($alreadyExistingFlows) > 0) {
         foreach ($alreadyExistingFlows as $alreadyExistingFlow) {
             $this->registrationFlowRepository->remove($alreadyExistingFlow);
         }
     }
     $registrationFlow->storeEncryptedPassword();
     // Store the RF and persist so the activate command will find it
     $this->registrationFlowRepository->add($registrationFlow);
     $this->persistenceManager->persistAll();
     // Directly activate the account
     $this->activateRegistrationCommand($username);
     $this->outputLine('Added the User <b>"%s"</b> with password <b>"%s"</b>.', [$username, $password]);
 }