diff --git a/plugin/whispeakauth/Controller/AuthenticationController.php b/plugin/whispeakauth/Controller/AuthenticationController.php index 1fa1841b75..831736ab88 100644 --- a/plugin/whispeakauth/Controller/AuthenticationController.php +++ b/plugin/whispeakauth/Controller/AuthenticationController.php @@ -120,6 +120,8 @@ class AuthenticationController extends BaseController $userId = api_get_user_id(); $user2fa = ChamiloSession::read(WhispeakAuthPlugin::SESSION_2FA_USER, 0); + $result = []; + if (!empty($user2fa) || !empty($userId)) { $isAllowed = !empty($_FILES['audio']); } else { @@ -191,6 +193,18 @@ class AuthenticationController extends BaseController $message = $this->plugin->get_lang('AuthentifyFailed'); + $authTokenRequest = new ApiRequest(); + $authTokenResponse = $authTokenRequest->createAuthenticationSessionToken($user); + + if (empty($authTokenResponse['text'])) { + $varNumber = mt_rand(1, 6); + $authTokenResponse['text'] = $this->plugin->get_lang("AuthentifySampleText$varNumber"); + } + + $result['text'] = $authTokenResponse['text']; + + ChamiloSession::write(WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $authTokenResponse['token']); + ChamiloSession::write(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins); if ($maxAttempts && $failedLogins >= $maxAttempts) { @@ -220,7 +234,7 @@ class AuthenticationController extends BaseController } } - echo Display::return_message( + $result['resultHtml'] = Display::return_message( $message, $success ? 'success' : 'warning', false @@ -230,11 +244,11 @@ class AuthenticationController extends BaseController ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); if (!empty($lpItemInfo)) { - echo ''; - exit; + return $result; } if (!empty($quizQuestionInfo)) { @@ -242,14 +256,14 @@ class AuthenticationController extends BaseController ChamiloSession::write(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, true); - echo ""; + $result['resultHtml'] .= ""; - exit; + return $result; } - echo ''; + $result['resultHtml'] .= ''; - exit; + return $result; } if ($success) { @@ -267,9 +281,9 @@ class AuthenticationController extends BaseController $lpItemInfo['lp'] ); - echo ''; + $result['resultHtml'] .= ''; - exit; + return $result; } if (!empty($quizQuestionInfo)) { @@ -285,9 +299,9 @@ class AuthenticationController extends BaseController $quizQuestionInfo['quiz'] ); - echo ''; + $result['resultHtml'] .= ''; - exit; + return $result; } if (empty($lpItemInfo) && empty($quizQuestionInfo)) { @@ -308,8 +322,12 @@ class AuthenticationController extends BaseController ChamiloSession::write('_user', $loggedUser); Login::init_user($user->getId(), true); - echo ''; + $result['resultHtml'] .= ''; + + return $result; } + + return $result; } /** diff --git a/plugin/whispeakauth/Controller/EnrollmentController.php b/plugin/whispeakauth/Controller/EnrollmentController.php index f08bd27af0..6a7bbb4c02 100644 --- a/plugin/whispeakauth/Controller/EnrollmentController.php +++ b/plugin/whispeakauth/Controller/EnrollmentController.php @@ -48,6 +48,8 @@ class EnrollmentController extends BaseController */ public function ajax() { + $result = ['resultHtml' => '']; + if (!$this->plugin->toolIsEnabled() || empty($_FILES['audio'])) { throw new \Exception(get_lang('NotAllowed')); } @@ -63,13 +65,28 @@ class EnrollmentController extends BaseController } $request = new ApiRequest(); - $response = $request->createEnrollment($token, $audioFilePath, $user); + + try { + $response = $request->createEnrollment($token, $audioFilePath, $user); + } catch (\Exception $exception) { + $enrollTokenRequest = new ApiRequest(); + $enrollTokenResponse = $enrollTokenRequest->createEnrollmentSessionToken($user); + + \ChamiloSession::write(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT, $enrollTokenResponse['token']); + + return [ + 'resultHtml' => \Display::return_message($exception->getMessage(), 'error'), + 'text' => $enrollTokenResponse['text'], + ]; + } \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); $this->plugin->saveEnrollment($user, $response['speaker']); - echo \Display::return_message($this->plugin->get_lang('EnrollmentSuccess'), 'success'); + $result['resultHtml'] .= \Display::return_message($this->plugin->get_lang('EnrollmentSuccess'), 'success'); + + return $result; } /** diff --git a/plugin/whispeakauth/ajax/record_audio.php b/plugin/whispeakauth/ajax/record_audio.php index f0d8bf9458..946f29821a 100644 --- a/plugin/whispeakauth/ajax/record_audio.php +++ b/plugin/whispeakauth/ajax/record_audio.php @@ -17,11 +17,15 @@ if ($isEnrollment) { $controller = new EnrollmentController(); + header('Content-Type: application/json'); + try { - $controller->ajax(); + echo json_encode($controller->ajax()); } catch (Exception $exception) { - WhispeakAuthPlugin::displayNotAllowedMessage( - $exception->getMessage() + echo json_encode( + [ + 'resultHtml' => Display::return_message($exception->getMessage(), 'error', false), + ] ); } die; @@ -30,9 +34,15 @@ if ($isEnrollment) { if ($isAuthentify) { $controller = new AuthenticationController(); + header('Content-Type: application/json'); + try { - $controller->ajax(); + echo json_encode($controller->ajax()); } catch (Exception $exception) { - echo Display::return_message($exception->getMessage(), 'error', false); + echo json_encode( + [ + 'resultHtml' => Display::return_message($exception->getMessage(), 'error', false), + ] + ); } } diff --git a/plugin/whispeakauth/assets/js/RecordAudio.js b/plugin/whispeakauth/assets/js/RecordAudio.js index 796ccf6353..fa6032ff40 100644 --- a/plugin/whispeakauth/assets/js/RecordAudio.js +++ b/plugin/whispeakauth/assets/js/RecordAudio.js @@ -43,7 +43,11 @@ window.RecordAudio = (function () { btnStop.prop('disabled', true).text(btnStop.data('loadingtext')); } }).done(function (response) { - $('#messages-deck').html(response); + if (response.text) { + $('#txt-sample-text').text(response.text); + } + + $('#messages-deck').html(response.resultHtml); if ($('#messages-deck > .alert.alert-success').length > 0) { tagAudio.parents('#audio-wrapper').addClass('hidden').removeClass('show'); diff --git a/plugin/whispeakauth/view/record_audio.html.twig b/plugin/whispeakauth/view/record_audio.html.twig index 782d3c105a..aa8e30710e 100644 --- a/plugin/whispeakauth/view/record_audio.html.twig +++ b/plugin/whispeakauth/view/record_audio.html.twig @@ -8,7 +8,7 @@
{{ sample_text }}
+{{ sample_text }}