ldap_users_list.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author Mustapha Alouani
  5. *
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. require '../auth/ldap/authldap.php';
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. api_protect_admin_script();
  13. $action = @$_GET["action"] ?: null;
  14. $login_as_user_id = @$_GET["user_id"] ?: null;
  15. // Login as ...
  16. if ($action == "login_as" && !empty($login_as_user_id)) {
  17. login_user($login_as_user_id);
  18. }
  19. //if we already have a session id and a user...
  20. /*
  21. if (($_GET['action']=="add_user") && ($_GET['id_session'] == strval(intval($_GET['id_session']))) && $_GET['id_session']>0 ){
  22. header('Location: ldap_import_students_to_session.php?id_session='.$_GET['id_session'].'&ldap_user='.$_GET['id']);
  23. }
  24. */
  25. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('Administration')];
  26. $tool_name = get_lang('Search for LDAP users');
  27. //Display::display_header($tool_name); //cannot display now as we need to redirect
  28. //api_display_tool_title($tool_name);
  29. if (isset($_GET['action'])) {
  30. $check = Security::check_token('get');
  31. if ($check) {
  32. switch ($_GET['action']) {
  33. case 'show_message':
  34. Display::addFlash(Display::return_message($_GET['message'], 'normal'));
  35. Display::display_header($tool_name);
  36. break;
  37. case 'delete_user':
  38. if ($user_id != $_user['user_id'] && UserManager::delete_user($_GET['user_id'])) {
  39. Display::addFlash(Display::return_message(get_lang('The user has been deleted'), 'normal'));
  40. } else {
  41. Display::addFlash(Display::return_message(get_lang('You cannot delete this user'), 'error'));
  42. }
  43. Display::display_header($tool_name);
  44. break;
  45. case 'lock':
  46. $message = lock_unlock_user('lock', $_GET['user_id']);
  47. Display::addFlash(Display::return_message($message, 'normal'));
  48. Display::display_header($tool_name);
  49. break;
  50. case 'unlock':
  51. $message = lock_unlock_user('unlock', $_GET['user_id']);
  52. Display::addFlash(Display::return_message($message, 'normal'));
  53. Display::display_header($tool_name);
  54. break;
  55. case 'add_user':
  56. $id = $_GET['id'];
  57. $UserList = [];
  58. $userid_match_login = [];
  59. foreach ($id as $user_id) {
  60. $tmp = ldap_add_user($user_id);
  61. $UserList[] = $tmp;
  62. $userid_match_login[$tmp] = $user_id;
  63. }
  64. if (isset($_GET['id_session']) && ($_GET['id_session'] == strval(intval($_GET['id_session']))) && ($_GET['id_session'] > 0)) {
  65. ldap_add_user_to_session($UserList, $_GET['id_session']);
  66. header('Location: resume_session.php?id_session='.intval($_GET['id_session']));
  67. } else {
  68. if (count($userid_match_login) > 0) {
  69. $message = get_lang('LDAP users added or updated').':<br />';
  70. foreach ($userid_match_login as $user_id => $login) {
  71. $message .= '- '.$login.'<br />';
  72. }
  73. } else {
  74. $message = get_lang('No user added');
  75. }
  76. Display::addFlash(Display::return_message($message, 'normal', false));
  77. Display::display_header($tool_name);
  78. }
  79. break;
  80. default:
  81. Display::display_header($tool_name);
  82. }
  83. Security::clear_token();
  84. } else {
  85. Display::display_header($tool_name);
  86. }
  87. } else {
  88. Display::display_header($tool_name);
  89. }
  90. if (isset($_POST['action'])) {
  91. $check = Security::check_token('get');
  92. if ($check) {
  93. switch ($_POST['action']) {
  94. case 'delete':
  95. $number_of_selected_users = count($_POST['id']);
  96. $number_of_deleted_users = 0;
  97. foreach ($_POST['id'] as $index => $user_id) {
  98. if ($user_id != $_user['user_id']) {
  99. if (UserManager::delete_user($user_id)) {
  100. $number_of_deleted_users++;
  101. }
  102. }
  103. }
  104. if ($number_of_selected_users == $number_of_deleted_users) {
  105. echo Display::return_message(get_lang('Selected users deleted'), 'normal');
  106. } else {
  107. echo Display::return_message(get_lang('Some of the selected users have not been deleted. We recommend you confirm which, by using the advanced search.'), 'error');
  108. }
  109. break;
  110. case 'add_user':
  111. $number_of_selected_users = count($_POST['id']);
  112. $number_of_added_users = 0;
  113. $UserList = [];
  114. foreach ($_POST['id'] as $index => $user_id) {
  115. if ($user_id != $_user['user_id']) {
  116. $UserList[] = ldap_add_user($user_id);
  117. }
  118. }
  119. if (isset($_GET['id_session']) && (trim($_GET['id_session']) != "")) {
  120. addUserToSession($UserList, $_GET['id_session']);
  121. }
  122. if (count($UserList) > 0) {
  123. echo Display::return_message(
  124. count($UserList)." ".get_lang('LDAP users added')
  125. );
  126. } else {
  127. echo Display::return_message(get_lang('No user added'));
  128. }
  129. break;
  130. }
  131. Security::clear_token();
  132. }
  133. }
  134. $form = new FormValidator('advanced_search', 'get');
  135. $form->addText('keyword_username', get_lang('Login'), false);
  136. if (api_is_western_name_order()) {
  137. $form->addText('keyword_firstname', get_lang('First name'), false);
  138. $form->addText('keyword_lastname', get_lang('Last name'), false);
  139. } else {
  140. $form->addText('keyword_lastname', get_lang('Last name'), false);
  141. $form->addText('keyword_firstname', get_lang('First name'), false);
  142. }
  143. if (isset($_GET['id_session'])) {
  144. $form->addElement('hidden', 'id_session', $_GET['id_session']);
  145. }
  146. $type = [];
  147. $type["all"] = get_lang('All');
  148. $type["employee"] = get_lang('Trainer');
  149. $type["student"] = get_lang('Learner');
  150. $form->addElement('select', 'keyword_type', get_lang('Status'), $type);
  151. // Structure a rajouer ??
  152. $form->addElement('submit', 'submit', get_lang('Validate'));
  153. //$defaults['keyword_active'] = 1;
  154. //$defaults['keyword_inactive'] = 1;
  155. //$form->setDefaults($defaults);
  156. $form->display();
  157. $parameters['keyword_username'] = @$_GET['keyword_username'] ?: null;
  158. $parameters['keyword_firstname'] = @$_GET['keyword_firstname'] ?: null;
  159. $parameters['keyword_lastname'] = @$_GET['keyword_lastname'] ?: null;
  160. $parameters['keyword_email'] = @$_GET['keyword_email'] ?: null;
  161. if (isset($_GET['id_session'])) {
  162. $parameters['id_session'] = $_GET['id_session'];
  163. }
  164. // Create a sortable table with user-data
  165. $parameters['sec_token'] = Security::get_token();
  166. $table = new SortableTable(
  167. 'users',
  168. 'ldap_get_number_of_users',
  169. 'ldap_get_user_data',
  170. (api_is_western_name_order() xor api_sort_by_first_name()) ? 3 : 2
  171. );
  172. $table->set_additional_parameters($parameters);
  173. $table->set_header(0, '', false);
  174. $table->set_header(1, get_lang('Login'));
  175. if (api_is_western_name_order()) {
  176. $table->set_header(2, get_lang('First name'));
  177. $table->set_header(3, get_lang('Last name'));
  178. } else {
  179. $table->set_header(2, get_lang('Last name'));
  180. $table->set_header(3, get_lang('First name'));
  181. }
  182. $table->set_header(4, get_lang('e-mail'));
  183. $table->set_header(5, get_lang('Detail'));
  184. //$table->set_column_filter(5, 'email_filter');
  185. //$table->set_column_filter(5, 'active_filter');
  186. $table->set_column_filter(5, 'modify_filter');
  187. $table->set_form_actions(['add_user' => get_lang('Add LDAP users')]);
  188. $table->display();
  189. Display::display_footer();