ldap_synchro.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. * @deprecated ?
  6. */
  7. /**
  8. * Code
  9. */
  10. exit(); //not yet functional, needs to be revised
  11. // name of the language file that needs to be included
  12. $language_file = 'admin';
  13. $cidReset = true;
  14. require '../inc/global.inc.php';
  15. $libpath = api_get_path(LIBRARY_PATH);
  16. require_once api_get_path(SYS_CODE_PATH).'auth/ldap/authldap.php';
  17. $annee_base = date('Y');
  18. // setting the section (for the tabs)
  19. $this_section = SECTION_PLATFORM_ADMIN;
  20. //api_protect_admin_script(); // on vire la secu... qui n'a pas lieu d'etre ici (script de synchro)
  21. // setting breadcrumbs
  22. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
  23. $interbreadcrumb[] = array('url' => api_get_self(), "name" => "Liste des sessions");
  24. // Database Table Definitions
  25. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  26. $tbl_session_rel_class = Database::get_main_table(TABLE_MAIN_SESSION_CLASS);
  27. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  28. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  29. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  30. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  31. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  32. $tbl_class = Database::get_main_table(TABLE_MAIN_CLASS);
  33. $tbl_class_user = Database::get_main_table(TABLE_MAIN_CLASS_USER);
  34. $tbl_session_rel_etape = "session_rel_etape";
  35. $message = "";
  36. $result = Database::query("SELECT id, name FROM $tbl_session");
  37. $Sessions = Database::store_result($result);
  38. $result = Database::query($sql);
  39. $users = Database::store_result($result);
  40. foreach ($Sessions as $session) {
  41. $id_session = $session['id'];
  42. $name_session = $session['name'];
  43. $UserList = array();
  44. $UserUpdate = array();
  45. $UserAdd = array();
  46. // Parse des code etape de la session
  47. /*
  48. $sql = "SELECT id_session, code_etape, etape_description, code_ufr, annee
  49. FROM $tbl_session_rel_etape
  50. WHERE id_session='$id_session'
  51. ORDER BY code_ufr, code_etape";
  52. $result = Database::query($sql);
  53. */
  54. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  55. ldap_set_version($ds);
  56. // Import des utilisateurs des etapes dans la session
  57. if ($ds) {
  58. $r = false;
  59. $res = ldap_handle_bind($ds, $r);
  60. $UserList = array();
  61. if ($result !== false) {
  62. //while($row = Database::fetch_array($result))
  63. //{
  64. /*
  65. $annee = $row['annee'];
  66. $code_ufr = $row['code_ufr'];
  67. $etape = $row['code_etape'];
  68. */
  69. // LDAP Query
  70. // edupersonorgunitdn=ou=12CI1,ou=2006,ou=diploma,o=Paris1,dc=univ-paris1,dc=fr
  71. //etapescommented
  72. //$sr = @ ldap_search($ds, "ou=people,$LDAPbasedn", "edupersonorgunitdn=ou=$etape,ou=$annee,ou=diploma,$LDAPbasedn");
  73. $sr = @ ldap_search($ds, $ldap_basedn, '(uid=*)');
  74. $info = ldap_get_entries($ds, $sr);
  75. for ($key = 0; $key < $info["count"]; $key++) {
  76. echo "<pre>";
  77. print_r($info[$key]);
  78. echo "</pre>";
  79. $lastname = api_utf8_decode($info[$key]["sn"][0], api_get_system_encoding());
  80. $firstname = api_utf8_decode($info[$key]["givenname"][0], api_get_system_encoding());
  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('platformLanguage'),
  105. $phone,
  106. $picture_uri,
  107. $auth_source,
  108. $expiration_date,
  109. $active
  110. );
  111. $UserAdd[] = $user_id;
  112. } else {
  113. $user = UserManager::get_user_info($username);
  114. $user_id = $user['user_id'];
  115. UserManager::update_user(
  116. $user_id,
  117. $firstname,
  118. $lastname,
  119. $username,
  120. null,
  121. null,
  122. $email,
  123. $status,
  124. $official_code,
  125. $phone,
  126. $picture_uri,
  127. $expiration_date,
  128. $active
  129. );
  130. $UserUpdate[] = $user_id;
  131. }
  132. $UserList[] = $user_id;
  133. }
  134. //}
  135. }
  136. if (isset($included) && ($included)) {
  137. $message .= "> $name_session: ".count($UserAdd)." ".get_lang('Added').' '.get_lang('And').' '.count(
  138. $UserUpdate
  139. ).' '.get_lang('Modified').'<br/>';
  140. } else {
  141. print "> $name_session: ".count($UserAdd).get_lang('Added').' '.get_lang('And').' '.count(
  142. $UserUpdate
  143. ).' '.get_lang('Modified')."\n";
  144. }
  145. // Une fois les utilisateurs importer dans la base des utilisateurs, on peux les affecter a� la session
  146. $result = Database::query("SELECT c_id FROM $tbl_session_rel_course WHERE id_session='$id_session'");
  147. $CourseList = array();
  148. while ($row = Database::fetch_array($result)) {
  149. $CourseList[] = $row['c_id'];
  150. }
  151. foreach ($CourseList as $courseId) {
  152. // On ajoute la relation entre l'utilisateur et le cours
  153. foreach ($UserList as $enreg_user) {
  154. Database::query(
  155. "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,c_id,id_user) VALUES('$id_session','$courseId','$enreg_user')"
  156. );
  157. }
  158. $sql = "SELECT COUNT(id_user) as nbUsers ".
  159. "FROM $tbl_session_rel_course_rel_user ".
  160. "WHERE id_session='$id_session' AND c_id='$courseId'";
  161. $rs = Database::query($sql);
  162. list($nbr_users) = Database::fetch_array($rs);
  163. $sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND c_id='$courseId'";
  164. Database::query($sql);
  165. }
  166. // On ajoute la relation entre l'utilisateur et la session
  167. foreach ($UserList as $enreg_user) {
  168. $sql = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) ".
  169. "VALUES('$id_session','$enreg_user')";
  170. Database::query($sql);
  171. }
  172. $sql = "SELECT COUNT(id_user) as nbUsers ".
  173. "FROM $tbl_session_rel_user ".
  174. "WHERE id_session='$id_session' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."";
  175. $rs = Database::query($sql);
  176. list($nbr_users) = Database::fetch_array($rs);
  177. $sql = "UPDATE $tbl_session SET nbr_users=$nbr_users WHERE id='$id_session'";
  178. Database::query($sql);
  179. }
  180. }