| Conditions | 33 |
| Paths | 2991 |
| Total Lines | 194 |
| Code Lines | 117 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 118 | public function ajax() |
||
| 119 | { |
||
| 120 | $userId = api_get_user_id(); |
||
| 121 | $user2fa = ChamiloSession::read(WhispeakAuthPlugin::SESSION_2FA_USER, 0); |
||
| 122 | |||
| 123 | if (!empty($user2fa) || !empty($userId)) { |
||
| 124 | $isAllowed = !empty($_FILES['audio']); |
||
| 125 | } else { |
||
| 126 | $isAllowed = !empty($_POST['username']) && !empty($_FILES['audio']); |
||
| 127 | } |
||
| 128 | |||
| 129 | if (!$isAllowed || !$this->plugin->toolIsEnabled()) { |
||
| 130 | throw new \Exception(get_lang('NotAllowed')); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (!empty($user2fa)) { |
||
| 134 | $user = api_get_user_entity($user2fa); |
||
| 135 | } elseif (!empty($userId)) { |
||
| 136 | $user = api_get_user_entity($userId); |
||
| 137 | } else { |
||
| 138 | /** @var User|null $user */ |
||
| 139 | $user = \UserManager::getRepository()->findOneBy(['username' => $_POST['username']]); |
||
| 140 | } |
||
| 141 | |||
| 142 | if (!$user) { |
||
| 143 | throw new \Exception(get_lang('NotFound')); |
||
| 144 | } |
||
| 145 | |||
| 146 | $audioFilePath = $this->uploadAudioFile($user); |
||
| 147 | |||
| 148 | $failedLogins = ChamiloSession::read(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, 0); |
||
| 149 | $maxAttempts = $this->plugin->getMaxAttempts(); |
||
| 150 | |||
| 151 | if ($maxAttempts && $failedLogins >= $maxAttempts) { |
||
| 152 | throw new \Exception($this->plugin->get_lang('MaxAttemptsReached')); |
||
| 153 | } |
||
| 154 | |||
| 155 | $token = \ChamiloSession::read(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
||
| 156 | |||
| 157 | $request = new ApiRequest(); |
||
| 158 | $success = $request->performAuthentication($token, $user, $audioFilePath); |
||
| 159 | |||
| 160 | \ChamiloSession::erase(\WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
||
| 161 | |||
| 162 | /** @var array $lpItemInfo */ |
||
| 163 | $lpItemInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_LP_ITEM, []); |
||
| 164 | /** @var array $quizQuestionInfo */ |
||
| 165 | $quizQuestionInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []); |
||
| 166 | |||
| 167 | $message = $this->plugin->get_lang('AuthentifySuccess'); |
||
| 168 | |||
| 169 | if (!$success) { |
||
| 170 | if (!empty($lpItemInfo)) { |
||
| 171 | $this->plugin->addAttemptInLearningPath( |
||
| 172 | LogEvent::STATUS_FAILED, |
||
| 173 | $user->getId(), |
||
| 174 | $lpItemInfo['lp_item'], |
||
| 175 | $lpItemInfo['lp'] |
||
| 176 | ); |
||
| 177 | } |
||
| 178 | |||
| 179 | if (!empty($quizQuestionInfo)) { |
||
| 180 | $this->plugin->addAttemptInQuiz( |
||
| 181 | LogEvent::STATUS_FAILED, |
||
| 182 | $user->getId(), |
||
| 183 | $quizQuestionInfo['question'], |
||
| 184 | $quizQuestionInfo['quiz'] |
||
| 185 | ); |
||
| 186 | } |
||
| 187 | |||
| 188 | if (empty($lpItemInfo) && empty($quizQuestionInfo)) { |
||
| 189 | $this->plugin->addAuthenticationAttempt(LogEvent::STATUS_FAILED, $user->getId()); |
||
| 190 | } |
||
| 191 | |||
| 192 | $message = $this->plugin->get_lang('AuthentifyFailed'); |
||
| 193 | |||
| 194 | ChamiloSession::write(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins); |
||
| 195 | |||
| 196 | if ($maxAttempts && $failedLogins >= $maxAttempts) { |
||
| 197 | $message .= PHP_EOL |
||
| 198 | .'<span data-reach-attempts="true">'.$this->plugin->get_lang('MaxAttemptsReached').'</span>' |
||
| 199 | .PHP_EOL |
||
| 200 | .'<br><strong>' |
||
| 201 | .$this->plugin->get_lang('LoginWithUsernameAndPassword') |
||
| 202 | .'</strong>'; |
||
| 203 | |||
| 204 | if (!empty($user2fa)) { |
||
| 205 | Display::addFlash( |
||
| 206 | Display::return_message($message, 'warning', false) |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | } else { |
||
| 210 | $message .= PHP_EOL.$this->plugin->get_lang('TryAgain'); |
||
| 211 | |||
| 212 | if ('true' === api_get_setting('allow_lostpassword')) { |
||
| 213 | $message .= '<br>' |
||
| 214 | .Display::url( |
||
| 215 | get_lang('LostPassword'), |
||
| 216 | api_get_path(WEB_CODE_PATH).'auth/lostPassword.php', |
||
| 217 | ['target' => $lpItemInfo ? '_top' : '_self'] |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | echo Display::return_message( |
||
| 224 | $message, |
||
| 225 | $success ? 'success' : 'warning', |
||
| 226 | false |
||
| 227 | ); |
||
| 228 | |||
| 229 | if (!$success && $maxAttempts && $failedLogins >= $maxAttempts) { |
||
| 230 | ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
||
| 231 | |||
| 232 | if (!empty($lpItemInfo)) { |
||
| 233 | echo '<script>window.location.href = "' |
||
| 234 | .api_get_path(WEB_PLUGIN_PATH) |
||
| 235 | .'whispeakauth/authentify_password.php";</script>'; |
||
| 236 | |||
| 237 | exit; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (!empty($quizQuestionInfo)) { |
||
| 241 | $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; |
||
| 242 | |||
| 243 | ChamiloSession::write(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, true); |
||
| 244 | |||
| 245 | echo "<script>window.location.href = '".$url."';</script>"; |
||
| 246 | |||
| 247 | exit; |
||
| 248 | } |
||
| 249 | |||
| 250 | echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>'; |
||
| 251 | |||
| 252 | exit; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ($success) { |
||
| 256 | ChamiloSession::erase(WhispeakAuthPlugin::SESSION_SENTENCE_TEXT); |
||
| 257 | ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
||
| 258 | |||
| 259 | if (!empty($lpItemInfo)) { |
||
| 260 | ChamiloSession::erase(WhispeakAuthPlugin::SESSION_LP_ITEM); |
||
| 261 | ChamiloSession::erase(WhispeakAuthPlugin::SESSION_2FA_USER); |
||
| 262 | |||
| 263 | $this->plugin->addAttemptInLearningPath( |
||
| 264 | LogEvent::STATUS_SUCCESS, |
||
| 265 | $user->getId(), |
||
| 266 | $lpItemInfo['lp_item'], |
||
| 267 | $lpItemInfo['lp'] |
||
| 268 | ); |
||
| 269 | |||
| 270 | echo '<script>window.location.href = "'.$lpItemInfo['src'].'";</script>'; |
||
| 271 | |||
| 272 | exit; |
||
| 273 | } |
||
| 274 | |||
| 275 | if (!empty($quizQuestionInfo)) { |
||
| 276 | $quizQuestionInfo['passed'] = true; |
||
| 277 | $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params']; |
||
| 278 | |||
| 279 | ChamiloSession::write(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, $quizQuestionInfo); |
||
| 280 | |||
| 281 | $this->plugin->addAttemptInQuiz( |
||
| 282 | LogEvent::STATUS_SUCCESS, |
||
| 283 | $user->getId(), |
||
| 284 | $quizQuestionInfo['question'], |
||
| 285 | $quizQuestionInfo['quiz'] |
||
| 286 | ); |
||
| 287 | |||
| 288 | echo '<script>window.location.href = "'.$url.'";</script>'; |
||
| 289 | |||
| 290 | exit; |
||
| 291 | } |
||
| 292 | |||
| 293 | if (empty($lpItemInfo) && empty($quizQuestionInfo)) { |
||
| 294 | $this->plugin->addAuthenticationAttempt(LogEvent::STATUS_SUCCESS, $user->getId()); |
||
| 295 | } |
||
| 296 | |||
| 297 | $loggedUser = [ |
||
| 298 | 'user_id' => $user->getId(), |
||
| 299 | 'status' => $user->getStatus(), |
||
| 300 | 'uidReset' => true, |
||
| 301 | ]; |
||
| 302 | |||
| 303 | if (empty($user2fa)) { |
||
| 304 | ChamiloSession::write(WhispeakAuthPlugin::SESSION_2FA_USER, $user->getId()); |
||
| 305 | } |
||
| 306 | |||
| 307 | ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS); |
||
| 308 | ChamiloSession::write('_user', $loggedUser); |
||
| 309 | Login::init_user($user->getId(), true); |
||
| 310 | |||
| 311 | echo '<script>window.location.href = "'.api_get_path(WEB_PATH).'";</script>'; |
||
| 312 | } |
||
| 340 |