-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
238 lines (209 loc) · 6.73 KB
/
lib.php
File metadata and controls
238 lines (209 loc) · 6.73 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
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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Lib file
*
* @package local_lmslight
* @copyright 2024 LMSLight
* @author Bryce Yoder
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Hook to remove unwanted settings for non-lmslight users
* This runs pretty early so we do a bunch of other stuff here too
*/
function local_lmslight_extend_settings_navigation($nav, $context) {
global $PAGE;
// Display disabled page if need be
check_site_disabled();
// Platform staff see the full admin tree — no pruning.
if (local_lmslight_is_platform_user()) {
return;
}
// Hide SSO auth plugins from the manageauth table for base-tier sites.
if (local_lmslight_get_subscription_level() < 1) {
$PAGE->requires->js_call_amd('local_lmslight/main', 'init');
}
foreach (local_lmslight_get_pruned_keys() as $setting) {
manipulate_setting($nav, $setting);
manipulate_admin_setting($setting);
}
}
/**
* Remove settings from admin tree
* @param settingname: The name of the setting to remove
*/
function manipulate_admin_setting($settingname) {
if (!function_exists('admin_get_root')) {
return;
}
$adminnav = admin_get_root();
$adminnav->prune($settingname);
}
/**
* Remove settings from page nav
* @param nav: The nav object from which to remove settings
* @param settingname: The name of the setting or category to remove
*/
function manipulate_setting($nav, $settingname) {
// if the setting is in the nav, recursively call this function on any children
if ($setting = $nav->find($settingname, global_navigation::TYPE_SETTING)) {
foreach ($setting->get_children_key_list() as $child_setting) {
manipulate_setting($nav, $child_setting);
}
// redirect the page if we're actually on this setting
if ($setting->action() !== null && str_contains(
$_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'],
$setting->action()->out_as_local_url()
)) {
redirect('/');
}
// remove it from the tree
$setting->remove();
}
}
/**
* Given a user object, login the current browser session as said user
* @param array user: The user objet
*/
function login_as_user($user) {
$auth = empty($user->auth) ? 'manual' : $user->auth;
if ($auth == 'nologin' or !is_enabled_auth($auth)) {
cli_error(sprintf("User authentication is either 'nologin' or disabled. Check Moodle authentication method for '%s'", $user->username));
}
$authplugin = get_auth_plugin($auth);
$authplugin->sync_roles($user);
login_attempt_valid($user);
complete_user_login($user);
}
/**
* Given an OAuth response from Google, create a new user
* @param array oauth_response: The returned OAuth object from Google
* @return array: The user object of the created user
*/
function create_lmslight_user($oauth_response) {
global $CFG;
require_once('../../user/lib.php');
$user = new \stdClass();
$user->email = $oauth_response['email'];
$user->maildigest = 0;
$user->city = 'Noblesville';
$user->country = 'US';
$user->firstname = $oauth_response['given_name'];
$user->lastname = $oauth_response['family_name'];
$user->timecreated = time();
$user->timemodified = $user->timecreated;
$user->username = $oauth_response['email'];
$user->confirmed = 1;
$user->mnethostid = $CFG->mnet_localhost_id;
return user_create_user($user);
}
/**
* Given a user ID, make the user a site administrator
* @param int userid: The ID of the user to make an admin
*/
function make_user_admin($userid) {
global $DB, $CFG;
$siteadmins = explode(',', $CFG->siteadmins);
array_push($siteadmins, $userid);
$siteadmins = implode(',', $siteadmins);
set_config('siteadmins', $siteadmins);
}
function check_site_disabled() {
global $CFG;
if (property_exists($CFG, 'sitedisabled') && $CFG->sitedisabled) {
require_once('disabled.php');
}
}
/**
* Check whether the current user is an LMS Light platform staff member.
*
* Platform users have @lmslight.io email addresses and see the full,
* unpruned admin tree.
*
* @return bool True if the current user is platform staff.
*/
function local_lmslight_is_platform_user(): bool {
global $USER;
if (!property_exists($USER, 'email') || empty($USER->email)) {
return false;
}
$domain = explode('@', $USER->email)[1] ?? '';
return $domain === 'lmslight.io';
}
/**
* Return the current site subscription level.
*
* Levels: 0 = Core, 1 = Grow, 2 = Pro.
*
* @return int The subscription level (defaults to 0 if not set).
*/
function local_lmslight_get_subscription_level(): int {
global $CFG;
return property_exists($CFG, 'subscriptionlevel') ? (int) $CFG->subscriptionlevel : 0;
}
/**
* Return the list of admin setting keys that are pruned for the current
* user and subscription level.
*
* @return string[] Setting keys that are hidden from the current user.
*/
function local_lmslight_get_pruned_keys(): array {
$keys = [
'analytics',
'license',
'moodlenet',
'backups',
'development',
'adminnotifications',
'registrationmoodleorg',
'moodleservices',
'userfeedback',
'tool_installaddon_index',
'pluginsoverview',
'taskconfig',
'email',
'systempaths',
'supportcontact',
'sessionhandling',
'http',
'maintenancemode',
'cleanup',
'environment',
'phpinfo',
'performance',
'updatenotifications',
'tool_filetypes',
'reportbackups',
'reportinfectedfiles',
'insights',
'reportperformance',
'reportsecurity',
'reportstatus',
];
if (local_lmslight_get_subscription_level() < 1) {
array_push($keys,
'server',
'oauth2',
'authsettingoauth2',
'authsettingsaml2',
'authsettingldap',
'authsettingcas',
'authsettingshibboleth'
);
}
return $keys;
}