Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core\session;
18
 
19
use core\context;
20
use core\context\course as context_course;
21
use core\context\system as context_system;
22
use core\session\manager as sessionmanager;
23
use stdClass;
24
 
25
/**
26
 * Helper functions for the 'login as' feature.
27
 *
28
 * @package core
29
 * @author Jason den Dulk <jasondendulk@catalyst-au.net>
30
 * @copyright 2025 Catalyst IT
31
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class loginas_helper {
34
    /**
35
     * Determine which context a user can login as another user for, given the requested target user and course. If the
36
     * person cannot login at all, then null will be returned.
37
     *
38
     * @param stdClass $currentuser The current user. Defaults to $USER.
39
     * @param stdClass $loginasuser The user to be logged in as.
40
     * @param stdClass|null $course The course currently being looked at. Applies to those who can only login within a
41
     *                            course context. If null, then only system context will be considered.
42
     * @return context|null Returns the context that the user is able to login as, or null if no context can be found.
43
     */
44
    public static function get_context_user_can_login_as(
45
        stdClass $currentuser,
46
        stdClass $loginasuser,
47
        ?stdClass $course = null
48
    ): ?context {
49
        $systemcontext = context_system::instance();
50
 
51
        if (
52
            $loginasuser->deleted || // Can't login as a user that has been removed.
53
            $currentuser->id == $loginasuser->id || // Pointless for a user to login as himself.
54
            sessionmanager::is_loggedinas() // Already logged in as someone.
55
        ) {
56
            return null;
57
        }
58
 
59
        // Site admins can always login as someone else.
60
        if (is_siteadmin($currentuser)) {
61
            return $systemcontext;
62
        }
63
 
64
        // Non admins can never login as an admin.
65
        if (is_siteadmin($loginasuser)) {
66
            return null;
67
        }
68
 
69
        // Now, we accept anyone who can loginas at the site level, and they can do so at the system level.
70
        if (has_capability('moodle/user:loginas', $systemcontext, $currentuser)) {
71
            return $systemcontext;
72
        }
73
 
74
        if (!empty($course)) {
75
            $coursecontext = context_course::instance($course->id);
76
 
77
            if (
78
                // Reject all that do not have loginas capability.
79
                !has_capability('moodle/user:loginas', $coursecontext, $currentuser) ||
80
                // Reject if user is trying to login as someone with site level loginas powers.
81
                has_capability('moodle/user:loginas', $systemcontext, $loginasuser) ||
82
                // Reject if other is not enrolled.
83
                !is_enrolled($coursecontext, $loginasuser->id)
84
            ) {
85
                return null;
86
            }
87
 
88
            // Check if the users are in the same group.
89
            if (groups_get_course_groupmode($course) == SEPARATEGROUPS &&
90
                !has_capability('moodle/site:accessallgroups', $coursecontext, $currentuser)) {
91
                $samegroup = false;
92
                if ($groups = groups_get_all_groups($course->id, $currentuser->id)) {
93
                    foreach ($groups as $group) {
94
                        if (groups_is_member($group->id, $loginasuser->id)) {
95
                            $samegroup = true;
96
                            break;
97
                        }
98
                    }
99
                }
100
                if (!$samegroup) {
101
                    return null;
102
                }
103
            }
104
 
105
            // Passed all checks.
106
            return $coursecontext;
107
        }
108
 
109
        return null;
110
    }
111
}