login.ldap.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. use ChamiloSession as Session;
  3. // External login module : LDAP
  4. /**
  5. * This file is included in main/inc/local.inc.php at user login if the user have 'external_ldap' in
  6. * his auth_source field instead of platform.
  7. *
  8. * Variables that can be used :
  9. * - $login : string containing the username posted by the user
  10. * - $password : string containing the password posted by the user
  11. * - $uData : associative array with those keys :
  12. * -username
  13. * -password
  14. * -auth_source
  15. * -active
  16. * -expiration_date
  17. *
  18. * If login succeeds, we have 2 choices :
  19. * 1. - set $loginFailed to false,
  20. * - set $_SESSION['_user']['user_id'] with the Chamilo user_id
  21. * - set $uidReset to true
  22. * - upgrade user info in chamilo database if needed
  23. * - let the script local.inc.php continue
  24. *
  25. * 2. - set $_SESSION['_user']['user_id'] with the Chamilo user_id
  26. * - set $_SESSION['_user']['uidReset'] to true
  27. * - upgrade user info in chamilo database if needed
  28. * - redirect to any page and let local.inc.php do the magic
  29. *
  30. * If login fails we have to redirect to index.php with the right message
  31. * Possible messages are :
  32. * - index.php?loginFailed=1&error=access_url_inactive
  33. * - index.php?loginFailed=1&error=account_expired
  34. * - index.php?loginFailed=1&error=account_inactive
  35. * - index.php?loginFailed=1&error=user_password_incorrect
  36. * - index.php?loginFailed=1&error=unrecognize_sso_origin');
  37. *
  38. * */
  39. require_once __DIR__.'/ldap.inc.php';
  40. require_once __DIR__.'/functions.inc.php';
  41. $debug = false;
  42. if ($debug) {
  43. error_log('Entering login.ldap.php');
  44. }
  45. $ldap_user = extldap_authenticate($login, $password);
  46. if ($ldap_user !== false) {
  47. if ($debug) {
  48. error_log('extldap_authenticate works');
  49. }
  50. $chamilo_user = extldap_get_chamilo_user($ldap_user);
  51. //userid is not on the ldap, we have to use $uData variable from local.inc.php
  52. $chamilo_user['user_id'] = $uData['user_id'];
  53. if ($debug) {
  54. error_log("chamilo_user found user_id: {$uData['user_id']}");
  55. }
  56. //U pdate user info
  57. if (isset($extldap_config['update_userinfo']) && $extldap_config['update_userinfo']) {
  58. external_update_user($chamilo_user);
  59. if ($debug) {
  60. error_log("Calling external_update_user");
  61. }
  62. }
  63. $loginFailed = false;
  64. $_user['user_id'] = $chamilo_user['user_id'];
  65. $_user['status'] = (isset($chamilo_user['status']) ? $chamilo_user['status'] : 5);
  66. $_user['uidReset'] = true;
  67. Session::write('_user', $_user);
  68. $uidReset = true;
  69. $logging_in = true;
  70. Event::eventLogin($_user['user_id']);
  71. } else {
  72. if ($debug) {
  73. error_log('extldap_authenticate error');
  74. }
  75. $loginFailed = true;
  76. $uidReset = false;
  77. if (isset($_user) && isset($_user['user_id'])) {
  78. unset($_user['user_id']);
  79. }
  80. }