ldap_synchro.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /**
  7. * Code
  8. */
  9. exit(); //not yet functional, needs to be revised
  10. $cidReset=true;
  11. require('../inc/global.inc.php');
  12. $libpath = api_get_path(LIBRARY_PATH);
  13. require_once('../auth/ldap/authldap.php');
  14. $annee_base=date('Y');
  15. // setting the section (for the tabs)
  16. $this_section = SECTION_PLATFORM_ADMIN;
  17. //api_protect_admin_script(); // on vire la secu... qui n'a pas lieu d'etre ici (script de synchro)
  18. // setting breadcrumbs
  19. $interbreadcrumb[]=array('url' => Container::getRouter()->generate('administration'),"name" => get_lang('PlatformAdmin'));
  20. $interbreadcrumb[]=array('url' => api_get_self(),"name" => "Liste des sessions");
  21. // Database Table Definitions
  22. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  23. $tbl_session_rel_class = Database::get_main_table(TABLE_MAIN_SESSION_CLASS);
  24. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  25. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  26. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  27. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  28. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  29. $tbl_class = Database::get_main_table(TABLE_MAIN_CLASS);
  30. $tbl_class_user = Database::get_main_table(TABLE_MAIN_CLASS_USER);
  31. $tbl_session_rel_etape = "session_rel_etape";
  32. $message="";
  33. $result=Database::query("SELECT id, name FROM $tbl_session");
  34. $Sessions=Database::store_result($result);
  35. $result=Database::query($sql);
  36. $users=Database::store_result($result);
  37. foreach($Sessions as $session){
  38. $id_session = $session['id'];
  39. $name_session = $session['name'];
  40. $UserList=array();
  41. $UserUpdate=array();
  42. $UserAdd=array();
  43. // Parse des code etape de la session
  44. /*
  45. $sql = "SELECT id_session, code_etape, etape_description, code_ufr, annee
  46. FROM $tbl_session_rel_etape
  47. WHERE id_session='$id_session'
  48. ORDER BY code_ufr, code_etape";
  49. $result = Database::query($sql);
  50. */
  51. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  52. ldap_set_version($ds);
  53. // Import des utilisateurs des etapes dans la session
  54. if ($ds)
  55. {
  56. $r = false;
  57. $res = ldap_handle_bind($ds, $r);
  58. $UserList=array();
  59. if($result !== false)
  60. {
  61. //while($row = Database::fetch_array($result))
  62. //{
  63. /*
  64. $annee = $row['annee'];
  65. $code_ufr = $row['code_ufr'];
  66. $etape = $row['code_etape'];
  67. */
  68. // LDAP Query
  69. // edupersonorgunitdn=ou=12CI1,ou=2006,ou=diploma,o=Paris1,dc=univ-paris1,dc=fr
  70. //etapescommented
  71. //$sr = @ ldap_search($ds, "ou=people,$LDAPbasedn", "edupersonorgunitdn=ou=$etape,ou=$annee,ou=diploma,$LDAPbasedn");
  72. $sr = @ ldap_search($ds, $ldap_basedn, '(uid=*)');
  73. $info = ldap_get_entries($ds, $sr);
  74. for ($key = 0; $key < $info["count"]; $key ++)
  75. {
  76. echo "<pre>";
  77. print_r($info[$key]);
  78. echo "</pre>";
  79. $lastname = api_utf8_decode($info[$key]["sn"][0]);
  80. $firstname = api_utf8_decode($info[$key]["givenname"][0]);
  81. $email = $info[$key]["mail"][0];
  82. // Get uid from dn
  83. $dn_array=ldap_explode_dn($info[$key]["dn"],1);
  84. $username = $dn_array[0]; // uid is first key
  85. $outab[] = $info[$key]["edupersonprimaryaffiliation"][0]; // Ici "student"
  86. $val = ldap_get_values_len($ds, $sr, "userPassword");
  87. $password = $val[0];
  88. // Pour faciliter la gestion on ajoute le code "etape-annee"
  89. $official_code=$etape."-".$annee;
  90. $auth_source="ldap";
  91. // Pas de date d'expiration d'etudiant (a recuperer par rapport au shadow expire LDAP)
  92. $expiration_date='0000-00-00 00:00:00';
  93. $active=1;
  94. // Ajout de l'utilisateur
  95. if (UserManager::is_username_available($username)) {
  96. $user_id = UserManager::create_user(
  97. $firstname,
  98. $lastname,
  99. $status,
  100. $email,
  101. $username,
  102. $password,
  103. $official_code,
  104. api_get_setting('language.platform_language'),
  105. $phone,
  106. $picture_uri,
  107. $auth_source,
  108. $expiration_date,
  109. $active
  110. );
  111. $UserAdd[]=$user_id;
  112. } else {
  113. $user = api_get_user_info_from_username($username);
  114. $user_id=$user['user_id'];
  115. UserManager::update_user($user_id, $firstname, $lastname, $username, null, null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active);
  116. $UserUpdate[]=$user_id;
  117. }
  118. $UserList[]=$user_id;
  119. }
  120. //}
  121. }
  122. if (isset($included) && ($included))
  123. {
  124. $message .= "> $name_session: ".count($UserAdd)." ".get_lang('Added').' '.get_lang('And').' '.count($UserUpdate).' '.get_lang('Modified').'<br/>';
  125. }
  126. else
  127. {
  128. print "> $name_session: ".count($UserAdd).get_lang('Added').' '.get_lang('And').' '.count($UserUpdate).' '.get_lang('Modified')."\n";
  129. }
  130. // Une fois les utilisateurs importer dans la base des utilisateurs, on peux les affecter la session
  131. $result=Database::query("SELECT c_id FROM $tbl_session_rel_course WHERE session_id='$id_session'");
  132. $CourseList=array();
  133. while($row=Database::fetch_array($result)) {
  134. $CourseList[]= $row['c_id'];
  135. }
  136. foreach ($CourseList as $enreg_course) {
  137. // On ajoute la relation entre l'utilisateur et le cours
  138. foreach ($UserList as $enreg_user) {
  139. $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(session_id,c_id,user_id)
  140. VALUES('$id_session','$enreg_course','$enreg_user')";
  141. Database::query($sql);
  142. }
  143. $sql = "SELECT COUNT(user_id) as nbUsers " .
  144. "FROM $tbl_session_rel_course_rel_user " .
  145. "WHERE session_id='$id_session' AND c_id='$enreg_course'";
  146. $rs = Database::query($sql);
  147. list($nbr_users) = Database::fetch_array($rs);
  148. $sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users
  149. WHERE session_id='$id_session' AND c_id = '$enreg_course'";
  150. Database::query($sql);
  151. }
  152. // On ajoute la relation entre l'utilisateur et la session
  153. foreach($UserList as $enreg_user){
  154. $sql = "INSERT IGNORE INTO $tbl_session_rel_user(session_id, user_id, registered_at) " .
  155. "VALUES('$id_session','$enreg_user', '" . api_get_utc_datetime() . "')";
  156. Database::query($sql);
  157. }
  158. $sql = "SELECT COUNT(user_id) as nbUsers " .
  159. "FROM $tbl_session_rel_user " .
  160. "WHERE session_id='$id_session' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."";
  161. $rs = Database::query($sql);
  162. list($nbr_users) = Database::fetch_array($rs);
  163. $sql = "UPDATE $tbl_session SET nbr_users=$nbr_users WHERE id='$id_session'";
  164. Database::query($sql);
  165. }
  166. }