From f254bcc4076e14d327f0941aa8e57530fa72bc81 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Thu, 9 Jul 2020 14:03:00 -0500 Subject: [PATCH] Whispeak: Remove unnecessary classes - refs BT#17415 --- .../AuthenticationRequestController.php | 287 ------------------ .../Controller/BaseRequestController.php | 142 --------- 2 files changed, 429 deletions(-) delete mode 100644 plugin/whispeakauth/Controller/AuthenticationRequestController.php delete mode 100644 plugin/whispeakauth/Controller/BaseRequestController.php diff --git a/plugin/whispeakauth/Controller/AuthenticationRequestController.php b/plugin/whispeakauth/Controller/AuthenticationRequestController.php deleted file mode 100644 index efdad5f1fd..0000000000 --- a/plugin/whispeakauth/Controller/AuthenticationRequestController.php +++ /dev/null @@ -1,287 +0,0 @@ -user2fa)) { - $this->user = api_get_user_entity($this->user2fa); - } elseif (isset($_POST['username'])) { - $this->user = \UserManager::getRepository()->findOneBy(['username' => $_POST['username']]); - } else { - $this->user = api_get_user_entity(api_get_user_id()); - } - } - - /** - * @return bool - */ - protected function userIsAllowed() - { - $userId = api_get_user_id(); - $this->user2fa = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_2FA_USER, 0); - - if (!empty($this->user2fa) || !empty($userId)) { - return !empty($_FILES['audio']); - } - - return !empty($_POST['username']) && !empty($_FILES['audio']); - } - - /** - * @throws \Exception - * - * @return string - */ - protected function doApiRequest() - { - $failedLogins = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS, 0); - $maxAttempts = $this->plugin->getMaxAttempts(); - - if ($maxAttempts && $failedLogins >= $maxAttempts) { - return \Display::return_message($this->plugin->get_lang('MaxAttemptsReached'), 'warning'); - } - - $wsId = \WhispeakAuthPlugin::getAuthUidValue($this->user->getId()); - - if (empty($wsId)) { - return \Display::return_message($this->plugin->get_lang('SpeechAuthNotEnrolled'), 'warning'); - } - - $token = $this->createSessionToken(); - $success = $this->performAuthentication($token, $wsId->getValue()); - - /** @var array $lpItemInfo */ - $lpItemInfo = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_LP_ITEM, []); - /** @var array $quizQuestionInfo */ - $quizQuestionInfo = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []); - - $return = ''; - - $message = $this->plugin->get_lang('AuthentifySuccess'); - - if (!$success) { - if (!empty($lpItemInfo)) { - $this->plugin->addAttemptInLearningPath( - LogEvent::STATUS_FAILED, - $this->user->getId(), - $lpItemInfo['lp_item'], - $lpItemInfo['lp'] - ); - } - - if (!empty($quizQuestionInfo)) { - $this->plugin->addAttemptInQuiz( - LogEvent::STATUS_FAILED, - $this->user->getId(), - $quizQuestionInfo['question'], - $quizQuestionInfo['quiz'] - ); - } - - $message = $this->plugin->get_lang('AuthentifyFailed'); - - \ChamiloSession::write(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins); - - if ($maxAttempts && $failedLogins >= $maxAttempts) { - $message .= PHP_EOL - .''.$this->plugin->get_lang('MaxAttemptsReached').'' - .PHP_EOL - .'
' - .$this->plugin->get_lang('LoginWithUsernameAndPassword') - .''; - - if (!empty($user2fa)) { - \Display::addFlash(\Display::return_message($message, 'warning', false)); - } - } else { - $message .= PHP_EOL.$this->plugin->get_lang('TryAgain'); - - if ('true' === api_get_setting('allow_lostpassword')) { - $message .= '
' - .\Display::url( - get_lang('LostPassword'), - api_get_path(WEB_CODE_PATH).'auth/lostPassword.php', - ['target' => $lpItemInfo ? '_top' : '_self'] - ); - } - } - } - - $return .= \Display::return_message( - $message, - $success ? 'success' : 'warning', - false - ); - - if (!$success && $maxAttempts && $failedLogins >= $maxAttempts) { - \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS); - - if (!empty($lpItemInfo)) { - $return .= ''; - - return $return; - } - - if (!empty($quizQuestionInfo)) { - $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; - - \ChamiloSession::write(\WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, true); - - $return .= ""; - - exit; - } - - $return .= ''; - - return $return; - } - - if ($success) { - \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); - \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS); - - if (!empty($lpItemInfo)) { - \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_LP_ITEM); - \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_2FA_USER); - - $this->plugin->addAttemptInLearningPath( - LogEvent::STATUS_SUCCESS, - $this->user->getId(), - $lpItemInfo['lp_item'], - $lpItemInfo['lp'] - ); - - $return .= ''; - - return $return; - } - - if (!empty($quizQuestionInfo)) { - $quizQuestionInfo['passed'] = true; - $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; - - \ChamiloSession::write(\WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, $quizQuestionInfo); - - $this->plugin->addAttemptInQuiz( - LogEvent::STATUS_SUCCESS, - $this->user->getId(), - $quizQuestionInfo['question'], - $quizQuestionInfo['quiz'] - ); - - $return .= ''; - - return $return; - } - - $loggedUser = [ - 'user_id' => $this->user->getId(), - 'status' => $this->user->getStatus(), - 'uidReset' => true, - ]; - - if (empty($user2fa)) { - \ChamiloSession::write(\WhispeakAuthPlugin::SESSION_2FA_USER, $this->user->getId()); - } - - \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_FAILED_LOGINS); - \ChamiloSession::write('_user', $loggedUser); - \Login::init_user($this->user->getId(), true); - - $return .= ''; - } - - return $return; - } - - /** - * @throws \Exception - * - * @return string - */ - private function createSessionToken() - { - try { - $response = $this->httpClient->post( - 'auth', - [ - 'headers' => [ - 'Authorization' => "Bearer {$this->apiKey}", - ], - 'json' => [], - 'query' => [ - 'lang' => api_get_language_isocode($this->user->getLanguage()), - ], - ] - ); - $json = json_decode((string) $response->getBody(), true); - - return $json['token']; - } catch (RequestException $requestException) { - $this->throwRequestException( - $requestException, - $this->plugin->get_lang('AuthentifyFailed') - ); - } - } - - /** - * @param string $token - * @param string $wsId - * - * @throws \Exception - * - * @return bool - */ - private function performAuthentication($token, $wsId) - { - try { - $this->httpClient->post( - 'auth', - [ - 'headers' => [ - 'Authorization' => "Bearer $token", - ], - 'multipart' => [ - [ - 'name' => 'speaker', - 'contents' => $wsId, - ], - [ - 'name' => 'file', - 'contents' => fopen($this->audioFilePath, 'r'), - 'filename' => basename($this->audioFilePath), - ], - ], - ] - ); - - return true; - } catch (RequestException $requestException) { - $this->throwRequestException( - $requestException, - $this->plugin->get_lang('AuthentifyFailed') - ); - } - } -} diff --git a/plugin/whispeakauth/Controller/BaseRequestController.php b/plugin/whispeakauth/Controller/BaseRequestController.php deleted file mode 100644 index 834b614e35..0000000000 --- a/plugin/whispeakauth/Controller/BaseRequestController.php +++ /dev/null @@ -1,142 +0,0 @@ -plugin = \WhispeakAuthPlugin::create(); - $this->httpClient = new Client(['base_uri' => $this->plugin->getApiUrl(),]); - $this->apiKey = $this->plugin->get(\WhispeakAuthPlugin::SETTING_TOKEN); - } - - abstract protected function setUser(); - - /** - * @return bool - */ - abstract protected function userIsAllowed(); - - /** - * @throws \Exception - */ - protected function protect() - { - if (false === $this->userIsAllowed()) { - throw new \Exception(get_lang('NotAllowed')); - } - - $this->plugin->protectTool(false); - } - - /** - * @throws \Exception - */ - private function uploadAudioFile() - { - $pluginName = $this->plugin->get_name(); - - $path = api_upload_file($pluginName, $_FILES['audio'], $this->user->getId()); - - if (false === $path) { - throw new \Exception(get_lang('UploadError')); - } - - $fullPath = api_get_path(SYS_UPLOAD_PATH).$pluginName.$path['path_to_save']; - $mimeType = mime_content_type($fullPath); - - if ('wav' !== substr($mimeType, -3)) { - $ffmeg = FFMpeg::create(); - - $audioFile = $ffmeg->open($fullPath); - - $fullPath = dirname($fullPath).'/audio.wav'; - - $audioFile->save(new Wav(), $fullPath); - } - - $this->audioFilePath = $fullPath; - } - - public function process() - { - try { - $this->protect(); - $this->setUser(); - - if (empty($this->user)) { - throw new \Exception(get_lang('NoUser')); - } - - $this->uploadAudioFile(); - - $response = $this->doApiRequest(); - - echo $response; - } catch (\Exception $exception) { - echo \Display::return_message($exception->getMessage(), 'error'); - } - } - - /** - * @throws \Exception - * - * @return mixed - */ - abstract protected function doApiRequest(); - - /** - * @param \GuzzleHttp\Exception\RequestException $requestException - * @param string $defaultMessage - * - * @throws \Exception - */ - protected function throwRequestException(RequestException $requestException, $defaultMessage) - { - $message = $defaultMessage; - - if ($requestException->hasResponse()) { - $json = json_decode((string) $requestException->getResponse()->getBody(), true); - $message = is_array($json['message']) ? implode("\n", $json['message']) : $json['message']; - } - - throw new \Exception($message); - } -}