public function authenticate() { /** * @var UserSocialToken $tokenModel */ $tokenModel = UserSocialToken::model()->byAppId($this->appId)->byToken($this->token)->find(); if ($tokenModel) { if (!$tokenModel->isAlive()) { throw new AuthFailedApiException('DeadToken'); } $this->user_id = $tokenModel->user_id; $this->handleUser(); } else { $tokenModel = UserSocialToken::model()->create($this->token, $this->service, $this->appId); if ($tokenModel->error == '') { $this->user_id = $tokenModel->user_id; $tokens = UserSocialToken::model()->byUser($this->user_id)->byAppId($this->appId)->findAll(); if (count($tokens) > 1) { /** * @var \EmongoDocument|UserSocialToken $token */ foreach ($tokens as $token) { if ($token->access_token != $this->token) { $token->delete(); } } } $this->handleUser(); } else { throw new AuthFailedApiException($tokenModel->error); } } return $this->errorMessage == ''; }
/** * @param string $appId * @param string $service * @param string $token */ private function createUserSocialToken($appId, $service, $token) { if (!UserSocialToken::model()->byAppId($appId)->byToken($token)->find()) { $token = UserSocialToken::model()->create($token, $service, $appId); if ($token->error != '') { throw new ApiException($token->error, 500); } } }