MDL-31501 rework user session architecture
[moodle.git] / lib / classes / session / manager.php
CommitLineData
d79d5ac2
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Session manager class.
19 *
20 * @package core
21 * @copyright 2013 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core\session;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Session manager, this is the public Moodle API for sessions.
31 *
32 * Following PHP functions MUST NOT be used directly:
33 * - session_start() - not necessary, lib/setup.php starts session automatically,
34 * use define('NO_MOODLE_COOKIE', true) if session not necessary.
35 * - session_write_close() - use \core\session\manager::write_close() instead.
36 * - session_destroy() - use require_logout() instead.
37 *
38 * @package core
39 * @copyright 2013 Petr Skoda {@link http://skodak.org}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class manager {
43 /** @var handler $handler active session handler instance */
44 protected static $handler;
45
46 /** @var bool $sessionactive Is the session active? */
47 protected static $sessionactive = null;
48
49 /**
50 * Start user session.
51 *
52 * Note: This is intended to be called only from lib/setup.php!
53 */
54 public static function start() {
55 global $CFG, $DB;
56
57 if (isset(self::$sessionactive)) {
58 debugging('Session was already started!', DEBUG_DEVELOPER);
59 return;
60 }
61
62 self::load_handler();
63
64 // Init the session handler only if everything initialised properly in lib/setup.php file
65 // and the session is actually required.
66 if (empty($DB) or empty($CFG->version) or !defined('NO_MOODLE_COOKIES') or NO_MOODLE_COOKIES or CLI_SCRIPT) {
67 self::$sessionactive = false;
68 self::init_empty_session();
69 return;
70 }
71
72 try {
73 self::$handler->init();
74 self::prepare_cookies();
75 $newsid = empty($_COOKIE[session_name()]);
76
77 session_start();
78
79 self::initialise_user_session($newsid);
80 self::check_security();
81
82 } catch (\Exception $ex) {
83 @session_write_close();
84 self::init_empty_session();
85 self::$sessionactive = false;
86 throw $ex;
87 }
88
89 self::$sessionactive = true;
90 }
91
92 /**
93 * Returns current page performance info.
94 *
95 * @return array perf info
96 */
97 public static function get_performance_info() {
98 if (!session_id()) {
99 return array();
100 }
101
102 self::load_handler();
103 $size = display_size(strlen(session_encode()));
104 $handler = get_class(self::$handler);
105
106 $info = array();
107 $info['size'] = $size;
108 $info['html'] = "<span class=\"sessionsize\">Session ($handler): $size</span> ";
109 $info['txt'] = "Session ($handler): $size ";
110
111 return $info;
112 }
113
114 /**
115 * Create handler instance.
116 */
117 protected static function load_handler() {
118 global $CFG, $DB;
119
120 if (self::$handler) {
121 return;
122 }
123
124 // Find out which handler to use.
125 if (PHPUNIT_TEST) {
126 $class = '\core\session\file';
127
128 } else if (!empty($CFG->session_handler_class)) {
129 $class = $CFG->session_handler_class;
130
131 } else if (!empty($CFG->dbsessions) and $DB->session_lock_supported()) {
132 $class = '\core\session\database';
133
134 } else {
135 $class = '\core\session\file';
136 }
137 self::$handler = new $class();
138 }
139
140 /**
141 * Empty current session, fill it with not-logged-in user info.
142 */
143 protected static function init_empty_session() {
144 global $CFG;
145
146 // Session not used at all.
147 $_SESSION = array();
148 $_SESSION['SESSION'] = new \stdClass();
149 $_SESSION['USER'] = new \stdClass();
150 $_SESSION['USER']->id = 0;
151 if (isset($CFG->mnet_localhost_id)) {
152 $_SESSION['USER']->mnethostid = $CFG->mnet_localhost_id;
153 } else {
154 // Not installed yet, the future host id will be most probably 1.
155 $_SESSION['USER']->mnethostid = 1;
156 }
157
158 if (PHPUNIT_TEST) {
159 // Phpunit tests use reversed reference.
160 global $USER, $SESSION;
161 $USER = $_SESSION['USER'];
162 $SESSION = $_SESSION['SESSION'];
163 $_SESSION['USER'] =& $USER;
164 $_SESSION['SESSION'] =& $SESSION;
165 }
166 }
167
168 /**
169 * Make sure all cookie and session related stuff is configured properly before session start.
170 */
171 protected static function prepare_cookies() {
172 global $CFG;
173
174 if (!isset($CFG->cookiesecure) or (strpos($CFG->wwwroot, 'https://') !== 0 and empty($CFG->sslproxy))) {
175 $CFG->cookiesecure = 0;
176 }
177
178 if (!isset($CFG->cookiehttponly)) {
179 $CFG->cookiehttponly = 0;
180 }
181
182 // Set sessioncookie variable if it isn't already.
183 if (!isset($CFG->sessioncookie)) {
184 $CFG->sessioncookie = '';
185 }
186 $sessionname = 'MoodleSession'.$CFG->sessioncookie;
187
188 // Make sure cookie domain makes sense for this wwwroot.
189 if (!isset($CFG->sessioncookiedomain)) {
190 $CFG->sessioncookiedomain = '';
191 } else if ($CFG->sessioncookiedomain !== '') {
192 $host = parse_url($CFG->wwwroot, PHP_URL_HOST);
193 if ($CFG->sessioncookiedomain !== $host) {
194 if (substr($CFG->sessioncookiedomain, 0, 1) === '.') {
195 if (!preg_match('|^.*'.preg_quote($CFG->sessioncookiedomain, '|').'$|', $host)) {
196 // Invalid domain - it must be end part of host.
197 $CFG->sessioncookiedomain = '';
198 }
199 } else {
200 if (!preg_match('|^.*\.'.preg_quote($CFG->sessioncookiedomain, '|').'$|', $host)) {
201 // Invalid domain - it must be end part of host.
202 $CFG->sessioncookiedomain = '';
203 }
204 }
205 }
206 }
207
208 // Make sure the cookiepath is valid for this wwwroot or autodetect if not specified.
209 if (!isset($CFG->sessioncookiepath)) {
210 $CFG->sessioncookiepath = '';
211 }
212 if ($CFG->sessioncookiepath !== '/') {
213 $path = parse_url($CFG->wwwroot, PHP_URL_PATH).'/';
214 if ($CFG->sessioncookiepath === '') {
215 $CFG->sessioncookiepath = $path;
216 } else {
217 if (strpos($path, $CFG->sessioncookiepath) !== 0 or substr($CFG->sessioncookiepath, -1) !== '/') {
218 $CFG->sessioncookiepath = $path;
219 }
220 }
221 }
222
223 // Discard session ID from POST, GET and globals to tighten security,
224 // this is session fixation prevention.
225 unset($GLOBALS[$sessionname]);
226 unset($_GET[$sessionname]);
227 unset($_POST[$sessionname]);
228 unset($_REQUEST[$sessionname]);
229
230 // Compatibility hack for non-browser access to our web interface.
231 if (!empty($_COOKIE[$sessionname]) && $_COOKIE[$sessionname] == "deleted") {
232 unset($_COOKIE[$sessionname]);
233 }
234
235 // Set configuration.
236 session_name($sessionname);
237 session_set_cookie_params(0, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly);
238 ini_set('session.use_trans_sid', '0');
239 ini_set('session.use_only_cookies', '1');
240 ini_set('session.hash_function', '0'); // For now MD5 - we do not have room for sha-1 in sessions table.
241 ini_set('session.use_strict_mode', '0'); // We have custom protection in session init.
242 ini_set('session.serialize_handler', 'php'); // We can move to 'php_serialize' after we require PHP 5.5.4 form Moodle.
243
244 // Moodle does normal session timeouts, this is for leftovers only.
245 ini_set('session.gc_probability', 1);
246 ini_set('session.gc_divisor', 1000);
247 ini_set('session.gc_maxlifetime', 60*60*24*4);
248 }
249
250 /**
251 * Initialise $USER and $SESSION objects, handles google access
252 * and sets up not-logged-in user properly.
253 *
254 * @param bool $newsid is this a new session in first http request?
255 */
256 protected static function initialise_user_session($newsid) {
257 global $CFG, $DB;
258
259 $sid = session_id();
260 if (!$sid) {
261 // No session, very weird.
262 error_log('Missing session ID, session not started!');
263 self::init_empty_session();
264 return;
265 }
266
267 if (!$record = $DB->get_record('sessions', array('sid'=>$sid), 'id, sid, state, userid, lastip, timecreated, timemodified')) {
268 if (!$newsid) {
269 if (!empty($_SESSION['USER']->id)) {
270 // This should not happen, just log it, we MUST not produce any output here!
271 error_log("Cannot find session record $sid for user ".$_SESSION['USER']->id.", creating new session.");
272 }
273 }
274 session_regenerate_id(true);
275 $_SESSION = array();
276 }
277 unset($sid);
278
279 if (isset($_SESSION['USER']->id)) {
280 if (!empty($_SESSION['USER']->realuser)) {
281 $userid = $_SESSION['USER']->realuser;
282 } else {
283 $userid = $_SESSION['USER']->id;
284 }
285
286 // Verify timeout first.
287 $maxlifetime = $CFG->sessiontimeout;
288 $timeout = false;
289 if (isguestuser($userid) or empty($userid)) {
290 // Ignore guest and not-logged in timeouts, there is very little risk here.
291 $timeout = false;
292
293 } else if ($record->timemodified < time() - $maxlifetime) {
294 $timeout = true;
295 $authsequence = get_enabled_auth_plugins(); // Auths, in sequence.
296 foreach ($authsequence as $authname) {
297 $authplugin = get_auth_plugin($authname);
298 if ($authplugin->ignore_timeout_hook($_SESSION['USER'], $record->sid, $record->timecreated, $record->timemodified)) {
299 $timeout = false;
300 break;
301 }
302 }
303 }
304
305 if ($timeout) {
306 session_regenerate_id(true);
307 $_SESSION = array();
308 $DB->delete_records('sessions', array('id'=>$record->id));
309
310 } else {
311 // Update session tracking record.
312
313 $update = new \stdClass();
314 $updated = false;
315
316 if ($record->userid != $userid) {
317 $update->userid = $record->userid = $userid;
318 $updated = true;
319 }
320
321 $ip = getremoteaddr();
322 if ($record->lastip != $ip) {
323 $update->lastip = $record->lastip = $ip;
324 $updated = true;
325 }
326
327 $updatefreq = empty($CFG->session_update_timemodified_frequency) ? 20 : $CFG->session_update_timemodified_frequency;
328
329 if ($record->timemodified == $record->timecreated) {
330 // Always do first update of existing record.
331 $update->timemodified = $record->timemodified = time();
332 $updated = true;
333
334 } else if ($record->timemodified < time() - $updatefreq) {
335 // Update the session modified flag only once every 20 seconds.
336 $update->timemodified = $record->timemodified = time();
337 $updated = true;
338 }
339
340 if ($updated) {
341 $update->id = $record->id;
342 $DB->update_record('sessions', $update);
343 }
344
345 return;
346 }
347 } else {
348 if ($record) {
349 // This happens when people switch session handlers...
350 session_regenerate_id(true);
351 $_SESSION = array();
352 $DB->delete_records('sessions', array('id'=>$record->id));
353 }
354 }
355 unset($record);
356
357 $timedout = false;
358 if (!isset($_SESSION['SESSION'])) {
359 $_SESSION['SESSION'] = new \stdClass();
360 if (!$newsid) {
361 $timedout = true;
362 }
363 }
364
365 $user = null;
366
367 if (!empty($CFG->opentogoogle)) {
368 if (is_web_crawler()) {
369 $user = guest_user();
370 }
371 if (!empty($CFG->guestloginbutton) and !$user and !empty($_SERVER['HTTP_REFERER'])) {
372 // Automatically log in users coming from search engine results.
373 if (strpos($_SERVER['HTTP_REFERER'], 'google') !== false ) {
374 $user = guest_user();
375 } else if (strpos($_SERVER['HTTP_REFERER'], 'altavista') !== false ) {
376 $user = guest_user();
377 }
378 }
379 }
380
381 // Setup $USER and insert the session tracking record.
382 if ($user) {
383 self::set_user($user);
384 self::add_session_record($user->id);
385 } else {
386 self::init_empty_session();
387 self::add_session_record(0);
388 }
389
390 if ($timedout) {
391 $_SESSION['SESSION']->has_timed_out = true;
392 }
393 }
394
395 /**
396 * Insert new empty session record.
397 * @param int $userid
398 * @return \stdClass the new record
399 */
400 protected static function add_session_record($userid) {
401 global $DB;
402 $record = new \stdClass();
403 $record->state = 0;
404 $record->sid = session_id();
405 $record->sessdata = null;
406 $record->userid = $userid;
407 $record->timecreated = $record->timemodified = time();
408 $record->firstip = $record->lastip = getremoteaddr();
409
410 $record->id = $DB->insert_record('sessions', $record);
411
412 return $record;
413 }
414
415 /**
416 * Do various session security checks.
417 */
418 protected static function check_security() {
419 global $CFG;
420
421 if (!empty($_SESSION['USER']->id) and !empty($CFG->tracksessionip)) {
422 // Make sure current IP matches the one for this session.
423 $remoteaddr = getremoteaddr();
424
425 if (empty($_SESSION['USER']->sessionip)) {
426 $_SESSION['USER']->sessionip = $remoteaddr;
427 }
428
429 if ($_SESSION['USER']->sessionip != $remoteaddr) {
430 // This is a security feature - terminate the session in case of any doubt.
431 self::terminate_current();
432 throw new exception('sessionipnomatch2', 'error');
433 }
434 }
435 }
436
437 /**
438 * Login user, to be called from complete_user_login() only.
439 * @param \stdClass $user
440 */
441 public static function login_user(\stdClass $user) {
442 global $DB;
443
444 // Regenerate session id and delete old session,
445 // this helps prevent session fixation attacks from the same domain.
446
447 $sid = session_id();
448 session_regenerate_id(true);
449 $DB->delete_records('sessions', array('sid'=>$sid));
450 self::add_session_record($user->id);
451
452 // Let enrol plugins deal with new enrolments if necessary.
453 enrol_check_plugins($user);
454
455 // Setup $USER object.
456 self::set_user($user);
457 }
458
459 /**
460 * Terminate current user session.
461 * @return void
462 */
463 public static function terminate_current() {
464 global $DB;
465
466 if (!self::$sessionactive) {
467 self::init_empty_session();
468 self::$sessionactive = false;
469 return;
470 }
471
472 try {
473 $DB->delete_records('external_tokens', array('sid'=>session_id(), 'tokentype'=>EXTERNAL_TOKEN_EMBEDDED));
474 } catch (\Exception $ignored) {
475 // Probably install/upgrade - ignore this problem.
476 }
477
478 // Initialize variable to pass-by-reference to headers_sent(&$file, &$line).
479 $file = null;
480 $line = null;
481 if (headers_sent($file, $line)) {
482 error_log('Cannot terminate session properly - headers were already sent in file: '.$file.' on line '.$line);
483 }
484
485 // Write new empty session and make sure the old one is deleted.
486 $sid = session_id();
487 session_regenerate_id(true);
488 $DB->delete_records('sessions', array('sid'=>$sid));
489 self::init_empty_session();
490 self::add_session_record($_SESSION['USER']->id);
491 session_write_close();
492 self::$sessionactive = false;
493 }
494
495 /**
496 * No more changes in session expected.
497 * Unblocks the sessions, other scripts may start executing in parallel.
498 */
499 public static function write_close() {
500 if (self::$sessionactive) {
501 session_write_close();
502 } else {
503 if (session_id()) {
504 @session_write_close();
505 }
506 }
507 self::$sessionactive = false;
508 }
509
510 /**
511 * Does the PHP session with given id exist?
512 *
513 * Note: this does not actually verify the presence of sessions record.
514 *
515 * @param string $sid
516 * @return bool
517 */
518 public static function session_exists($sid) {
519 self::load_handler();
520 return self::$handler->session_exists($sid);
521 }
522
523 /**
524 * Fake last access for given session, this prevents session timeout.
525 * @param string $sid
526 */
527 public static function touch_session($sid) {
528 global $DB;
529
530 // Timeouts depend on core sessions table only, no need to update anything in external stores.
531
532 $sql = "UPDATE {sessions} SET timemodified = :now WHERE sid = :sid";
533 $DB->execute($sql, array('now'=>time(), 'sid'=>$sid));
534 }
535
536 /**
537 * Terminate all sessions unconditionally.
538 */
539 public static function kill_all_sessions() {
540 global $DB;
541
542 self::terminate_current();
543
544 self::load_handler();
545 self::$handler->kill_all_sessions();
546
547 try {
548 $DB->delete_records('sessions');
549 } catch (\dml_exception $ignored) {
550 // Do not show any warnings - might be during upgrade/installation.
551 }
552 }
553
554 /**
555 * Terminate give session unconditionally.
556 * @param string $sid
557 */
558 public static function kill_session($sid) {
559 global $DB;
560
561 self::load_handler();
562
563 if ($sid === session_id()) {
564 self::write_close();
565 }
566
567 self::$handler->kill_session($sid);
568
569 $DB->delete_records('sessions', array('sid'=>$sid));
570 }
571
572 /**
573 * Terminate all sessions of given user unconditionally.
574 * @param int $userid
575 */
576 public static function kill_user_sessions($userid) {
577 global $DB;
578
579 $sessions = $DB->get_records('sessions', array('userid'=>$userid), 'id DESC', 'id, sid');
580 foreach ($sessions as $session) {
581 self::kill_session($session->sid);
582 }
583 }
584
585 /**
586 * Set current user.
587 *
588 * @param \stdClass $user record
589 */
590 public static function set_user(\stdClass $user) {
591 $_SESSION['USER'] = $user;
592 unset($_SESSION['USER']->description); // Conserve memory.
593 unset($_SESSION['USER']->password); // Improve security.
594 if (isset($_SESSION['USER']->lang)) {
595 // Make sure it is a valid lang pack name.
596 $_SESSION['USER']->lang = clean_param($_SESSION['USER']->lang, PARAM_LANG);
597 }
598 sesskey(); // Init session key.
599
600 if (PHPUNIT_TEST) {
601 // Phpunit tests use reversed reference.
602 global $USER;
603 $USER = $_SESSION['USER'];
604 $_SESSION['USER'] =& $USER;
605 }
606 }
607
608 /**
609 * Periodic timed-out session cleanup.
610 */
611 public static function gc() {
612 global $CFG, $DB;
613
614 // This may take a long time...
615 set_time_limit(0);
616
617 $maxlifetime = $CFG->sessiontimeout;
618
619 try {
620 // Kill all sessions of deleted and suspended users without any hesitation.
621 $rs = $DB->get_recordset_select('sessions', "userid IN (SELECT id FROM {user} WHERE deleted <> 0 OR suspended <> 0)", array(), 'id DESC', 'id, sid');
622 foreach ($rs as $session) {
623 self::kill_session($session->sid);
624 }
625 $rs->close();
626
627 // Kill sessions of users with disabled plugins.
628 $auth_sequence = get_enabled_auth_plugins(true);
629 $auth_sequence = array_flip($auth_sequence);
630 unset($auth_sequence['nologin']); // No login means user cannot login.
631 $auth_sequence = array_flip($auth_sequence);
632
633 list($notplugins, $params) = $DB->get_in_or_equal($auth_sequence, SQL_PARAMS_QM, '', false);
634 $rs = $DB->get_recordset_select('sessions', "userid IN (SELECT id FROM {user} WHERE auth $notplugins)", $params, 'id DESC', 'id, sid');
635 foreach ($rs as $session) {
636 self::kill_session($session->sid);
637 }
638 $rs->close();
639
640 // Now get a list of time-out candidates - real users only.
641 $sql = "SELECT u.*, s.sid, s.timecreated AS s_timecreated, s.timemodified AS s_timemodified
642 FROM {user} u
643 JOIN {sessions} s ON s.userid = u.id
644 WHERE s.timemodified < :purgebefore AND u.id <> :guestid";
645 $params = array('purgebefore' => (time() - $maxlifetime), 'guestid'=>$CFG->siteguest);
646
647 $authplugins = array();
648 foreach ($auth_sequence as $authname) {
649 $authplugins[$authname] = get_auth_plugin($authname);
650 }
651 $rs = $DB->get_recordset_sql($sql, $params);
652 foreach ($rs as $user) {
653 foreach ($authplugins as $authplugin) {
654 /** @var \auth_plugin_base $authplugin*/
655 if ($authplugin->ignore_timeout_hook($user, $user->sid, $user->s_timecreated, $user->s_timemodified)) {
656 continue;
657 }
658 }
659 self::kill_session($user->sid);
660 }
661 $rs->close();
662
663 // Delete expired sessions for guest user account, give them larger timeout, there is no security risk here.
664 $params = array('purgebefore' => (time() - ($maxlifetime * 5)), 'guestid'=>$CFG->siteguest);
665 $rs = $DB->get_recordset_select('sessions', 'userid = :guestid AND timemodified < :purgebefore', $params, 'id DESC', 'id, sid');
666 foreach ($rs as $session) {
667 self::kill_session($session->sid);
668 }
669 $rs->close();
670
671 // Delete expired sessions for userid = 0 (not logged in), better kill them asap to release memory.
672 $params = array('purgebefore' => (time() - $maxlifetime));
673 $rs = $DB->get_recordset_select('sessions', 'userid = 0 AND timemodified < :purgebefore', $params, 'id DESC', 'id, sid');
674 foreach ($rs as $session) {
675 self::kill_session($session->sid);
676 }
677 $rs->close();
678
679 // Cleanup letfovers from the first browser access because it may set multiple cookies and then use only one.
680 $params = array('purgebefore' => (time() - 60*3));
681 $rs = $DB->get_recordset_select('sessions', 'userid = 0 AND timemodified = timecreated AND timemodified < :purgebefore', $params, 'id ASC', 'id, sid');
682 foreach ($rs as $session) {
683 self::kill_session($session->sid);
684 }
685 $rs->close();
686
687 } catch (\Exception $ex) {
688 debugging('Error gc-ing sessions: '.$ex->getMessage(), DEBUG_NORMAL, $ex->getTrace());
689 }
690 }
691
692 /**
693 * Is current $USER logged-in-as somebody else?
694 * @return bool
695 */
696 public static function is_loggedinas() {
697 return !empty($_SESSION['USER']->realuser);
698 }
699
700 /**
701 * Returns the $USER object ignoring current login-as session
702 * @return \stdClass user object
703 */
704 public static function get_realuser() {
705 if (self::is_loggedinas()) {
706 return $_SESSION['REALUSER'];
707 } else {
708 return $_SESSION['USER'];
709 }
710 }
711
712 /**
713 * Login as another user - no security checks here.
714 * @param int $userid
715 * @param \context $context
716 * @return void
717 */
718 public static function loginas($userid, \context $context) {
719 global $USER;
720
721 if (self::is_loggedinas()) {
722 return;
723 }
724
725 // Switch to fresh new $SESSION.
726 $_SESSION['REALSESSION'] = $_SESSION['SESSION'];
727 $_SESSION['SESSION'] = new \stdClass();
728
729 // Create the new $USER object with all details and reload needed capabilities.
730 $_SESSION['REALUSER'] = $_SESSION['USER'];
731 $user = get_complete_user_data('id', $userid);
732 $user->realuser = $_SESSION['REALUSER']->id;
733 $user->loginascontext = $context;
734
735 // Let enrol plugins deal with new enrolments if necessary.
736 enrol_check_plugins($user);
737
738 // Create event before $USER is updated.
739 $event = \core\event\user_loggedinas::create(
740 array(
741 'objectid' => $USER->id,
742 'context' => $context,
743 'relateduserid' => $userid,
744 'other' => array(
745 'originalusername' => fullname($USER, true),
746 'loggedinasusername' => fullname($user, true)
747 )
748 )
749 );
750 // Set up global $USER.
751 \core\session\manager::set_user($user);
752 $event->trigger();
753 }
754}