/** * 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; }
/** * Save a user view/edit form data. * * @param Storage\Entity\Account $account * @param Form $form * * @return FormEntityHandler */ public function saveProfileForm(Storage\Entity\Account $account, Form $form) { $guid = $account->getId(); if ($guid === null) { throw new \RuntimeException('GUID not set.'); } $account->setDisplayname($form->get('displayname')->getData()); $account->setEmail($form->get('email')->getData()); // Dispatch the account profile pre-save event $event = new MembersProfileEvent($account); $this->eventDispatcher->dispatch(MembersEvents::MEMBER_PROFILE_PRE_SAVE, $event); $this->records->saveAccount($account); $password = $form->get('password')->getData(); if ($password !== null) { $oauth = $this->getOauth($guid); if ($oauth === false) { $oauth = $this->createLocalOauthAccount($guid, $password); $this->createLocalProviderEntity($guid); } $oauth->setPassword($password); $this->records->saveOauth($oauth); } // Save any defined meta fields foreach ($event->getMetaEntityNames() as $metaField) { $metaEntity = $this->records->getAccountMeta($guid, $metaField); if ($metaEntity === false) { $metaEntity = new Storage\Entity\AccountMeta(); } $metaEntity->setGuid($guid); $metaEntity->setMeta($metaField); $metaEntity->setValue($form->get($metaField)->getData()); $this->records->saveAccountMeta($metaEntity); $event->addMetaEntity($metaEntity); } // Dispatch the account profile post-save event $this->eventDispatcher->dispatch(MembersEvents::MEMBER_PROFILE_POST_SAVE, $event); return $this; }
/** * Add a member. * * @param Application $app * @param Request $request * * @return Response */ public function userAdd(Application $app, Request $request) { $builder = $app['members.forms.manager']->getFormProfileEdit($request, true, null); $form = $builder->getForm(Form\MembersForms::FORM_PROFILE_EDIT); // Handle the form request data if ($form->isValid()) { /** @var Form\Entity\Profile $entity */ $entity = $builder->getEntity(Form\MembersForms::FORM_PROFILE_EDIT); // Create and store the account entity $account = new Storage\Entity\Account(); $account->setGuid($entity->getGuid()); $account->setDisplayname($entity->getDisplayname()); $account->setEmail($entity->getEmail()); $account->setRoles([]); $account->setEnabled(true); $app['members.records']->saveAccount($account); // Save the password to a meta record $oauth = new Storage\Entity\Oauth(); $oauth->setGuid($account->getGuid()); $oauth->setResourceOwnerId($account->getGuid()); $oauth->setEnabled(true); $app['members.records']->saveOauth($oauth); // Create a local provider entry $provider = new Storage\Entity\Provider(); $provider->setGuid($account->getGuid()); $provider->setProvider('local'); $provider->setResourceOwnerId($account->getGuid()); $provider->setLastupdate(Carbon::now()); $app['members.records']->saveProvider($provider); return new RedirectResponse($app['url_generator']->generate('membersAdmin')); } $html = $app['members.forms.manager']->renderForms($builder, $app['twig'], '@MembersAdmin/profile_add.twig'); return new Response(new \Twig_Markup($html, 'UTF-8')); }
/** * @param Storage\Entity\Account $account * @param PasswordReset $passwordReset * @param \Twig_Environment $twig * @param string $siteUrl * * @return string */ private function getResetHtml(Storage\Entity\Account $account, PasswordReset $passwordReset, \Twig_Environment $twig, $siteUrl) { $config = $this->getMembersConfig(); $query = http_build_query(['code' => $passwordReset->getQueryCode()]); $context = ['name' => $account->getDisplayname(), 'email' => $account->getEmail(), 'link' => sprintf('%s%s/reset?%s', $siteUrl, $config->getUrlAuthenticate(), $query), 'member' => $account]; $mailHtml = $twig->render($config->getTemplate('recovery', 'body'), $context); return $mailHtml; }
/** * {@inheritdoc} */ public function jsonSerialize() { return ['guid' => $this->guid, 'cookie' => $this->cookie, 'expiry' => $this->expiry, 'accessTokens' => $this->accessTokens, 'account' => $this->account->toArray()]; }