newUser.ldap.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 __DIR__.'/ldap.inc.php';
  40. require_once __DIR__.'/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. $chamiloUser = api_get_user_entity($chamilo_uid);
  48. if ($chamiloUser) {
  49. $loginFailed = false;
  50. $_user['user_id'] = $chamiloUser->getId();
  51. $_user['status'] = $chamiloUser->getStatus();
  52. $_user['uidReset'] = true;
  53. Session::write('_user', $_user);
  54. $uidReset = true;
  55. // Is user admin?
  56. if ($chamilo_user['admin'] === true) {
  57. $is_platformAdmin = true;
  58. Database::query("INSERT INTO admin values ('{$chamiloUser->getId()}')");
  59. }
  60. Event::eventLogin($chamiloUser->getId());
  61. MessageManager::sendNotificationByRegisteredUser($chamiloUser);
  62. }
  63. } else {
  64. $loginFailed = true;
  65. $uidReset = false;
  66. }