newUser.ldap.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. // External login module : LDAP
  3. /**
  4. * This file is included by main/inc/local.inc.php when extldap is activated, a user try to login
  5. * and chamilo does not find his user
  6. * Variables that can be used :
  7. * - $login : string containing the username posted by the user
  8. * - $password : string containing the password posted by the user
  9. *
  10. * Please configure the exldap module in main/auth/external_login/ldap.conf.php
  11. *
  12. * If login succeeds, we have to add the user in the chamilo database and then
  13. * we have 2 choices :
  14. * 1. - set $loginFailed to false,
  15. * - set $_SESSION['_user']['user_id'] with the dokeos user_id
  16. * - set $uidReset to true
  17. * - let the script local.inc.php continue
  18. *
  19. * 2. - set $_SESSION['_user']['user_id'] with the dokeos user_id
  20. * - set $_SESSION['_user']['uidReset'] to true
  21. * - upgrade user info in dokeos database if needeed
  22. * - redirect to any page and let local.inc.php do the magic
  23. *
  24. * If login fails we have also 2 choices :
  25. * 1. - unset $_user['user_id']
  26. * - set $loginFailed=true
  27. * - set $uidReset = false
  28. * User wil then have the user password incorrect message
  29. *
  30. * 2. We redirect the user to index.php with appropriate 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. use \ChamiloSession as Session;
  39. require_once dirname(__FILE__) . '/ldap.inc.php';
  40. require_once dirname(__FILE__) . '/functions.inc.php';
  41. $ldap_user = extldap_authenticate($login, $password);
  42. if ($ldap_user !== false) {
  43. $chamilo_user = extldap_get_chamilo_user($ldap_user);
  44. //username is not on the ldap, we have to use $login variable
  45. $chamilo_user['username'] = $login;
  46. $chamilo_uid = external_add_user($chamilo_user);
  47. if ($chamilo_uid !== false) {
  48. $loginFailed = false;
  49. $_user['user_id'] = $chamilo_uid;
  50. $_user['status'] = (isset($chamilo_user['status']) ? $chamilo_user['status'] : 5);
  51. $_user['uidReset'] = true;
  52. Session::write('_user', $_user);
  53. $uidReset = true;
  54. // Is user admin?
  55. if ($chamilo_user['admin'] === true) {
  56. $is_platformAdmin = true;
  57. Database::query("INSERT INTO admin values ('$chamilo_uid')");
  58. }
  59. }
  60. event_login();
  61. } else {
  62. $loginFailed = true;
  63. $uidReset = false;
  64. unset($_user['user_id']);
  65. }