WhispeakConditionalLoginHook.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class WhispeakConditionalLoginHook.
  5. *
  6. * Implements a Two-Factor Authentication with Whispeak.
  7. */
  8. class WhispeakConditionalLoginHook extends HookObserver implements HookConditionalLoginObserverInterface
  9. {
  10. /**
  11. * WhispeakConditionalLoginHook constructor.
  12. */
  13. protected function __construct()
  14. {
  15. parent::__construct(
  16. 'plugin/whispeakauth/WhispeakAuthPlugin.php',
  17. 'whispeakauth'
  18. );
  19. }
  20. /**
  21. * Return an associative array (callable, url) needed for Conditional Login.
  22. * <code>
  23. * [
  24. * 'conditional_function' => function (array $userInfo) {},
  25. * 'url' => '',
  26. * ]
  27. * </code>
  28. * conditional_function returns false to redirect to the url and returns true to continue with the classical login.
  29. *
  30. * @param HookConditionalLoginEventInterface $hook
  31. *
  32. * @return array
  33. */
  34. public function hookConditionalLogin(HookConditionalLoginEventInterface $hook)
  35. {
  36. return [
  37. 'conditional_function' => function (array $userInfo) {
  38. $isEnrolled = WhispeakAuthPlugin::checkUserIsEnrolled($userInfo['user_id']);
  39. if (!$isEnrolled) {
  40. return true;
  41. }
  42. $user2fa = (int) ChamiloSession::read(WhispeakAuthPlugin::SESSION_2FA_USER, 0);
  43. if ($user2fa === (int) $userInfo['user_id']) {
  44. ChamiloSession::erase(WhispeakAuthPlugin::SESSION_2FA_USER);
  45. return true;
  46. }
  47. ChamiloSession::write(WhispeakAuthPlugin::SESSION_2FA_USER, $userInfo['user_id']);
  48. return false;
  49. },
  50. 'url' => api_get_path(WEB_PLUGIN_PATH).$this->getPluginName().'/authentify.php',
  51. ];
  52. }
  53. }