/** * 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!'; }
/** * Handle a successful account authentication. * * @param AccessToken $accessToken * * @throws Ex\MissingAccountException * @throws Ex\InvalidAuthorisationRequestException */ protected function handleAccountTransition(AccessToken $accessToken) { $providerName = $this->providerManager->getProviderName(); $resourceOwner = $this->getResourceOwner($accessToken); $email = $resourceOwner->getEmail(); if ((bool) $email === false) { // Redirect to registration $this->setDebugMessage(sprintf('No email address found for transitional %s provider ID %s', $providerName, $resourceOwner->getId())); throw new Ex\MissingAccountException(sprintf('Provider %s data for ID %s does not include an email address.', $providerName, $resourceOwner->getId())); } $guid = $this->session->getAuthorisation()->getGuid(); $accountEntity = $this->records->getAccountByGuid($guid); if ($accountEntity === false) { $accountEntity = $this->records->getAccountByEmail($email); } if ($accountEntity === false) { $this->setDebugMessage(sprintf('No account found for transitional %s provider ID %s', $providerName, $resourceOwner->getId())); throw new Ex\MissingAccountException(sprintf('No account for %s provider ID %s during transition', $providerName, $resourceOwner->getId())); } $providerEntity = $this->session->getTransitionalProvider()->getProviderEntity(); $providerEntity->setGuid($accountEntity->getGuid()); $providerEntity->setLastupdate(Carbon::now()); $this->records->saveProvider($providerEntity); $this->session->removeTransitionalProvider(); $this->setSession($accessToken); }
/** * Delete a member account. * * @param string $guid * @param string $role * * @return bool */ public function deleteAccountRole($guid, $role) { $account = $this->records->getAccountByGuid($guid); $roles = array_filter((array) $account->getRoles(), function ($r) use($role) { return $r !== $role ?: false; }); $account->setRoles($roles); return $this->records->saveAccount($account); }
/** * Check if the current logged-in session has a member role. * * @param string|array $role * * @return bool */ public function hasRole($role) { $auth = $this->getAuthorisation(); if ($auth === null) { return false; } $account = $this->records->getAccountByGuid($auth->getGuid()); if ($account === false) { return false; } $roles = (array) $account->getRoles(); if (is_string($role)) { return in_array($role, $roles); } return array_intersect($role, $roles) !== false; }
/** * Return a member's account. * * @param string|null $guid * * @return Storage\Entity\Member|null */ public function getMember($guid = null) { if ($guid === null) { if (!$this->session->hasAuthorisation()) { return null; } $auth = $this->session->getAuthorisation(); $guid = $auth->getGuid(); } $account = $this->records->getAccountByGuid($guid); if ($account) { $meta = $this->records->getAccountMetaAll($guid); $member = new Storage\Entity\Member($account, $meta); return $member; } return null; }
/** * @param string $guid Member GUID. * * @return Profile */ private function getEntityProfile($guid = null) { if ($guid !== null && !Uuid::isValid($guid)) { throw new \RuntimeException(sprintf('Invalid GUID value "%s" given.', $guid)); } $account = $this->records->getAccountByGuid($guid); $profile = $account ? new Profile($account->toArray()) : new Profile([]); $accountMeta = $this->records->getAccountMetaAll($guid); if ($accountMeta === false) { return $profile; } /** @var Storage\Entity\AccountMeta $metaEntity */ foreach ((array) $accountMeta as $metaEntity) { if ($profile->has($metaEntity->getMeta())) { // Meta shouldn't override continue; } $profile[$metaEntity->getMeta()] = $metaEntity->getValue(); } return $profile; }