/
auth.php
727 lines (581 loc) · 21 KB
/
auth.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?php
/**
* Custom authentication for Water Corporation Education project
*
* Authentication class
*
* @package auth_watercorped
* @author Bevan Holman <bevan@pukunui.com>, Pukunui
* @copyright 2015 onwards, Pukunui
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/authlib.php');
/**
* Water Corporation Education authentication class
*/
class local_plugin_meditraxcohort extends local_plugin_base {
/**
* Constructor.
*/
public $loginform;
public $signupform;
public function __construct() {
$this->authtype = 'watercorped';
$this->config = get_config('auth/watercorped');
}
/**
* Update a user session
*
* @return true
*/
public function update_user_session($u) {
global $USER;
// Override old $USER session variable.
foreach ((array)$u as $variable => $value) {
if ($variable === 'description' or $variable === 'password') {
// These are not set for security and perf reasons.
continue;
}
$USER->$variable = $value;
}
return true;
}
/**
* Return a form to log into a user account
* This is used in /login/signup.php
*
* @return moodle_form a form which edits a record from the user table
*/
public function login_form($loginurl) {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_login_form($loginurl, null, 'post', '', array('autocomplete' => 'on'));
}
/**
* Returns true if the username and password work and false if they are
* wrong or don't exist.
*
* @param string $username The username
* @param string $password The password
* @return bool Authentication success or failure.
*/
public function user_login($username, $password) {
global $CFG, $DB;
if ($user = $DB->get_record('user', array('username' => $username, 'mnethostid' => $CFG->mnet_localhost_id))) {
return validate_internal_user_password($user, $password);
}
return false;
}
/**
* Updates the user's password.
*
* called when the user password is updated.
*
* @param object $user User table object (with system magic quotes)
* @param string $password Plaintext password (with system magic quotes)
* @return boolean result
*
*/
public function user_update_password($user, $password) {
$user = get_complete_user_data('id', $user->id);
// This will also update the stored hash to the latest algorithm if the existing hash is using an out-of-date
// algorithm (or the legacy md5 algorithm).
return update_internal_user_password($user, $password);
}
public function can_signup() {
return true;
}
/**
* Sign up a new user ready for confirmation.
* Password is passed in plaintext.
*
* @param object $user new user object
* @param boolean $notify print notice with link and terminate
*/
public function user_signup($user, $notify=true) {
global $CFG, $DB;
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
$plainpassword = $user->password;
$user->password = hash_internal_user_password($user->password);
$user->mnethostid = $CFG->mnet_localhost_id;
if (empty($user->secret)) {
$user->secret = '';
}
if (empty($user->calendartype)) {
$user->calendartype = $CFG->calendartype;
}
$firstname = strtolower($user->firstname);
$lastname = strtolower($user->lastname);
$initials = $firstname[0].$lastname[0];
try {
$transaction = $DB->start_delegated_transaction();
do {
$username = sprintf($initials."%04d", rand (1 , 9999));
} while ($DB->get_record('user', array("username" => $username), 'id', IGNORE_MISSING));
$user->username = $username;
$userdetailstext = "Username: $username<br />Password: $plainpassword";
$user->id = user_create_user($user, false, false);
user_add_password_history($user->id, $plainpassword);
// Save any custom profile field information.
$user->profile_field_typeofaccount = empty($user->profile_field_typeofaccount) ? 'student' : $user->profile_field_typeofaccount;
$user->profile_field_yearlevel = empty($user->profile_field_yearlevel) ? 'N/A' : $user->profile_field_yearlevel;
$user->profile_field_yearofbirth = empty($user->profile_field_yearofbirth) ? 'N/A' : $user->profile_field_yearofbirth;
$user->profile_field_whereareyoufrom = empty($user->profile_field_whereareyoufrom) ? 'Perth' : $user->profile_field_whereareyoufrom;
profile_save_data($user);
$record = new stdClass();
$record->studentuserid = $user->id;
$record->teacheruserid = $user->id;
switch ($user->profile_field_typeofaccount) {
case 'wceteacher': {
$this->add_teacher($user->id);
break;
}
case 'adult':
case 'student':
default: {
$this->add_student($user->id);
break;
}
}
// Trigger event.
\core\event\user_created::create_from_userid($user->id)->trigger();
// Assuming the both inserts work, we get to the following line.
$transaction->allow_commit();
} catch (Exception $e) {
$transaction->rollback($e);
return false;
}
if (!send_confirmation_email($user)) {
print_error('auth_emailnoemail, auth_email');
}
if ($notify) {
global $CFG, $PAGE, $OUTPUT;
$emailconfirm = get_string('emailconfirm');
$PAGE->navbar->add($emailconfirm);
$PAGE->set_title($emailconfirm);
$PAGE->set_heading($PAGE->course->fullname);
echo $OUTPUT->header();
notice(get_string('signup:emailconfirmsent:text', 'auth_watercorped', $userdetailstext), "$CFG->wwwroot/index.php");
} else {
return true;
}
}
/**
* Update details for the current user
* Password is passed in plaintext.
*
* @param object $user current user object
* @param boolean $notify print notice with link and terminate
*/
public function user_update_details($user) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
if ($user->password == $user->confirmpassword and !empty($user->password)) {
$plainpassword = $user->password;
echo $plainpassword;
$user->password = hash_internal_user_password($user->password);
$this->user_update_password($user, $user->password);
user_add_password_history($user->id, $plainpassword);
}
if (empty($user->calendartype)) {
$user->calendartype = $CFG->calendartype;
}
try {
$transaction = $DB->start_delegated_transaction();
user_update_user($user, false, false);
$user->profile_field_yearlevel = empty($user->profile_field_yearlevel) ? 'N/A' : $user->profile_field_yearlevel;
$user->profile_field_yearofbirth = empty($user->profile_field_yearofbirth) ? 'N/A' : $user->profile_field_yearofbirth;
$user->profile_field_whereareyoufrom = empty($user->profile_field_whereareyoufrom) ? 'Perth' : $user->profile_field_whereareyoufrom;
$USER->profile['yearlevel'] = $user->profile_field_yearlevel;
$USER->profile['yearofbirth'] = $user->profile_field_yearofbirth;
$USER->profile['whereareyoufrom'] = $user->profile_field_whereareyoufrom;
profile_save_data($user);
// Trigger event.
\core\event\user_updated::create_from_userid($user->id)->trigger();
// Assuming the both inserts work, we get to the following line.
$transaction->allow_commit();
} catch (Exception $e) {
$transaction->rollback($e);
return false;
}
return $this->update_user_session($user);
}
/**
* Returns true if plugin allows confirming of new users.
*
* @return bool
*/
public function can_confirm() {
return true;
}
/**
* Confirm the new user as registered.
*
* @param string $username
* @param string $confirmsecret
*/
public function user_confirm($username, $confirmsecret) {
global $DB;
$user = get_complete_user_data('username', $username);
if (!empty($user)) {
if ($user->auth != $this->authtype) {
return AUTH_CONFIRM_ERROR;
} else if ($user->secret == $confirmsecret && $user->confirmed) {
return AUTH_CONFIRM_ALREADY;
} else if ($user->secret == $confirmsecret) {
// They have provided the secret key to get in.
$DB->set_field("user", "confirmed", 1, array("id" => $user->id));
return AUTH_CONFIRM_OK;
}
} else {
return AUTH_CONFIRM_ERROR;
}
}
public function prevent_local_passwords() {
return false;
}
/**
* Returns true if this authentication plugin is 'internal'.
*
* @return bool
*/
public function is_internal() {
return true;
}
/**
* Returns true if this authentication plugin can change the user's
* password.
*
* @return bool
*/
public function can_change_password() {
return true;
}
/**
* Returns the URL for changing the user's pw, or empty if the default can
* be used.
*
* @return moodle_url
*/
public function change_password_url() {
// Use default internal method.
return null;
}
/**
* Returns true if plugin allows resetting of internal password.
*
* @return bool
*/
public function can_reset_password() {
return true;
}
/**
* Returns true if plugin can be manually set.
*
* @return bool
*/
public function can_be_manually_set() {
return true;
}
/**
* Prints a form for configuring this authentication plugin.
*
* This function is called from admin/auth.php, and outputs a full page with
* a form for configuring this plugin.
*
* @param array $page An object containing all the data for this page.
*/
public function config_form($config, $err, $userfields) {
include("config.html");
}
/**
* Processes and stores configuration data for this authentication plugin.
*/
public function process_config($config) {
// Set to defaults if undefined.
if (!isset($config->recaptcha)) {
$config->recaptcha = false;
}
// Save settings.
set_config('recaptcha', $config->recaptcha, 'auth/watercorped');
return true;
}
/**
* Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
* @return bool
*/
public function is_captcha_enabled() {
global $CFG;
return isset($CFG->recaptchapublickey) &&
isset($CFG->recaptchaprivatekey) &&
get_config("auth/{$this->authtype}", 'recaptcha');
}
/**
* Return a form to capture user details for account creation
* This is used in /login/signup.php
*
* @return moodle_form a form which edits a record from the user table
*/
public function signup_form() {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_signup_form(null, null, 'post', '', array('autocomplete' => 'on'));
}
/**
* Return a form to capture user details for account creation
* This is used in /login/signup.php
*
* @return moodle_form a form which edits a record from the user table
*/
public function updateuser_form() {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_updateuser_form(null, null, 'post', '', array('autocomplete' => 'on'));
}
/**
* Return a form to capture user details for account creation
* This is used in /login/signup.php
*
* @return moodle_form a form which edits a record from the user table
*/
public function classessectionform() {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_manage_classes_section(null, null, 'post', '', array('autocomplete' => 'on'));
}
/**
* Return a form to add/update/delete a class
*
* @return moodle_form a form which edits a record from the class table
*/
public function classform($url, $id) {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_classform($url, $id, 'post', '', array('autocomplete' => 'on'));
}
/**
* Links to add a student and a a group of students
*
* @return moodle_form a form which links to other pages
*/
public function studentlistform($url, $teacherid) {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_manage_students_classes_section($url, $teacherid, 'post', '', array('autocomplete' => 'on'));
}
/**
* Links to add a student and a a group of students
*
* @return moodle_form a form which links to other pages
*/
public function upload_import_file($url, $teacherid) {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_import_students_section($url, $teacherid, 'post', '', array('autocomplete' => 'on'));
}
/**
* List all students for a teacher
*
* @return table that has a list of all students for a teacher ordered by class name
*/
public function listofallstudents($teacherid) {
global $CFG;
require_once($CFG->dirroot.'/auth/watercorped/locallib.php');
return auth_watercorped_get_student_class_table($teacherid);
}
/**
* List all classes a student is in of a teacher
*
* @return table that has a list of all students for a teacher ordered by class name
*/
public function deletefromclasseslist($delclass) {
global $DB;
/*
JOIN {auth_watercorped_student} AS aws ON aws.studentuserid = awsc.studentuserid
JOIN {user} AS u ON u.id = aws.studentuserid
*/
$sql = '
SELECT awsc.id, awsc.classid, awc.name AS classname
FROM {auth_watercorped_teacher} AS awt
JOIN {auth_watercorped_class} AS awc ON awc.teacheruserid = awt.teacheruserid
JOIN {auth_watercorped_studclass} AS awsc ON awsc.classid = awc.id
WHERE awt.teacheruserid = :teacheruserid
AND awsc.studentuserid = :studentuserid
';
$params = array('teacheruserid' => $delclass->teacheruserid, 'studentuserid' => $delclass->studentuserid);
if (!empty($delclass->classid)) {
$sql .= 'AND awsc.classid = :classid';
$params['classid'] = $delclass->classid;
}
if ($class = $DB->get_records_sql($sql, $params)) {
return $class;
}
return false;
}
/**
* For for deleting a student from a class
*
* @return table that has a list of all students for a teacher ordered by class name
*/
public function deletefromclassesform($url, $deleteclasslist, $classinfo) {
global $CFG;
$params = $arrayName = array('deleteclasslist' => $deleteclasslist, 'classinfo' => $classinfo);
require_once($CFG->dirroot.'/auth/watercorped/forms.php');
return new auth_watercorped_delete_student_from_class($url, $params, 'post', '', array('autocomplete' => 'on'));
}
/**
* Add a new teacher.
*
* @param int teacheruserid
*/
public function add_teacher($teacheruserid) {
global $DB;
$newteacher = new stdClass();
$newteacher->teacheruserid = $teacheruserid;
if (!($DB->insert_record('auth_watercorped_teacher', $newteacher))) {
$strcontinue = get_string('db:teacher:insert:success', 'auth_watercorped');
} else {
$strcontinue = get_string('db:teacher:insert:error', 'auth_watercorped');
}
return $strcontinue;
}
/**
* Delete a teacher.
*
* @param int teacheruserid
*/
public function remove_teacher($teacheruserid) {
global $DB;
if (!($DB->delete_records('auth_watercorped_teacher', array("teacheruserid" => $teacheruserid)))) {
return false;
}
return true;
}
/**
* Add a new student.
*
* @param int studentuserid
* @param string name
*/
public function add_student($studentuserid) {
global $DB;
$newstudent = new stdClass();
$newstudent->studentuserid = $studentuserid;
if (!($DB->insert_record('auth_watercorped_student', $newstudent))) {
return false;
}
return true;
}
/**
* Delete a student.
*
* @param int studentuserid
*/
public function remove_student($studentuserid) {
global $DB;
if (!($DB->delete_records('auth_watercorped_student', array("studentuserid" => $studentuserid)))) {
return false;
}
return true;
}
/**
* Add a new class.
*
* @param int teacheruserid
* @param string name
*/
public function add_class($teacheruserid, $name) {
global $DB;
$strcontinue = '';
$addparams = new stdClass();
$addparams->teacheruserid = $teacheruserid;
$addparams->name = $name;
if ($classid = $DB->insert_record('auth_watercorped_class', $addparams)) {
// $strcontinue = get_string('db:class:insert:success', 'auth_watercorped');
} else {
// $strcontinue = get_string('db:class:insert:error', 'auth_watercorped');
$classid = 0;
}
return $classid;
}
/**
* Update a class.
*
* @param int classid
* @param int teacheruserid
* @param string name
*/
public function update_class_name($classid, $name) {
global $DB;
$updateparams = new stdClass();
$updateparams->id = $classid;
$updateparams->name = $name;
if ($DB->update_record('auth_watercorped_class', $updateparams)) {
$strcontinue = get_string('db:class:update:success', 'auth_watercorped');
} else {
$strcontinue = get_string('db:class:update:error', 'auth_watercorped');
}
return $strcontinue;
}
/**
* Remove a class.
*
* @param int teacheruserid
* @param string name
*/
public function remove_class($id) {
global $DB;
if ($DB->delete_records('auth_watercorped_class', array("id" => $id))) {
$strcontinue = get_string('db:class:delete:success', 'auth_watercorped');
} else {
$strcontinue = get_string('db:class:delete:error', 'auth_watercorped');
}
return $strcontinue;
}
/**
* Remove all of one teacher's classes.
*
* @param int teacheruserid
*/
public function remove_teacher_classes($removeclasses, $confirmsecret) {
global $DB;
if (!($DB->delete_records('auth_watercorped_class', array("teacheruserid" => $teacheruserid)))) {
return false;
}
return true;
}
/**
* Add student to class.
*
* @param int classid
* @param int studentuserid
*/
public function add_student_class($classid, $studentuserid) {
global $DB;
$studclass = new stdClass();
$studclass->classid = $classid;
$studclass->studentuserid = $studentuserid;
if (!($DB->insert_record('auth_watercorped_studclass', $studclass))) {
return false;
}
return true;
}
/**
* Remove student to class.
*
* @param int classid
* @param int studentuserid
*/
public function remove_student_class($classid, $studentuserid) {
global $DB;
if ($DB->delete_records('auth_watercorped_studclass', array("classid" => $classid, "studentuserid" => $studentuserid))) {
$strcontinue = get_string('db:class:delete:success', 'auth_watercorped');
} else {
$strcontinue = get_string('db:class:delete:error', 'auth_watercorped');
}
return $strcontinue;
}
}