From b2dd661fa21e2e804558fdde85fae612c054ecc0 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Fri, 5 Jul 2019 11:19:04 -0500 Subject: [PATCH] Pluing: Whispeak validate maximum attempts - refs BT#15820 --- plugin/whispeakauth/WhispeakAuthPlugin.php | 2 ++ plugin/whispeakauth/ajax/record_audio.php | 41 +++++++++++++++++----- plugin/whispeakauth/lang/english.php | 4 ++- plugin/whispeakauth/lang/french.php | 4 +++ plugin/whispeakauth/lang/spanish.php | 4 +++ 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/plugin/whispeakauth/WhispeakAuthPlugin.php b/plugin/whispeakauth/WhispeakAuthPlugin.php index 55451059fc..39f730cae2 100644 --- a/plugin/whispeakauth/WhispeakAuthPlugin.php +++ b/plugin/whispeakauth/WhispeakAuthPlugin.php @@ -18,6 +18,8 @@ class WhispeakAuthPlugin extends Plugin const API_URL = 'http://api.whispeak.io:8080/v1.1/'; + const SESSION_FAILED_LOGINS = 'whispeak_failed_logins'; + /** * StudentFollowUpPlugin constructor. */ diff --git a/plugin/whispeakauth/ajax/record_audio.php b/plugin/whispeakauth/ajax/record_audio.php index 76c2d7f62d..aa79199778 100644 --- a/plugin/whispeakauth/ajax/record_audio.php +++ b/plugin/whispeakauth/ajax/record_audio.php @@ -22,6 +22,8 @@ if ($isEnrollment) { $isAllowed = !empty($_FILES['audio']); } elseif ($isAuthentify) { $isAllowed = !empty($_POST['username']) && !empty($_FILES['audio']); +} else { + $isAllowed = false; } if (!$isAllowed) { @@ -34,7 +36,13 @@ $plugin = WhispeakAuthPlugin::create(); $plugin->protectTool(false); +$failedLogins = 0; +$maxAttempts = 0; + if ($isAuthentify) { + $failedLogins = ChamiloSession::read(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, 0); + $maxAttempts = $plugin->getMaxAttempts(); + $em = Database::getManager(); /** @var User|null $user */ $user = $em->getRepository('ChamiloUserBundle:User')->findOneBy(['username' => $_POST['username']]); @@ -93,9 +101,9 @@ if ($isEnrollment) { $qualityNote = !empty($enrollmentResult['quality']) ? explode('|', $enrollmentResult['quality']) : []; $qualityNote = array_map('ucfirst', $qualityNote); - if ($reliability <= 0) { - $message = $plugin->get_lang('EnrollmentSignature0'); - } else { + $message = $plugin->get_lang('EnrollmentSignature0'); + + if ($reliability > 0) { $plugin->saveEnrollment($user, $enrollmentResult['wsid']); $message = ''.$plugin->get_lang('EnrollmentSuccess').''; @@ -115,16 +123,22 @@ if ($isEnrollment) { } if ($isAuthentify) { + if ($maxAttempts && $failedLogins >= $maxAttempts) { + echo Display::return_message($plugin->get_lang('MaxAttemptsReached'), 'warning'); + + exit; + } + $wsid = WhispeakAuthPlugin::getAuthUidValue($user->getId()); try { if (empty($wsid)) { - throw new Exception($plugin->get_lang('AuthentifyFailed')); + throw new Exception(); } $authentifyResult = $plugin->authentify($wsid->getValue(), $newFullPath); } catch (Exception $exception) { - echo Display::return_message($plugin->get_lang('AuthentifyFailed'), 'error'); + echo Display::return_message($plugin->get_lang('TryAgain'), 'error'); exit; } @@ -133,14 +147,22 @@ if ($isAuthentify) { $qualityNote = !empty($authentifyResult['quality']) ? explode('|', $authentifyResult['quality']) : []; $qualityNote = array_map('ucfirst', $qualityNote); + $message = $plugin->get_lang('AuthentifySuccess'); + if (!$success) { - $message = $plugin->get_lang('TryAgain'); - } else { - $message = $plugin->get_lang('AuthentifySuccess'); + $message = $plugin->get_lang('AuthentifyFailed'); + + ChamiloSession::write(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins); + + if ($maxAttempts && $failedLogins >= $maxAttempts) { + $message .= PHP_EOL.$plugin->get_lang('MaxAttemptsReached'); + } else { + $message .= PHP_EOL.$plugin->get_lang('TryAgain'); + } } foreach ($qualityNote as $note) { - $message .= PHP_EOL.'
'.$plugin->get_lang("AudioQuality$note"); + $message .= '
'.PHP_EOL.$plugin->get_lang("AudioQuality$note"); } echo Display::return_message( @@ -156,6 +178,7 @@ if ($isAuthentify) { 'uidReset' => true, ]; + ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); ChamiloSession::write('_user', $loggedUser); Login::init_user($user->getId(), true); diff --git a/plugin/whispeakauth/lang/english.php b/plugin/whispeakauth/lang/english.php index 8290ffb046..c52b66b602 100644 --- a/plugin/whispeakauth/lang/english.php +++ b/plugin/whispeakauth/lang/english.php @@ -41,4 +41,6 @@ $strings['AudioQualityNoisy'] = 'Too noisy audio'; $strings['AudioQualityFrequency'] = 'Missing some audio frequencies'; $strings['AudioQualityPoorness'] = 'Too poor general audio quality'; -$strings['AgreeAllowResearch'] = 'I agree to allow the use of data for research (no commercial usage)'; +$strings['AgreeAllowResearch'] = 'I agree to allow the use of data for research (no commercial usage).'; + +$strings['MaxAttemptsReached'] = 'You reached the maximum number of attempts allowed.'; diff --git a/plugin/whispeakauth/lang/french.php b/plugin/whispeakauth/lang/french.php index af53491d28..56201b961f 100644 --- a/plugin/whispeakauth/lang/french.php +++ b/plugin/whispeakauth/lang/french.php @@ -40,3 +40,7 @@ $strings['AudioQualityLoud'] = 'Too loud audio'; $strings['AudioQualityNoisy'] = 'Too noisy audio'; $strings['AudioQualityFrequency'] = 'Missing some audio frequencies'; $strings['AudioQualityPoorness'] = 'Too poor general audio quality'; + +$strings['AgreeAllowResearch'] = 'I agree to allow the use of data for research (no commercial usage).'; + +$strings['MaxAttemptsReached'] = 'You reached the maximum number of attempts allowed.'; diff --git a/plugin/whispeakauth/lang/spanish.php b/plugin/whispeakauth/lang/spanish.php index f6cf977f0f..04da40872d 100644 --- a/plugin/whispeakauth/lang/spanish.php +++ b/plugin/whispeakauth/lang/spanish.php @@ -40,3 +40,7 @@ $strings['AudioQualityLoud'] = 'Audio demasiado alto'; $strings['AudioQualityNoisy'] = 'Audio demasiado ruidoso'; $strings['AudioQualityFrequency'] = 'Falta algunas frecuencias de audio'; $strings['AudioQualityPoorness'] = 'Calidad de audio general demasiado pobre'; + +$strings['AgreeAllowResearch'] = 'Estoy de acuerdo en permitir el uso de datos para investigación (no uso comercial).'; + +$strings['MaxAttemptsReached'] = 'Ha alcanzado el número máximo de intentos permitidos.';