PageRenderTime 25ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/user.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 391 lines | 323 code | 55 blank | 13 comment | 103 complexity | e8cc719001992a6ed9a44e4d25e7b28c MD5 | raw file
  1. <?php
  2. require_once('../config.php');
  3. require_once($CFG->libdir.'/adminlib.php');
  4. require_once($CFG->libdir.'/authlib.php');
  5. require_once($CFG->dirroot.'/user/filters/lib.php');
  6. $delete = optional_param('delete', 0, PARAM_INT);
  7. $confirm = optional_param('confirm', '', PARAM_ALPHANUM); //md5 confirmation hash
  8. $confirmuser = optional_param('confirmuser', 0, PARAM_INT);
  9. $sort = optional_param('sort', 'name', PARAM_ALPHANUM);
  10. $dir = optional_param('dir', 'ASC', PARAM_ALPHA);
  11. $page = optional_param('page', 0, PARAM_INT);
  12. $perpage = optional_param('perpage', 30, PARAM_INT); // how many per page
  13. $ru = optional_param('ru', '2', PARAM_INT); // show remote users
  14. $lu = optional_param('lu', '2', PARAM_INT); // show local users
  15. $acl = optional_param('acl', '0', PARAM_INT); // id of user to tweak mnet ACL (requires $access)
  16. $suspend = optional_param('suspend', 0, PARAM_INT);
  17. $unsuspend = optional_param('unsuspend', 0, PARAM_INT);
  18. $unlock = optional_param('unlock', 0, PARAM_INT);
  19. admin_externalpage_setup('editusers');
  20. $sitecontext = context_system::instance();
  21. $site = get_site();
  22. if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
  23. print_error('nopermissions', 'error', '', 'edit/delete users');
  24. }
  25. $stredit = get_string('edit');
  26. $strdelete = get_string('delete');
  27. $strdeletecheck = get_string('deletecheck');
  28. $strshowallusers = get_string('showallusers');
  29. $strsuspend = get_string('suspenduser', 'admin');
  30. $strunsuspend = get_string('unsuspenduser', 'admin');
  31. $strunlock = get_string('unlockaccount', 'admin');
  32. $strconfirm = get_string('confirm');
  33. if (empty($CFG->loginhttps)) {
  34. $securewwwroot = $CFG->wwwroot;
  35. } else {
  36. $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
  37. }
  38. $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
  39. if ($confirmuser and confirm_sesskey()) {
  40. require_capability('moodle/user:update', $sitecontext);
  41. if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  42. print_error('nousers');
  43. }
  44. $auth = get_auth_plugin($user->auth);
  45. $result = $auth->user_confirm($user->username, $user->secret);
  46. if ($result == AUTH_CONFIRM_OK or $result == AUTH_CONFIRM_ALREADY) {
  47. redirect($returnurl);
  48. } else {
  49. echo $OUTPUT->header();
  50. redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
  51. }
  52. } else if ($delete and confirm_sesskey()) { // Delete a selected user, after confirmation
  53. require_capability('moodle/user:delete', $sitecontext);
  54. $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
  55. if (is_siteadmin($user->id)) {
  56. print_error('useradminodelete', 'error');
  57. }
  58. if ($confirm != md5($delete)) {
  59. echo $OUTPUT->header();
  60. $fullname = fullname($user, true);
  61. echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
  62. $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
  63. echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), new moodle_url($returnurl, $optionsyes), $returnurl);
  64. echo $OUTPUT->footer();
  65. die;
  66. } else if (data_submitted() and !$user->deleted) {
  67. if (delete_user($user)) {
  68. session_gc(); // remove stale sessions
  69. redirect($returnurl);
  70. } else {
  71. session_gc(); // remove stale sessions
  72. echo $OUTPUT->header();
  73. echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
  74. }
  75. }
  76. } else if ($acl and confirm_sesskey()) {
  77. if (!has_capability('moodle/user:update', $sitecontext)) {
  78. print_error('nopermissions', 'error', '', 'modify the NMET access control list');
  79. }
  80. if (!$user = $DB->get_record('user', array('id'=>$acl))) {
  81. print_error('nousers', 'error');
  82. }
  83. if (!is_mnet_remote_user($user)) {
  84. print_error('usermustbemnet', 'error');
  85. }
  86. $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
  87. if ($accessctrl != 'allow' and $accessctrl != 'deny') {
  88. print_error('invalidaccessparameter', 'error');
  89. }
  90. $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid));
  91. if (empty($aclrecord)) {
  92. $aclrecord = new stdClass();
  93. $aclrecord->mnet_host_id = $user->mnethostid;
  94. $aclrecord->username = $user->username;
  95. $aclrecord->accessctrl = $accessctrl;
  96. $DB->insert_record('mnet_sso_access_control', $aclrecord);
  97. } else {
  98. $aclrecord->accessctrl = $accessctrl;
  99. $DB->update_record('mnet_sso_access_control', $aclrecord);
  100. }
  101. $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
  102. redirect($returnurl);
  103. } else if ($suspend and confirm_sesskey()) {
  104. require_capability('moodle/user:update', $sitecontext);
  105. if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
  106. if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
  107. $user->suspended = 1;
  108. $user->timemodified = time();
  109. $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
  110. $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
  111. // force logout
  112. session_kill_user($user->id);
  113. events_trigger('user_updated', $user);
  114. }
  115. }
  116. redirect($returnurl);
  117. } else if ($unsuspend and confirm_sesskey()) {
  118. require_capability('moodle/user:update', $sitecontext);
  119. if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
  120. if ($user->suspended != 0) {
  121. $user->suspended = 0;
  122. $user->timemodified = time();
  123. $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
  124. $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
  125. events_trigger('user_updated', $user);
  126. }
  127. }
  128. redirect($returnurl);
  129. } else if ($unlock and confirm_sesskey()) {
  130. require_capability('moodle/user:update', $sitecontext);
  131. if ($user = $DB->get_record('user', array('id'=>$unlock, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
  132. login_unlock_account($user);
  133. }
  134. redirect($returnurl);
  135. }
  136. // create the user filter form
  137. $ufiltering = new user_filtering();
  138. echo $OUTPUT->header();
  139. // Carry on with the user listing
  140. $context = context_system::instance();
  141. $extracolumns = get_extra_user_fields($context);
  142. $columns = array_merge(array('firstname', 'lastname'), $extracolumns,
  143. array('city', 'country', 'lastaccess'));
  144. foreach ($columns as $column) {
  145. $string[$column] = get_user_field_name($column);
  146. if ($sort != $column) {
  147. $columnicon = "";
  148. if ($column == "lastaccess") {
  149. $columndir = "DESC";
  150. } else {
  151. $columndir = "ASC";
  152. }
  153. } else {
  154. $columndir = $dir == "ASC" ? "DESC":"ASC";
  155. if ($column == "lastaccess") {
  156. $columnicon = ($dir == "ASC") ? "sort_desc" : "sort_asc";
  157. } else {
  158. $columnicon = ($dir == "ASC") ? "sort_asc" : "sort_desc";
  159. }
  160. $columnicon = "<img class='iconsort' src=\"" . $OUTPUT->pix_url('t/' . $columnicon) . "\" alt=\"\" />";
  161. }
  162. $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
  163. }
  164. $override = new stdClass();
  165. $override->firstname = 'firstname';
  166. $override->lastname = 'lastname';
  167. $fullnamelanguage = get_string('fullnamedisplay', '', $override);
  168. if (($CFG->fullnamedisplay == 'firstname lastname') or
  169. ($CFG->fullnamedisplay == 'firstname') or
  170. ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
  171. $fullnamedisplay = "$firstname / $lastname";
  172. if ($sort == "name") { // If sort has already been set to something else then ignore.
  173. $sort = "firstname";
  174. }
  175. } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname').
  176. $fullnamedisplay = "$lastname / $firstname";
  177. if ($sort == "name") { // This should give the desired sorting based on fullnamedisplay.
  178. $sort = "lastname";
  179. }
  180. }
  181. list($extrasql, $params) = $ufiltering->get_sql_filter();
  182. $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
  183. $extrasql, $params, $context);
  184. $usercount = get_users(false);
  185. $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
  186. if ($extrasql !== '') {
  187. echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
  188. $usercount = $usersearchcount;
  189. } else {
  190. echo $OUTPUT->heading("$usercount ".get_string('users'));
  191. }
  192. $strall = get_string('all');
  193. $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
  194. echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
  195. flush();
  196. if (!$users) {
  197. $match = array();
  198. echo $OUTPUT->heading(get_string('nousersfound'));
  199. $table = NULL;
  200. } else {
  201. $countries = get_string_manager()->get_list_of_countries(false);
  202. if (empty($mnethosts)) {
  203. $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
  204. }
  205. foreach ($users as $key => $user) {
  206. if (isset($countries[$user->country])) {
  207. $users[$key]->country = $countries[$user->country];
  208. }
  209. }
  210. if ($sort == "country") { // Need to resort by full country name, not code
  211. foreach ($users as $user) {
  212. $susers[$user->id] = $user->country;
  213. }
  214. asort($susers);
  215. foreach ($susers as $key => $value) {
  216. $nusers[] = $users[$key];
  217. }
  218. $users = $nusers;
  219. }
  220. $table = new html_table();
  221. $table->head = array ();
  222. $table->colclasses = array();
  223. $table->head[] = $fullnamedisplay;
  224. $table->attributes['class'] = 'admintable generaltable';
  225. $table->colclasses[] = 'leftalign';
  226. foreach ($extracolumns as $field) {
  227. $table->head[] = ${$field};
  228. $table->colclasses[] = 'leftalign';
  229. }
  230. $table->head[] = $city;
  231. $table->colclasses[] = 'leftalign';
  232. $table->head[] = $country;
  233. $table->colclasses[] = 'leftalign';
  234. $table->head[] = $lastaccess;
  235. $table->colclasses[] = 'leftalign';
  236. $table->head[] = get_string('edit');
  237. $table->colclasses[] = 'centeralign';
  238. $table->head[] = "";
  239. $table->colclasses[] = 'centeralign';
  240. $table->id = "users";
  241. foreach ($users as $user) {
  242. $buttons = array();
  243. $lastcolumn = '';
  244. // delete button
  245. if (has_capability('moodle/user:delete', $sitecontext)) {
  246. if (is_mnet_remote_user($user) or $user->id == $USER->id or is_siteadmin($user)) {
  247. // no deleting of self, mnet accounts or admins allowed
  248. } else {
  249. $buttons[] = html_writer::link(new moodle_url($returnurl, array('delete'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>$strdelete, 'class'=>'iconsmall')), array('title'=>$strdelete));
  250. }
  251. }
  252. // suspend button
  253. if (has_capability('moodle/user:update', $sitecontext)) {
  254. if (is_mnet_remote_user($user)) {
  255. // mnet users have special access control, they can not be deleted the standard way or suspended
  256. $accessctrl = 'allow';
  257. if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
  258. $accessctrl = $acl->accessctrl;
  259. }
  260. $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
  261. $buttons[] = " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
  262. } else {
  263. if ($user->suspended) {
  264. $buttons[] = html_writer::link(new moodle_url($returnurl, array('unsuspend'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>$strunsuspend, 'class'=>'iconsmall')), array('title'=>$strunsuspend));
  265. } else {
  266. if ($user->id == $USER->id or is_siteadmin($user)) {
  267. // no suspending of admins or self!
  268. } else {
  269. $buttons[] = html_writer::link(new moodle_url($returnurl, array('suspend'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/hide'), 'alt'=>$strsuspend, 'class'=>'iconsmall')), array('title'=>$strsuspend));
  270. }
  271. }
  272. if (login_is_lockedout($user)) {
  273. $buttons[] = html_writer::link(new moodle_url($returnurl, array('unlock'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/unlock'), 'alt'=>$strunlock, 'class'=>'iconsmall')), array('title'=>$strunlock));
  274. }
  275. }
  276. }
  277. // edit button
  278. if (has_capability('moodle/user:update', $sitecontext)) {
  279. // prevent editing of admins by non-admins
  280. if (is_siteadmin($USER) or !is_siteadmin($user)) {
  281. $buttons[] = html_writer::link(new moodle_url($securewwwroot.'/user/editadvanced.php', array('id'=>$user->id, 'course'=>$site->id)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/edit'), 'alt'=>$stredit, 'class'=>'iconsmall')), array('title'=>$stredit));
  282. }
  283. }
  284. // the last column - confirm or mnet info
  285. if (is_mnet_remote_user($user)) {
  286. // all mnet users are confirmed, let's print just the name of the host there
  287. if (isset($mnethosts[$user->mnethostid])) {
  288. $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
  289. } else {
  290. $lastcolumn = get_string($accessctrl, 'mnet');
  291. }
  292. } else if ($user->confirmed == 0) {
  293. if (has_capability('moodle/user:update', $sitecontext)) {
  294. $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
  295. } else {
  296. $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
  297. }
  298. }
  299. if ($user->lastaccess) {
  300. $strlastaccess = format_time(time() - $user->lastaccess);
  301. } else {
  302. $strlastaccess = get_string('never');
  303. }
  304. $fullname = fullname($user, true);
  305. $row = array ();
  306. $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
  307. foreach ($extracolumns as $field) {
  308. $row[] = $user->{$field};
  309. }
  310. $row[] = $user->city;
  311. $row[] = $user->country;
  312. $row[] = $strlastaccess;
  313. if ($user->suspended) {
  314. foreach ($row as $k=>$v) {
  315. $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
  316. }
  317. }
  318. $row[] = implode(' ', $buttons);
  319. $row[] = $lastcolumn;
  320. $table->data[] = $row;
  321. }
  322. }
  323. // add filters
  324. $ufiltering->display_add();
  325. $ufiltering->display_active();
  326. if (has_capability('moodle/user:create', $sitecontext)) {
  327. echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
  328. }
  329. if (!empty($table)) {
  330. echo html_writer::table($table);
  331. echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
  332. if (has_capability('moodle/user:create', $sitecontext)) {
  333. echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
  334. }
  335. }
  336. echo $OUTPUT->footer();