-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser.php
549 lines (490 loc) · 23 KB
/
user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Moodle Form for purgeusers action
* @package tool_ldapsync
* @copyright Copyright (c) 2024, UCSF Education IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/authlib.php');
require_once($CFG->dirroot . '/user/filters/lib.php');
require_once($CFG->dirroot . '/user/lib.php');
//
// Copied from user_bulk_action_form() in admin/user/user_bulk/user_bulk_forms.php.
/**
* Create a form that is similar to user bulk form to filter users for purging
* @package tool_ldapsync
*/
class ldapsync_purgeusers_action_form extends moodleform {
/**
* Function definition for moodleform.
*/
private function definition() {
global $CFG;
$mform =& $this->_form;
$syscontext = context_system::instance();
$actions = [0 => get_string('choose') . '...'];
if (has_capability('moodle/user:delete', $syscontext)) {
$actions[3] = get_string('delete');
}
if (has_capability('moodle/user:update', $syscontext)) {
$actions[5] = get_string('download', 'admin');
}
$objs = [];
$objs[] =& $mform->createElement('select', 'action', null, $actions);
$objs[] =& $mform->createElement('submit', 'doaction', get_string('go'));
$mform->addElement('group', 'actionsgrp', get_string('withselectedusers'), $objs, ' ', false);
}
}
$delete = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', '', PARAM_ALPHANUM); // This is an md5 confirmation hash.
$confirmuser = optional_param('confirmuser', 0, PARAM_INT);
$sort = optional_param('sort', 'name', PARAM_ALPHANUM);
$dir = optional_param('dir', 'ASC', PARAM_ALPHA);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 30, PARAM_INT); // How many per page.
$ru = optional_param('ru', '2', PARAM_INT); // Show remote users.
$lu = optional_param('lu', '2', PARAM_INT); // Show local users.
$acl = optional_param('acl', '0', PARAM_INT); // Id of user to tweak mnet ACL (requires $access).
$suspend = optional_param('suspend', 0, PARAM_INT);
$unsuspend = optional_param('unsuspend', 0, PARAM_INT);
$unlock = optional_param('unlock', 0, PARAM_INT);
$resendemail = optional_param('resendemail', 0, PARAM_INT);
admin_externalpage_setup('ldapsync_purgeusers');
$sitecontext = context_system::instance();
$site = get_site();
if (!has_capability('moodle/user:update', $sitecontext) && !has_capability('moodle/user:delete', $sitecontext)) {
throw new \moodle_exception('nopermissions', 'error', '', 'edit/delete users');
}
$stredit = get_string('edit');
$strdelete = get_string('delete');
$strdeletecheck = get_string('deletecheck');
$strshowallusers = get_string('showallusers');
$strsuspend = get_string('suspenduser', 'admin');
$strunsuspend = get_string('unsuspenduser', 'admin');
$strunlock = get_string('unlockaccount', 'admin');
$strconfirm = get_string('confirm');
$strresendemail = get_string('resendemail');
$returnurl = new moodle_url(
'/admin/tool/ldapsync/user.php',
['sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page' => $page]
);
// Create the user filter form.
$fieldnames = ['realname' => 1, 'lastname' => 1, 'firstname' => 1, 'username' => 1, 'email' => 1, 'city' => 1,
'country' => 1, 'confirmed' => 1, 'suspended' => 1, 'profile' => 1, 'courserole' => 1,
'anycourses' => 0, 'systemrole' => 1, 'cohort' => 1, 'firstaccess' => 1, 'lastaccess' => 1,
'neveraccessed' => 0, 'timecreated' => 1, 'timemodified' => 1, 'nevermodified' => 1, 'auth' => 0,
'mnethostid' => 1, 'idnumber' => 1, 'activeonldap' => 0, 'additionalldapfilter' => 1];
$ufiltering = new \tool_ldapsync\user_filtering($fieldnames);
// Create the bulk operations form.
$actionform = new ldapsync_purgeusers_action_form();
if ($data = $actionform->get_data()) {
$SESSION->ufiltering = serialize($ufiltering);
// Check if an action should be performed and do so.
switch ($data->action) {
case 1:
redirect($CFG->wwwroot . '/' . $CFG->admin . '/user/user_bulk_confirm.php');
break;
case 2:
redirect($CFG->wwwroot . '/' . $CFG->admin . '/user/user_bulk_message.php');
break;
case 3:
redirect($CFG->wwwroot . '/' . $CFG->admin . '/tool/ldapsync/user_bulk_delete.php');
break;
case 4:
redirect($CFG->wwwroot . '/' . $CFG->admin . '/tool/ldapsync/user_bulk_display.php');
break;
case 5:
redirect($CFG->wwwroot . '/' . $CFG->admin . '/tool/ldapsync/user_bulk_download.php');
break;
case 7:
redirect($CFG->wwwroot . '/' . $CFG->admin . '/user/user_bulk_forcepasswordchange.php');
break;
case 8:
redirect($CFG->wwwroot . '/' . $CFG->admin . '/user/user_bulk_cohortadd.php');
break;
}
}
// The $user variable is also used outside of these if statements.
$user = null;
if ($confirmuser && confirm_sesskey()) {
require_capability('moodle/user:update', $sitecontext);
if (!$user = $DB->get_record('user', ['id' => $confirmuser, 'mnethostid' => $CFG->mnet_localhost_id])) {
throw new \moodle_exception('nousers');
}
$auth = get_auth_plugin($user->auth);
$result = $auth->user_confirm($user->username, $user->secret);
if ($result == AUTH_CONFIRM_OK || $result == AUTH_CONFIRM_ALREADY) {
redirect($returnurl);
} else {
echo $OUTPUT->header();
redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
}
} else if ($resendemail && confirm_sesskey()) {
if (!$user = $DB->get_record('user', ['id' => $resendemail, 'mnethostid' => $CFG->mnet_localhost_id, 'deleted' => 0])) {
throw new \moodle_exception('nousers');
}
// Prevent spamming users who are already confirmed.
if ($user->confirmed) {
throw new \moodle_exception('alreadyconfirmed');
}
$returnmsg = get_string('emailconfirmsentsuccess');
$messagetype = \core\output\notification::NOTIFY_SUCCESS;
if (!send_confirmation_email($user)) {
$returnmsg = get_string('emailconfirmsentfailure');
$messagetype = \core\output\notification::NOTIFY_ERROR;
}
redirect($returnurl, $returnmsg, null, $messagetype);
} else if ($delete && confirm_sesskey()) { // Delete a selected user, after confirmation.
require_capability('moodle/user:delete', $sitecontext);
$user = $DB->get_record('user', ['id' => $delete, 'mnethostid' => $CFG->mnet_localhost_id], '*', MUST_EXIST);
if ($user->deleted) {
throw new \moodle_exception('usernotdeleteddeleted', 'error');
}
if (is_siteadmin($user->id)) {
throw new \moodle_exception('useradminodelete', 'error');
}
if ($confirm != md5($delete)) {
echo $OUTPUT->header();
$fullname = fullname($user, true);
echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
$optionsyes = ['delete' => $delete, 'confirm' => md5($delete), 'sesskey' => sesskey()];
$deleteurl = new moodle_url($returnurl, $optionsyes);
$deletebutton = new single_button($deleteurl, get_string('delete'), 'post');
echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), $deletebutton, $returnurl);
echo $OUTPUT->footer();
die;
} else if (data_submitted()) {
if (delete_user($user)) {
\core\session\manager::gc(); // Remove stale sessions.
redirect($returnurl);
} else {
\core\session\manager::gc(); // Remove stale sessions.
echo $OUTPUT->header();
echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
}
}
} else if ($acl && confirm_sesskey()) {
if (!has_capability('moodle/user:update', $sitecontext)) {
throw new \moodle_exception('nopermissions', 'error', '', 'modify the NMET access control list');
}
if (!$user = $DB->get_record('user', ['id' => $acl])) {
throw new \moodle_exception('nousers', 'error');
}
if (!is_mnet_remote_user($user)) {
throw new \moodle_exception('usermustbemnet', 'error');
}
$accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
if ($accessctrl != 'allow' && $accessctrl != 'deny') {
throw new \moodle_exception('invalidaccessparameter', 'error');
}
$aclrecord = $DB->get_record(
'mnet_sso_access_control',
['username' => $user->username, 'mnet_host_id' => $user->mnethostid]
);
if (empty($aclrecord)) {
$aclrecord = new stdClass();
$aclrecord->mnet_host_id = $user->mnethostid;
$aclrecord->username = $user->username;
$aclrecord->accessctrl = $accessctrl;
$DB->insert_record('mnet_sso_access_control', $aclrecord);
} else {
$aclrecord->accessctrl = $accessctrl;
$DB->update_record('mnet_sso_access_control', $aclrecord);
}
$mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
redirect($returnurl);
} else if ($suspend && confirm_sesskey()) {
require_capability('moodle/user:update', $sitecontext);
if ($user = $DB->get_record('user', ['id' => $suspend, 'mnethostid' => $CFG->mnet_localhost_id, 'deleted' => 0])) {
if (!is_siteadmin($user) && $USER->id != $user->id && $user->suspended != 1) {
$user->suspended = 1;
// Force logout.
\core\session\manager::kill_user_sessions($user->id);
user_update_user($user, false);
}
}
redirect($returnurl);
} else if ($unsuspend && confirm_sesskey()) {
require_capability('moodle/user:update', $sitecontext);
if ($user = $DB->get_record('user', ['id' => $unsuspend, 'mnethostid' => $CFG->mnet_localhost_id, 'deleted' => 0])) {
if ($user->suspended != 0) {
$user->suspended = 0;
user_update_user($user, false);
}
}
redirect($returnurl);
} else if ($unlock && confirm_sesskey()) {
require_capability('moodle/user:update', $sitecontext);
if ($user = $DB->get_record('user', ['id' => $unlock, 'mnethostid' => $CFG->mnet_localhost_id, 'deleted' => 0])) {
login_unlock_account($user);
}
redirect($returnurl);
}
echo $OUTPUT->header();
// Carry on with the user listing.
$context = context_system::instance();
// These columns are always shown in the users list.
$requiredcolumns = ['city', 'country', 'lastaccess'];
// Extra columns containing the extra user fields, excluding the required columns (city and country, to be specific).
$extracolumns = get_extra_user_fields($context, $requiredcolumns);
// Get all user name fields as an array.
$allusernamefields = get_all_user_name_fields(false, null, null, null, true);
$columns = array_merge($allusernamefields, $extracolumns, $requiredcolumns);
foreach ($columns as $column) {
$string[$column] = get_user_field_name($column);
if ($sort != $column) {
$columnicon = "";
if ($column == "lastaccess") {
$columndir = "DESC";
} else {
$columndir = "ASC";
}
} else {
$columndir = $dir == "ASC" ? "DESC" : "ASC";
if ($column == "lastaccess") {
$columnicon = ($dir == "ASC") ? "sort_desc" : "sort_asc";
} else {
$columnicon = ($dir == "ASC") ? "sort_asc" : "sort_desc";
}
$columnicon = $OUTPUT->pix_icon(
't/' . $columnicon,
get_string(strtolower($columndir)),
'core',
['class' => 'iconsort']
);
}
$$column = "<a href=\"user.php?sort=$column&dir=$columndir\">" . $string[$column] . "</a>$columnicon";
}
// We need to check that alternativefullnameformat is not set to '' or language.
// We don't need to check the fullnamedisplay setting here as the fullname function call further down has
// the override parameter set to true.
$fullnamesetting = $CFG->alternativefullnameformat;
// If we are using language or it is empty, then retrieve the default user names of just 'firstname' and 'lastname'.
if ($fullnamesetting == 'language' || empty($fullnamesetting)) {
// Set $a variables to return 'firstname' and 'lastname'.
$a = new stdClass();
$a->firstname = 'firstname';
$a->lastname = 'lastname';
// Getting the fullname display will ensure that the order in the language file is maintained.
$fullnamesetting = get_string('fullnamedisplay', null, $a);
}
// Order in string will ensure that the name columns are in the correct order.
$usernames = order_in_string($allusernamefields, $fullnamesetting);
$fullnamedisplay = [];
foreach ($usernames as $name) {
// Use the link from $$column for sorting on the user's name.
$fullnamedisplay[] = ${$name};
}
// All of the names are in one column. Put them into a string and separate them with a /.
$fullnamedisplay = implode(' / ', $fullnamedisplay);
// If $sort = name then it is the default for the setting and we should use the first name to sort by.
if ($sort == "name") {
// Use the first item in the array.
$sort = reset($usernames);
}
[$extrasql, $params] = $ufiltering->get_sql_filter();
$users = get_users_listing(
$sort,
$dir,
$page * $perpage,
$perpage,
'',
'',
'',
$extrasql,
$params,
$context
);
$usercount = get_users(false);
$usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
if ($extrasql !== '') {
echo $OUTPUT->heading("$usersearchcount / $usercount " . get_string('users'));
$usercount = $usersearchcount;
} else {
echo $OUTPUT->heading("$usercount " . get_string('users'));
}
$strall = get_string('all');
$baseurl = new moodle_url('/admin/tool/ldapsync/user.php', ['sort' => $sort, 'dir' => $dir, 'perpage' => $perpage]);
echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
flush();
if (!$users) {
$match = [];
echo $OUTPUT->heading(get_string('nousersfound'));
$table = null;
} else {
$countries = get_string_manager()->get_list_of_countries(true);
if (empty($mnethosts)) {
$mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
}
foreach ($users as $key => $user) {
if (isset($countries[$user->country])) {
$users[$key]->country = $countries[$user->country];
}
}
if ($sort == "country") {
// Need to resort by full country name, not code.
foreach ($users as $user) {
$susers[$user->id] = $user->country;
}
// Sort by country name, according to $dir.
if ($dir === 'DESC') {
arsort($susers);
} else {
asort($susers);
}
foreach ($susers as $key => $value) {
$nusers[] = $users[$key];
}
$users = $nusers;
}
$table = new html_table();
$table->head = [];
$table->colclasses = [];
$table->head[] = $fullnamedisplay;
$table->attributes['class'] = 'admintable generaltable';
foreach ($extracolumns as $field) {
$table->head[] = ${$field};
}
$table->head[] = $city;
$table->head[] = $country;
$table->head[] = $lastaccess;
$table->head[] = get_string('edit');
$table->colclasses[] = 'centeralign';
$table->head[] = "";
$table->colclasses[] = 'centeralign';
$table->id = "users";
foreach ($users as $user) {
$buttons = [];
$lastcolumn = '';
// Delete button.
if (has_capability('moodle/user:delete', $sitecontext)) {
// No deleting of self, mnet accounts or admins allowed.
if (
($user->id != $USER->id) &&
(!is_mnet_remote_user($user)) &&
(!is_siteadmin($user))
) {
$url = new moodle_url($returnurl, ['delete' => $user->id, 'sesskey' => sesskey()]);
$buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/delete', $strdelete));
}
}
// Suspend button.
if (has_capability('moodle/user:update', $sitecontext)) {
if (is_mnet_remote_user($user)) {
// The mnet users have special access control, they can not be deleted the standard way or suspended.
$accessctrl = 'allow';
if (
$acl = $DB->get_record(
'mnet_sso_access_control',
['username' => $user->username, 'mnet_host_id' => $user->mnethostid]
)
) {
$accessctrl = $acl->accessctrl;
}
$changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
$buttons[] = " (<a href=\"?acl={$user->id}&accessctrl=$changeaccessto&sesskey="
. sesskey() . "\">" . get_string($changeaccessto, 'mnet') . " access</a>)";
} else {
if ($user->suspended) {
$url = new moodle_url($returnurl, ['unsuspend' => $user->id, 'sesskey' => sesskey()]);
$buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/show', $strunsuspend));
} else {
// No suspending of admins or self!
if ($user->id != $USER->id && !is_siteadmin($user)) {
$url = new moodle_url($returnurl, ['suspend' => $user->id, 'sesskey' => sesskey()]);
$buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/hide', $strsuspend));
}
}
if (login_is_lockedout($user)) {
$url = new moodle_url($returnurl, ['unlock' => $user->id, 'sesskey' => sesskey()]);
$buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/unlock', $strunlock));
}
}
}
// Edit button.
if (has_capability('moodle/user:update', $sitecontext)) {
// Prevent editing of admins by non-admins.
if (is_siteadmin($USER) || !is_siteadmin($user)) {
$url = new moodle_url('/user/editadvanced.php', ['id' => $user->id, 'course' => $site->id]);
$buttons[] = html_writer::link($url, $OUTPUT->pix_icon('t/edit', $stredit));
}
}
// The last column - confirm or mnet info.
if (is_mnet_remote_user($user)) {
// All mnet users are confirmed, let's print just the name of the host there.
if (isset($mnethosts[$user->mnethostid])) {
$lastcolumn = get_string($accessctrl, 'mnet') . ': ' . $mnethosts[$user->mnethostid]->name;
} else {
$lastcolumn = get_string($accessctrl, 'mnet');
}
} else if ($user->confirmed == 0) {
if (has_capability('moodle/user:update', $sitecontext)) {
$lastcolumn = html_writer::link(
new moodle_url(
$returnurl,
['confirmuser' => $user->id, 'sesskey' => sesskey()]
),
$strconfirm
);
} else {
$lastcolumn = "<span class=\"dimmed_text\">" . get_string('confirm') . "</span>";
}
$lastcolumn .= ' | ' . html_writer::link(new moodle_url(
$returnurl,
[
'resendemail' => $user->id,
'sesskey' => sesskey(),
]
), $strresendemail);
}
if ($user->lastaccess) {
$strlastaccess = format_time(time() - $user->lastaccess);
} else {
$strlastaccess = get_string('never');
}
$fullname = fullname($user, true);
$row = [];
$row[] = "<a href=\"../../../user/view.php?id=$user->id&course=$site->id\">$fullname</a>";
foreach ($extracolumns as $field) {
$row[] = s($user->{$field});
}
$row[] = $user->city;
$row[] = $user->country;
$row[] = $strlastaccess;
if ($user->suspended) {
foreach ($row as $k => $v) {
$row[$k] = html_writer::tag('span', $v, ['class' => 'usersuspended']);
}
}
$row[] = implode(' ', $buttons);
$row[] = $lastcolumn;
$table->data[] = $row;
}
}
// Add filters.
$ufiltering->display_add();
$ufiltering->display_active();
if (!empty($table)) {
echo html_writer::start_tag('div', ['class' => 'no-overflow']);
echo html_writer::table($table);
echo html_writer::end_tag('div');
echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
}
$actionform->display();
echo $OUTPUT->footer();