-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeadmin.php
More file actions
115 lines (88 loc) · 3.32 KB
/
Copy pathmakeadmin.php
File metadata and controls
115 lines (88 loc) · 3.32 KB
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
<?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/>.
/**
* @package cli_makeadmin
* @copyright 2026 Onwards LSU Online & Continuing Education
* @author Robert Russo
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Make sure we cannot execute this on the web.
define('CLI_SCRIPT', true);
// We need config.
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/clilib.php');
// We need DB access.
global $DB;
// Build out allowable options.
list($options, $unrecognized) = cli_get_params(
['help' => false],
['h' => 'help']
);
// If someone asks for help.
if ($options['help']) {
// Build the help.
$help = "Moodle CLI Admin Promotion
A simple CLI utility for promoting an existing Moodle user to a Site Administrator.
Usage:
php makeadmin.php <username>
Options:
-h, --help Print out this help
Example:
php makeadmin.php jsmith
";
// Write out the help and exit.
cli_writeln($help);
exit(0);
}
// If we have no parms, let the user know how to use the tool.
if (count($unrecognized) !== 1) {
cli_error("Usage: php makeadmin.php <username>
Try 'php makeadmin.php --help' for more information.", 1);
}
// Get the script parm and assign it to username.
$username = $unrecognized[0];
try {
// Get the user object.
$user = $DB->get_record('user', ['username' => $username, 'deleted' => 0], '*', MUST_EXIST);
// Make sure the user is not suspended.
if ($user->suspended) {
cli_error("Error: User '{$username}' is suspended.", 3);
}
// Make sure the user is not the guest user.
if (isguestuser($user)) {
cli_error("Error: Cannot make the guest user an administrator.", 4);
}
// Get a list of site admins.
$admins = explode(',', (string)get_config('moodle', 'siteadmins'));
// Check to see if the supplied user is already an admin, if not then do stuff.
if (!in_array($user->id, $admins)) {
// Add the user to the list of admin userids.
$admins[] = $user->id;
// No dupes please.
$admins = array_filter(array_unique($admins));
// Write the config out as a comma separated value.
set_config('siteadmins', implode(',', $admins));
// Let the user know we did what they wanted.
cli_writeln("User '{$username}' (ID {$user->id}) is now a site administrator.");
// The supplied user is already a site admin.
} else {
// Let the user know they might be special.
cli_writeln("User '{$username}' is already a site administrator.");
}
// We do not have a matching username. Let the user know.
} catch (dml_missing_record_exception $e) {
cli_error("Error: User '{$username}' not found.", 2);
}