Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Moodle MFA plugin lib
19
 *
20
 * @package     tool_mfa
21
 * @author      Mikhail Golenkov <golenkovm@gmail.com>
22
 * @copyright   Catalyst IT
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
use core\context;
27
 
28
/**
29
 * Main hook.
30
 *
31
 * e.g. Add permissions logic across a site or course
32
 *
33
 * @param mixed $courseorid
34
 * @param mixed $autologinguest
35
 * @param mixed $cm
36
 * @param mixed $setwantsurltome
37
 * @param mixed $preventredirect
38
 * @return void
39
 * @throws \moodle_exception
40
 */
41
function tool_mfa_after_require_login($courseorid = null, $autologinguest = null, $cm = null,
42
    $setwantsurltome = null, $preventredirect = null): void {
43
 
44
    global $SESSION;
45
    // Tests for hooks being fired to test patches.
46
    if (PHPUNIT_TEST) {
47
        $SESSION->mfa_login_hook_test = true;
48
    }
49
 
50
    if (empty($SESSION->tool_mfa_authenticated)) {
51
        \tool_mfa\manager::require_auth($courseorid, $autologinguest, $cm, $setwantsurltome, $preventredirect);
52
    }
53
}
54
 
55
/**
56
 * Extends navigation bar and injects MFA Preferences menu to user preferences.
57
 *
58
 * @param navigation_node $navigation
59
 * @param stdClass $user
60
 * @param context_user $usercontext
61
 * @param stdClass $course
62
 * @param context_course $coursecontext
63
 *
64
 * @return mix void or null
65
 * @throws \moodle_exception
66
 */
67
function tool_mfa_extend_navigation_user_settings(navigation_node $navigation, stdClass $user, $usercontext, stdClass $course, $coursecontext) {
68
    global $PAGE;
69
 
70
    // Only inject if user is on the preferences page.
71
    $onpreferencepage = $PAGE->url->compare(new moodle_url('/user/preferences.php'), URL_MATCH_BASE);
72
    if (!$onpreferencepage) {
73
        return null;
74
    }
75
 
76
    if (\tool_mfa\manager::is_ready() && \tool_mfa\manager::possible_factor_setup()) {
77
        $url = new moodle_url('/admin/tool/mfa/user_preferences.php');
78
        $node = navigation_node::create(get_string('preferences:header', 'tool_mfa'), $url,
79
            navigation_node::TYPE_SETTING);
80
        $usernode = $navigation->find('useraccount', navigation_node::TYPE_CONTAINER);
81
        $usernode->add_node($node);
82
    }
83
}
84
 
85
/**
86
 * Triggered as soon as practical on every moodle bootstrap after config has
87
 * been loaded. The $USER object is available at this point too.
88
 *
89
 * @return void
90
 */
91
function tool_mfa_after_config(): void {
92
    global $CFG, $SESSION;
93
 
94
    // Tests for hooks being fired to test patches.
95
    // Store in $CFG, $SESSION not present at this point.
96
    if (PHPUNIT_TEST) {
97
        $CFG->mfa_config_hook_test = true;
98
    }
99
 
100
    // Check for not logged in.
101
    if (isloggedin() && !isguestuser()) {
102
        // If not authenticated, force login required.
103
        if (empty($SESSION->tool_mfa_authenticated)) {
104
            \tool_mfa\manager::require_auth();
105
        }
106
    }
107
}
108
 
109
/**
110
 * Serves any files for the guidance page.
111
 *
112
 * @param stdClass $course
113
 * @param stdClass $cm
114
 * @param context $context
115
 * @param string $filearea
116
 * @param array $args
117
 * @param bool $forcedownload
118
 * @param array $options
119
 * @return bool
120
 */
121
function tool_mfa_pluginfile(stdClass $course, stdClass $cm, context $context, string $filearea,
122
    array $args, bool $forcedownload, array $options = []): bool {
123
    // Hardcode to only send guidance files from the top level.
124
    $fs = get_file_storage();
125
    $file = $fs->get_file(
126
        $context->id,
127
        'tool_mfa',
128
        'guidance',
129
        0,
130
        '/',
131
        $args[1]
132
    );
133
    if (!$file) {
134
        send_file_not_found();
135
        return false;
136
    }
137
    send_file($file, $file->get_filename());
138
 
139
    return true;
140
}
141
 
142
/**
143
 * Fragment to confirm a factor action using the confirmation form.
144
 *
145
 * @param array $args Arguments to the form.
146
 * @return null|string The rendered form.
147
 */
148
function tool_mfa_output_fragment_factor_action_confirmation_form(
149
    array $args,
150
): ?string {
151
    // Check args are not empty.
152
    foreach ($args as $key => $arg) {
153
        if (empty($arg)) {
154
            throw new \moodle_exception('missingparam', 'error', '', $key);
155
        }
156
    }
157
 
158
    $customdata = [
159
        'action' => $args['action'],
160
        'factor' => $args['factor'],
161
        'factorid' => $args['factorid'],
162
        'devicename' => $args['devicename'],
163
    ];
164
    // Indicate we are performing a replacement by include the replace id.
165
    if ($args['action'] === 'replace') {
166
        $customdata['replaceid'] = $args['factorid'];
167
    }
168
 
169
    $mform = new tool_mfa\local\form\factor_action_confirmation_form($args['actionurl'], $customdata);
170
 
171
    return $mform->render();
172
}