/** * 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 a 'remote' provider record from a session stored 'transitional' one. * * @param string $guid * * @return Storage\Entity\Provider */ protected function convertTransitionalProviderToEntity($guid) { $provider = $this->session->getTransitionalProvider()->getProviderEntity(); $provider->setGuid($guid); $provider->setLastupdate(Carbon::now()); $this->records->saveProvider($provider); $this->session->removeTransitionalProvider(); return $provider; }
/** * 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); }
/** * Return an array of registered OAuth providers for an account. * * @param string $guid * * @return array */ public function getProviders($guid = null) { $providers = []; if ($guid === null) { $auth = $this->session->getAuthorisation(); if ($auth === null) { return $providers; } $guid = $auth->getGuid(); } $providerEntities = $this->records->getProvisionsByGuid($guid); if ($providerEntities === false) { return $providers; } /** @var Storage\Entity\Provider $providerEntity */ foreach ($providerEntities as $providerEntity) { $providers[] = $providerEntity->getProvider(); } return $providers; }
/** * @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; }
/** * . * * @param string $providerName * @param AccessToken $accessToken * @param ResourceOwnerInterface $resourceOwner */ protected function setSessionNewProvider($providerName, AccessToken $accessToken, ResourceOwnerInterface $resourceOwner) { if ($this->session->hasAuthorisation()) { // Member is already in possession of another login, and the provider does NOT exist $this->createProviderTransition($accessToken, $resourceOwner); return; } $account = $this->records->getAccountByEmail($resourceOwner->getEmail()); if ($account === false) { $account = $this->records->createAccount($resourceOwner->getName(), $resourceOwner->getEmail(), $this->config->getRolesRegister()); } $providerEntity = new Entity\Provider(); $providerEntity->setGuid($account->getGuid()); $providerEntity->setProvider($providerName); $providerEntity->setResourceOwner($resourceOwner); $providerEntity->setResourceOwnerId($resourceOwner->getId()); $providerEntity->setLastupdate(Carbon::now()); $this->records->saveProvider($providerEntity); $this->session->addAccessToken($providerName, $accessToken)->createAuthorisation($providerEntity->getGuid()); $this->providerEntity = $providerEntity; }
/** * Persist session data to storage. */ public function persistData() { if ($this->authorisation === null) { return; } /** @var AccessToken $accessToken */ foreach ($this->authorisation->getAccessTokens() as $provider => $accessToken) { $tokenEntities = $this->records->getTokensByGuid($this->authorisation->getGuid()); if ($tokenEntities === false) { $tokenEntities[] = new Storage\Entity\Token(); } /** @var Storage\Entity\Token $tokenEntity */ foreach ($tokenEntities as $tokenEntity) { $tokenEntity->setGuid($this->authorisation->getGuid()); $tokenEntity->setToken((string) $accessToken); $tokenEntity->setTokenType('access_token'); $tokenEntity->setTokenData($accessToken); $tokenEntity->setExpires($accessToken->getExpires()); $tokenEntity->setCookie($this->authorisation->getCookie()); $this->records->saveToken($tokenEntity); } } $this->session->set(self::SESSION_AUTHORISATION, json_encode($this->authorisation)); }