mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
loginas.php
93 lines (67 loc) · 3.13 KB
/
loginas.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
<?php // $Id$
// Allows a teacher/admin to login as another user (in stealth mode)
require_once("../config.php");
require_once("lib.php");
require_variable($id); // course id
optional_variable($user); // login as this user
optional_variable($return); // return to the page we came from
if (! $course = get_record("course", "id", $id)) {
error("Course ID was incorrect");
}
if ($course->category) {
require_login($course->id);
}
if ($USER->realuser) { /// Reset user back to their real self
$USER = get_user_info_from_db("id", $USER->realuser);
$USER->loggedin = true;
$USER->site = $CFG->wwwroot;
if (isset($SESSION->oldcurrentgroup)) { // Restore previous "current group"
$SESSION->currentgroup[$course->id] = $SESSION->oldcurrentgroup;
unset($SESSION->oldcurrentgroup);
}
if (isset($SESSION->oldtimeaccess)) { // Restore previous timeaccess settings
$USER->timeaccess = $SESSION->oldtimeaccess;
unset($SESSION->oldtimeaccess);
}
if ($return) { /// That's all we wanted to do, so let's go back
redirect($_SERVER["HTTP_REFERER"]);
exit;
}
}
// $user must be defined to go on
if (!isteacher($course->id)) {
error("Only teachers can use this page!");
}
check_for_restricted_user($USER->username, "$CFG->wwwroot/user/view.php?id=$user&course=$course->id");
if ($course->category and !isstudent($course->id, $user) and !isadmin()) {
error("This student is not in this course!");
}
if (iscreator($user)) {
error("You can not login as this person!");
}
// Remember current timeaccess settings for later
if (isset($USER->timeaccess)) {
$SESSION->oldtimeaccess = $USER->timeaccess;
}
// Login as this student and return to course home page.
$teacher_name = fullname($USER, true);
$teacher_id = "$USER->id";
$USER = get_user_info_from_db("id", $user); // Create the new USER object with all details
$USER->loggedin = true;
$USER->site = $CFG->wwwroot;
$USER->realuser = $teacher_id;
$USER->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
if (isset($SESSION->currentgroup[$course->id])) { // Remember current setting for later
$SESSION->oldcurrentgroup = $SESSION->currentgroup[$course->id];
unset($SESSION->currentgroup[$course->id]);
}
set_moodle_cookie($USER->username);
$student_name = fullname($USER, true);
add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&user=$user", "$teacher_name -> $student_name");
$strloginas = get_string("loginas");
$strloggedinas = get_string("loggedinas", "", $student_name);
print_header("$course->fullname: $strloginas $student_name", "$course->fullname",
"<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->
$strloginas $student_name");
notice($strloggedinas, "$CFG->wwwroot/course/view.php?id=$course->id");
?>