ldap_synchro.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_once __DIR__.'/../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' => 'index.php', "name" => get_lang('Administration'));
  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('LDAP Connection Error'));
  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 = '';
  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 = api_get_user_info_from_username($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. {
  138. $message .= "> $name_session: ".count($UserAdd)." ".get_lang('Added').' '.get_lang('and').' '.count($UserUpdate).' '.get_lang('Modified').'<br/>';
  139. }
  140. else
  141. {
  142. print "> $name_session: ".count($UserAdd).get_lang('Added').' '.get_lang('and').' '.count($UserUpdate).' '.get_lang('Modified')."\n";
  143. }
  144. // Une fois les utilisateurs importer dans la base des utilisateurs, on peux les affecter la session
  145. $result = Database::query("SELECT c_id FROM $tbl_session_rel_course WHERE session_id='$id_session'");
  146. $CourseList = array();
  147. while ($row = Database::fetch_array($result)) {
  148. $CourseList[] = $row['c_id'];
  149. }
  150. foreach ($CourseList as $enreg_course) {
  151. // On ajoute la relation entre l'utilisateur et le cours
  152. foreach ($UserList as $enreg_user) {
  153. $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(session_id,c_id,user_id)
  154. VALUES('$id_session','$enreg_course','$enreg_user')";
  155. Database::query($sql);
  156. }
  157. $sql = "SELECT COUNT(user_id) as nbUsers ".
  158. "FROM $tbl_session_rel_course_rel_user ".
  159. "WHERE session_id='$id_session' AND c_id='$enreg_course'";
  160. $rs = Database::query($sql);
  161. list($nbr_users) = Database::fetch_array($rs);
  162. $sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users
  163. WHERE session_id='$id_session' AND c_id = '$enreg_course'";
  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(session_id, user_id, registered_at) ".
  169. "VALUES('$id_session','$enreg_user', '".api_get_utc_datetime()."')";
  170. Database::query($sql);
  171. }
  172. $sql = "SELECT COUNT(user_id) as nbUsers ".
  173. "FROM $tbl_session_rel_user ".
  174. "WHERE session_id='$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. }