login.ldap.php 2.8 KB

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