ldap_synchro.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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' => 'index.php',"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($firstname,$lastname,$status,$email,$username,$password,$official_code,api_get_setting('platformLanguage'),$phone,$picture_uri,$auth_source,$expiration_date,$active);
  97. $UserAdd[]=$user_id;
  98. } else {
  99. $user = api_get_user_info_from_username($username);
  100. $user_id=$user['user_id'];
  101. UserManager::update_user($user_id, $firstname, $lastname, $username, null, null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active);
  102. $UserUpdate[]=$user_id;
  103. }
  104. $UserList[]=$user_id;
  105. }
  106. //}
  107. }
  108. if (isset($included) && ($included))
  109. {
  110. $message .= "> $name_session: ".count($UserAdd)." ".get_lang('Added').' '.get_lang('And').' '.count($UserUpdate).' '.get_lang('Modified').'<br/>';
  111. }
  112. else
  113. {
  114. print "> $name_session: ".count($UserAdd).get_lang('Added').' '.get_lang('And').' '.count($UserUpdate).' '.get_lang('Modified')."\n";
  115. }
  116. // Une fois les utilisateurs importer dans la base des utilisateurs, on peux les affecter la session
  117. $result=Database::query("SELECT c_id FROM $tbl_session_rel_course WHERE session_id='$id_session'");
  118. $CourseList=array();
  119. while($row=Database::fetch_array($result)) {
  120. $CourseList[]= $row['c_id'];
  121. }
  122. foreach ($CourseList as $enreg_course) {
  123. // On ajoute la relation entre l'utilisateur et le cours
  124. foreach ($UserList as $enreg_user) {
  125. $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user(session_id,c_id,user_id)
  126. VALUES('$id_session','$enreg_course','$enreg_user')";
  127. Database::query($sql);
  128. }
  129. $sql = "SELECT COUNT(user_id) as nbUsers " .
  130. "FROM $tbl_session_rel_course_rel_user " .
  131. "WHERE session_id='$id_session' AND c_id='$enreg_course'";
  132. $rs = Database::query($sql);
  133. list($nbr_users) = Database::fetch_array($rs);
  134. $sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users
  135. WHERE session_id='$id_session' AND c_id = '$enreg_course'";
  136. Database::query($sql);
  137. }
  138. // On ajoute la relation entre l'utilisateur et la session
  139. foreach($UserList as $enreg_user){
  140. $sql = "INSERT IGNORE INTO $tbl_session_rel_user(session_id, user_id, registered_at) " .
  141. "VALUES('$id_session','$enreg_user', '" . api_get_utc_datetime() . "')";
  142. Database::query($sql);
  143. }
  144. $sql = "SELECT COUNT(user_id) as nbUsers " .
  145. "FROM $tbl_session_rel_user " .
  146. "WHERE session_id='$id_session' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."";
  147. $rs = Database::query($sql);
  148. list($nbr_users) = Database::fetch_array($rs);
  149. $sql = "UPDATE $tbl_session SET nbr_users=$nbr_users WHERE id='$id_session'";
  150. Database::query($sql);
  151. }
  152. }