/** * Validate a validation code's validity, validly. * * @param Records $records * @param string $code */ public function validateCode(Records $records, $code) { $this->code = $code; if (strlen($code) !== 40) { $this->message = 'Invalid code'; return; } // Get the verification key meta entity $metaEntities = $records->getAccountMetaValues(self::KEY_NAME, $code); if ($metaEntities === false) { $this->throwException(new AccountVerificationException('Stored meta code not found', AccountVerificationException::MISSING_META)); } /** @var Storage\Entity\AccountMeta $metaEntity */ $metaEntity = reset($metaEntities); if ($metaEntity === false) { $this->throwException(new AccountVerificationException('Stored meta code previously removed.', AccountVerificationException::REMOVED_META)); } $guid = $metaEntity->getGuid(); // Get the account and set it as verified $this->account = $records->getAccountByGuid($guid); if ($this->account === false) { $this->throwException(new AccountVerificationException('Missing account record.', AccountVerificationException::MISSING_ACCOUNT)); } $this->account->setVerified(true); $records->saveAccount($this->account); // Remove meta record $records->deleteAccountMeta($metaEntity); $this->success = true; $this->message = 'Account validated!'; }
/** * Create and store the account record. * * @param string $displayName * @param string $emailAddress * @param array $roles * * @return Entity\Account|false */ public function createAccount($displayName, $emailAddress, array $roles) { $account = new Entity\Account(); $account->setDisplayname($displayName); $account->setEmail($emailAddress); $account->setRoles($roles); $account->setEnabled(true); $account->setVerified(false); return $this->getAccountRepository()->save($account) ? $account : false; }