Whispeak: Add details in message error for auth/enroll - refs BT#17415

pull/3669/head
Angel Fernando Quiroz Campos 5 years ago
parent 32cfb4b04f
commit 5dbbc737e6
  1. 27
      plugin/whispeakauth/Controller/AuthenticationController.php
  2. 20
      plugin/whispeakauth/Request/ApiRequest.php

@ -156,9 +156,6 @@ class AuthenticationController extends BaseController
$token = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); $token = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT);
$request = new ApiRequest();
$success = $request->performAuthentication($token, $user, $audioFilePath);
\ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT);
/** @var array $lpItemInfo */ /** @var array $lpItemInfo */
@ -166,7 +163,21 @@ class AuthenticationController extends BaseController
/** @var array $quizQuestionInfo */ /** @var array $quizQuestionInfo */
$quizQuestionInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []); $quizQuestionInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []);
$success = true;
$request = new ApiRequest();
try {
$request->performAuthentication($token, $user, $audioFilePath);
$message = $this->plugin->get_lang('AuthentifySuccess'); $message = $this->plugin->get_lang('AuthentifySuccess');
} catch (\Exception $exception) {
$message = $this->plugin->get_lang('AuthentifyFailed')
.PHP_EOL
.$exception->getMessage();
$success = false;
}
if (!$success) { if (!$success) {
if (!empty($lpItemInfo)) { if (!empty($lpItemInfo)) {
@ -191,8 +202,6 @@ class AuthenticationController extends BaseController
$this->plugin->addAuthenticationAttempt(LogEvent::STATUS_FAILED, $user->getId()); $this->plugin->addAuthenticationAttempt(LogEvent::STATUS_FAILED, $user->getId());
} }
$message = $this->plugin->get_lang('AuthentifyFailed');
$authTokenRequest = new ApiRequest(); $authTokenRequest = new ApiRequest();
$authTokenResponse = $authTokenRequest->createAuthenticationSessionToken($user); $authTokenResponse = $authTokenRequest->createAuthenticationSessionToken($user);
@ -210,8 +219,8 @@ class AuthenticationController extends BaseController
if ($maxAttempts && $failedLogins >= $maxAttempts) { if ($maxAttempts && $failedLogins >= $maxAttempts) {
$message .= PHP_EOL $message .= PHP_EOL
.'<span data-reach-attempts="true">'.$this->plugin->get_lang('MaxAttemptsReached').'</span>' .'<span data-reach-attempts="true">'.$this->plugin->get_lang('MaxAttemptsReached').'</span>'
.PHP_EOL .PHP_EOL.PHP_EOL
.'<br><strong>' .'<strong>'
.$this->plugin->get_lang('LoginWithUsernameAndPassword') .$this->plugin->get_lang('LoginWithUsernameAndPassword')
.'</strong>'; .'</strong>';
@ -224,7 +233,7 @@ class AuthenticationController extends BaseController
$message .= PHP_EOL.$this->plugin->get_lang('TryAgain'); $message .= PHP_EOL.$this->plugin->get_lang('TryAgain');
if ('true' === api_get_setting('allow_lostpassword')) { if ('true' === api_get_setting('allow_lostpassword')) {
$message .= '<br>' $message .= PHP_EOL
.Display::url( .Display::url(
get_lang('LostPassword'), get_lang('LostPassword'),
api_get_path(WEB_CODE_PATH).'auth/lostPassword.php', api_get_path(WEB_CODE_PATH).'auth/lostPassword.php',
@ -235,7 +244,7 @@ class AuthenticationController extends BaseController
} }
$result['resultHtml'] = Display::return_message( $result['resultHtml'] = Display::return_message(
$message, nl2br($message),
$success ? 'success' : 'warning', $success ? 'success' : 'warning',
false false
); );

@ -116,7 +116,6 @@ class ApiRequest
$langIso = api_get_language_isocode($user ? $user->getLanguage() : null); $langIso = api_get_language_isocode($user ? $user->getLanguage() : null);
try {
$this->sendRequest( $this->sendRequest(
'post', 'post',
'auth', 'auth',
@ -134,11 +133,6 @@ class ApiRequest
], ],
] ]
); );
return true;
} catch (\Exception $e) {
return false;
}
} }
/** /**
@ -180,11 +174,19 @@ class ApiRequest
$responseBody = $requestException->getResponse()->getBody()->getContents(); $responseBody = $requestException->getResponse()->getBody()->getContents();
$json = json_decode($responseBody, true); $json = json_decode($responseBody, true);
if (empty($json['message'])) { $message = '';
throw new \Exception($requestException->getMessage());
}
if (isset($json['asserts'])) {
foreach ($json['asserts'] as $assert) {
if (in_array($assert['value'], ['valid_audio', 'invalid_audio'])) {
$message .= $assert['message'].PHP_EOL;
}
}
} elseif (empty($json['message'])) {
$message = $requestException->getMessage();
} else {
$message = is_array($json['message']) ? implode(PHP_EOL, $json['message']) : $json['message']; $message = is_array($json['message']) ? implode(PHP_EOL, $json['message']) : $json['message'];
}
throw new \Exception($message); throw new \Exception($message);
} catch (Exception $exception) { } catch (Exception $exception) {

Loading…
Cancel
Save