authentify_password.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\PluginBundle\Entity\WhispeakAuth\LogEvent;
  4. $cidReset = true;
  5. require_once __DIR__.'/../../../main/inc/global.inc.php';
  6. api_block_anonymous_users(false);
  7. $plugin = WhispeakAuthPlugin::create();
  8. $plugin->protectTool(false);
  9. $tokenIsValid = Security::check_token();
  10. if (!$tokenIsValid) {
  11. WhispeakAuthPlugin::displayNotAllowedMessage();
  12. }
  13. $maxAttempts = $plugin->getMaxAttempts();
  14. $failedLogins = ChamiloSession::read(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, 0);
  15. if ($maxAttempts && $failedLogins >= $maxAttempts) {
  16. echo Display::return_message($plugin->get_lang('MaxAttemptsReached'), 'warning');
  17. exit;
  18. }
  19. $user = api_get_user_entity(api_get_user_id());
  20. $password = isset($_POST['password']) ? $_POST['password'] : null;
  21. if (empty($password) || empty($user)) {
  22. WhispeakAuthPlugin::displayNotAllowedMessage();
  23. }
  24. if (!in_array($user->getAuthSource(), [PLATFORM_AUTH_SOURCE, CAS_AUTH_SOURCE])) {
  25. WhispeakAuthPlugin::displayNotAllowedMessage();
  26. }
  27. /** @var array $lpItemInfo */
  28. $lpItemInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_LP_ITEM, []);
  29. /** @var array $quizQuestionInfo */
  30. $quizQuestionInfo = ChamiloSession::read(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, []);
  31. $isValidPassword = UserManager::isPasswordValid($user->getPassword(), $password, $user->getSalt());
  32. $isActive = $user->isActive();
  33. $isExpired = empty($user->getExpirationDate()) || $user->getExpirationDate() > api_get_utc_datetime(null, false, true);
  34. $userPass = true;
  35. if (!$isValidPassword || !$isActive || !$isExpired) {
  36. if (!empty($lpItemInfo)) {
  37. $plugin->addAttemptInLearningPath(
  38. LogEvent::STATUS_FAILED,
  39. $user->getId(),
  40. $lpItemInfo['lp_item'],
  41. $lpItemInfo['lp']
  42. );
  43. } elseif (!empty($quizQuestionInfo)) {
  44. $plugin->addAttemptInQuiz(
  45. LogEvent::STATUS_FAILED,
  46. $user->getId(),
  47. $quizQuestionInfo['question'],
  48. $quizQuestionInfo['quiz']
  49. );
  50. }
  51. $userPass = false;
  52. $message = $plugin->get_lang('AuthentifyFailed');
  53. if (!$isActive) {
  54. $message .= PHP_EOL.get_lang('Account inactive');
  55. }
  56. if (!$isExpired) {
  57. $message .= PHP_EOL.get_lang('AccountExpired');
  58. }
  59. ChamiloSession::write(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins);
  60. if ($maxAttempts && $failedLogins >= $maxAttempts) {
  61. $message .= PHP_EOL.'<span data-reach-attempts="true">'.$plugin->get_lang('MaxAttemptsReached').'</span>';
  62. } else {
  63. $message .= PHP_EOL.$plugin->get_lang('TryAgain');
  64. }
  65. echo Display::return_message($message, 'error', false);
  66. if (!$maxAttempts ||
  67. ($maxAttempts && $failedLogins >= $maxAttempts)
  68. ) {
  69. $userPass = true;
  70. }
  71. } elseif ($isValidPassword) {
  72. if (!empty($lpItemInfo)) {
  73. $plugin->addAttemptInLearningPath(
  74. LogEvent::STATUS_SUCCESS,
  75. $user->getId(),
  76. $lpItemInfo['lp_item'],
  77. $lpItemInfo['lp']
  78. );
  79. } elseif (!empty($quizQuestionInfo)) {
  80. $plugin->addAttemptInQuiz(
  81. LogEvent::STATUS_SUCCESS,
  82. $user->getId(),
  83. $quizQuestionInfo['question'],
  84. $quizQuestionInfo['quiz']
  85. );
  86. }
  87. echo Display::return_message($plugin->get_lang('AuthentifySuccess'), 'success');
  88. }
  89. if ($userPass) {
  90. $url = '';
  91. ChamiloSession::erase(WhispeakAuthPlugin::SESSION_FAILED_LOGINS);
  92. ChamiloSession::erase(WhispeakAuthPlugin::SESSION_2FA_USER);
  93. if ($lpItemInfo) {
  94. ChamiloSession::erase(WhispeakAuthPlugin::SESSION_LP_ITEM);
  95. $url = $lpItemInfo['src'];
  96. } elseif ($quizQuestionInfo) {
  97. $quizQuestionInfo['passed'] = true;
  98. $url = api_get_path(WEB_CODE_PATH).'exercise/exercise_submit.php?'.$quizQuestionInfo['url_params'];
  99. ChamiloSession::write(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, $quizQuestionInfo);
  100. }
  101. if (!empty($url)) {
  102. echo '
  103. <script>window.location.href = "'.$url.'";</script>
  104. ';
  105. }
  106. }