-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
387 lines (364 loc) · 14.2 KB
/
lib.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
<?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/>.
/**
* Authentication Plugin: Magic Authentication lib functions.
*
*
* @package auth_magic
* @copyright 2022 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_user\output\myprofile\tree;
/**
* Get available manual enrolment courses.
* @return array courses.
*/
function auth_magic_get_courses_for_registration() {
global $PAGE, $DB;
$courses = [];
$coursesinfo = $DB->get_records('course', null, 'id DESC');
if (!empty($coursesinfo)) {
foreach ($coursesinfo as $info) {
$instances = enrol_get_instances($info->id, true);
// Make sure manual enrolments instance exists.
foreach ($instances as $instance) {
if ($instance->enrol == 'manual') {
$courselist = new core_course_list_element($info);
$courses[$info->id] = $courselist->get_formatted_fullname();
}
}
}
}
return $courses;
}
/**
* Get available manual enrolment given course.
* @param int $courseid
* @return bool status.
*/
function auth_magic_is_course_manual_enrollment($courseid) {
$instances = enrol_get_instances($courseid, true);
// Make sure manual enrolments instance exists.
foreach ($instances as $instance) {
if ($instance->enrol == 'manual') {
return true;
}
}
return false;
}
/**
* Display user courses.
* @param int $userid
* @return string
*/
function auth_magic_user_courses($userid) {
$courses = enrol_get_all_users_courses($userid, true, array('fullname'), 'fullname');
$fullnames = array_column($courses, 'fullname');
$content = '';
if (count($fullnames) > 3) {
$show = array_slice($fullnames, 0, 3);
$more = array_slice($fullnames, 3);
$content .= implode(',', $show);
$content .= "<details><summary>" . get_string('more', 'auth_magic', count($more)) . "</summary>";
$content .= implode(',', $more). "</details>";
} else {
$content .= implode(',', $fullnames);
}
return $content;
}
/**
* Enroll user into the course.
* @param int $courseid courseid
* @param object $user user
* @param int $enrolmentduration duration
* @return bool enrol or not
*/
function auth_magic_enroll_course_user($courseid, $user, $enrolmentduration = 0) {
global $DB;
$context = context_course::instance($courseid);
$instance = $DB->get_record('enrol', array('courseid' => $courseid, 'enrol' => 'manual'));
$enrolmanual = enrol_get_plugin('manual');
if ($enrolmanual && !empty($instance)) {
if ($enrolmanual->allow_enrol($instance)) {
$timeend = 0;
if ($enrolmentduration) {
$timeend = time() + $enrolmentduration;
}
$roleid = get_config('auth_magic', 'enrolmentrole');
$enrolmanual->enrol_user($instance, $user->id, $roleid, time(), $timeend);
return true;
}
}
return false;
}
/**
* Display user created confim box
* @param array $args
* @return string
*/
function auth_magic_output_fragment_display_box_content($args) {
global $DB, $OUTPUT;
$templatecontext = [];
$user = $DB->get_record('user', array('id' => $args['user']));
$profileurl = new moodle_url('/user/profile.php', array('id' => $user->id));
if ($args['course']) {
$course = get_course($args['course']);
$courseelement = new core_course_list_element($course);
$templatecontext['coursename'] = $courseelement->get_formatted_fullname();
$profileurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
}
$userkeyinfo = auth_magic_get_user_userkeyinfo($user->id);
$templatecontext['username'] = fullname($user);
$templatecontext['magicinvitation'] = $userkeyinfo->magicinvitation;
$templatecontext['profileurl'] = $profileurl->out(false);
$status = '';
if (!$args['userexist'] && $args['course']) {
$status = get_string('createuserenrolcourse', 'auth_magic', $templatecontext['coursename']);
} else if ($args['userexist'] && $args['course']) {
$status = get_string('existuserenrolcourse', 'auth_magic', $templatecontext['coursename']);
} else if (!$args['userexist'] && !$args['course']) {
$status = get_string('statuscreateuser', 'auth_magic');
}
$templatecontext['status'] = $status;
return $OUTPUT->render_from_template('auth_magic/modalbox', $templatecontext);
}
/**
* Get the logininfo for given user.
* @param int $userid
* @return object
*/
function auth_magic_get_user_userkeyinfo($userid) {
global $DB;
return $DB->get_record('auth_magic_loginlinks', array('userid' => $userid));
}
/**
* Get user login link
* @param int $userid
* @return string url.
*/
function auth_magic_get_user_login_link($userid) {
global $DB;
return $DB->get_field('auth_magic_loginlinks', 'magiclogin', array('userid' => $userid), 'loginurl');
}
/**
* Get user invitation link
* @param int $userid
* @return string url.
*/
function auth_magic_get_user_invitation_link($userid) {
global $DB;
return $DB->get_field('auth_magic_loginlinks', 'magicinvitation', array('userid' => $userid), 'loginurl');
}
/**
* Defines learningtools nodes for my profile navigation tree.
*
* @param \core_user\output\myprofile\tree $tree Tree object
* @param stdClass $user user object
* @param bool $iscurrentuser is the user viewing profile, current user ?
* @param stdClass $course course object
*
* @return bool
*/
function auth_magic_myprofile_navigation(tree $tree, $user, $iscurrentuser, $course) {
global $USER;
if (!is_enabled_auth('magic')) {
return;
}
// Get the learningtools category.
if (!array_key_exists('magicauth', $tree->__get('categories'))) {
// Create the category.
$categoryname = get_string('configtitle', 'auth_magic');
$category = new core_user\output\myprofile\category('magicauth', $categoryname, 'privacyandpolicies');
$tree->add_category($category);
} else {
// Get the existing category.
$category = $tree->__get('categories')['magicauth'];
}
$systemcontext = context_system::instance();
if ($iscurrentuser) {
// Quick registration.
if (!empty($course)) {
$coursecontext = context_course::instance($course->id);
if (has_capability('auth/magic:cancoursequickregistration', $coursecontext) &&
auth_magic_is_course_manual_enrollment($course->id)) {
$registrationurl = new moodle_url('/auth/magic/registration.php', array('courseid' => $course->id));
$registersnode = new core_user\output\myprofile\node('magicauth', 'quickregistration',
get_string('quickregistration', 'auth_magic'), null, $registrationurl);
$tree->add_node($registersnode);
}
} else {
$usercontext = context_user::instance($USER->id);
if (has_capability('auth/magic:viewloginlinks', $systemcontext)) {
$magickeysurl = new moodle_url('/auth/magic/listusers.php');
$magickeysnode = new core_user\output\myprofile\node('magicauth', 'magickeys',
get_string('userkeyslist', 'auth_magic'), null, $magickeysurl);
$tree->add_node($magickeysnode);
} else if (auth_magic_is_parent_see_child_magiclinks()) {
$magickeysurl = new moodle_url('/auth/magic/listusers.php', array('userid' => $USER->id));
$magickeysnode = new core_user\output\myprofile\node('magicauth', 'magickeys',
get_string('userkeyslist', 'auth_magic'), null, $magickeysurl);
$tree->add_node($magickeysnode);
}
if (has_capability('auth/magic:cansitequickregistration', $systemcontext)) {
$registrationurl = new moodle_url('/auth/magic/registration.php');
$registersnode = new core_user\output\myprofile\node('magicauth', 'quickregistration',
get_string('quickregistration', 'auth_magic'), null, $registrationurl);
$tree->add_node($registersnode);
}
}
}
}
/**
* Get the child users for the parent.
* @param int $parentid
* @param bool $checkparent
* @return array child users.
*/
function auth_magic_get_parent_child_users($parentid, $checkparent = false) {
global $DB;
$isparent = false;
$users = [];
if ($usercontexts = $DB->get_records_sql("SELECT c.instanceid
FROM {role_assignments} ra, {context} c, {user} u, {auth_magic_loginlinks} mc
WHERE ra.userid = ?
AND ra.contextid = c.id
AND u.id = mc.userid
AND c.instanceid = u.id
AND c.contextlevel = ".CONTEXT_USER, array($parentid))) {
$users = array_keys($usercontexts);
$isparent = true;
}
if ($checkparent) {
return $isparent;
}
return $users;
}
/**
* Parent can see the child user keys
*/
function auth_magic_is_parent_see_child_magiclinks() {
global $USER;
$status = false;
$users = auth_magic_get_parent_child_users($USER->id);
if (!empty($users)) {
$user = current($users);
$usercontext = context_user::instance($user);
if (has_capability('auth/magic:viewchildloginlinks', $usercontext)) {
$status = true;
}
}
return $status;
}
/**
* Send message to user using message api.
*
* @param mixed $userto
* @param mixed $subject
* @param mixed $messageplain
* @param mixed $messagehtml
* @param mixed $courseid
* @return bool message status
*/
function auth_magic_messagetouser($userto, $subject, $messageplain, $messagehtml, $courseid = null) {
$eventdata = new \core\message\message();
$eventdata->name = 'auth_magic';
$eventdata->component = 'auth_magic';
$eventdata->modulename = 'moodle';
$eventdata->courseid = empty($courseid) ? SITEID : $courseid;
$eventdata->userfrom = core_user::get_support_user();
$eventdata->userto = $userto;
$eventdata->subject = $subject;
$eventdata->fullmessage = $messageplain;
$eventdata->fullmessageformat = FORMAT_HTML;
$eventdata->fullmessagehtml = $messagehtml;
$eventdata->smallmessage = $subject;
if (message_send($eventdata)) {
return true;
} else {
return false;
}
}
/**
* Sent the invitation link to the user.
* @param int $userid
* @return bool message status
*/
function auth_magic_sent_invitation_user($userid) {
$site = get_site();
$user = \core_user::get_user($userid);
$invitationlink = auth_magic_get_user_invitation_link($userid);
$subject = get_string('magicloginlink', 'auth_magic', format_string($site->fullname));
// Lang data.
$data = new stdClass();
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();
$data->fullname = fullname($user);
$data->link = $invitationlink;
$messageplain = get_string('invitationmessage', 'auth_magic', $data); // Plain text.
$messagehtml = text_to_html($messageplain, false, false, true);
$user->mailformat = 1; // Always send HTML version as well.
return auth_magic_messagetouser($user, $subject, $messageplain, $messagehtml);
}
/**
* Sent the login link to the user.
* @param int $userid
* @param bool $otherauth
* @param bool $expired
* @return bool message status
*/
function auth_magic_sent_loginlink_touser($userid, $otherauth = false, $expired = false) {
$site = get_site();
$user = \core_user::get_user($userid);
if ($otherauth) {
$auth = get_auth_plugin('magic');
$auth->create_magic_instance($user, false);
}
$loginlink = auth_magic_get_user_login_link($userid);
$subject = get_string('loginsubject', 'auth_magic', format_string($site->fullname));
$data = new stdClass();
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();
$data->fullname = fullname($user);
$data->link = $loginlink;
if ($expired) {
$messageplain = get_string('expiredloginlinkmsg', 'auth_magic', $data);
} else {
$messageplain = get_string('loginlinknmessage', 'auth_magic', $data);
}
$messagehtml = text_to_html($messageplain, false, false, true);
$user->mailformat = 1; // Always send HTML version as well.
return auth_magic_messagetouser($user, $subject, $messageplain, $messagehtml);
}
/**
* Sent the information for non magic auth users.
* @param int $userid
* @return void
*/
function auth_magic_requiredmail_magic_authentication($userid) {
$site = get_site();
$user = \core_user::get_user($userid);
$forgothtml = html_writer::link(new moodle_url('/login/forgot_password.php'), get_string('forgotten'));
$subject = get_string('loginsubject', 'auth_magic', format_string($site->fullname));
$data = new stdClass();
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();
$data->fullname = fullname($user);
$data->forgothtml = $forgothtml;
$messageplain = get_string('preventmagicauthmessage', 'auth_magic', $data);
$messagehtml = text_to_html($messageplain, false, false, true);
$user->mailformat = 1;
return auth_magic_messagetouser($user, $subject, $messageplain, $messagehtml);
}