Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
/**
1441 ariadna 110
 * Any plugin typically an admin tool can add new bulk user actions
111
 *
112
 * @return array
113
 */
114
function tool_mfa_bulk_user_actions(): array {
115
    if (!has_capability('moodle/site:config', context_system::instance())) {
116
        return [];
117
    }
118
    return [
119
        'tool_mfa_reset_factors' => new action_link(
120
            new moodle_url('/admin/tool/mfa/reset_factor.php'),
121
            get_string('resetfactor', 'tool_mfa'),
122
        ),
123
    ];
124
}
125
 
126
/**
1 efrain 127
 * Serves any files for the guidance page.
128
 *
1441 ariadna 129
 * @param stdClass|null $course
130
 * @param stdClass|null $cm
1 efrain 131
 * @param context $context
132
 * @param string $filearea
133
 * @param array $args
134
 * @param bool $forcedownload
135
 * @param array $options
136
 * @return bool
137
 */
1441 ariadna 138
function tool_mfa_pluginfile(stdClass|null $course, stdClass|null $cm, context $context, string $filearea,
1 efrain 139
    array $args, bool $forcedownload, array $options = []): bool {
140
    // Hardcode to only send guidance files from the top level.
141
    $fs = get_file_storage();
142
    $file = $fs->get_file(
143
        $context->id,
144
        'tool_mfa',
145
        'guidance',
146
        0,
147
        '/',
148
        $args[1]
149
    );
150
    if (!$file) {
151
        send_file_not_found();
152
        return false;
153
    }
154
    send_file($file, $file->get_filename());
155
 
156
    return true;
157
}
158
 
159
/**
160
 * Fragment to confirm a factor action using the confirmation form.
161
 *
162
 * @param array $args Arguments to the form.
163
 * @return null|string The rendered form.
164
 */
165
function tool_mfa_output_fragment_factor_action_confirmation_form(
166
    array $args,
167
): ?string {
168
    // Check args are not empty.
169
    foreach ($args as $key => $arg) {
170
        if (empty($arg)) {
171
            throw new \moodle_exception('missingparam', 'error', '', $key);
172
        }
173
    }
174
 
175
    $customdata = [
176
        'action' => $args['action'],
177
        'factor' => $args['factor'],
178
        'factorid' => $args['factorid'],
179
        'devicename' => $args['devicename'],
180
    ];
181
    // Indicate we are performing a replacement by include the replace id.
182
    if ($args['action'] === 'replace') {
183
        $customdata['replaceid'] = $args['factorid'];
184
    }
185
 
186
    $mform = new tool_mfa\local\form\factor_action_confirmation_form($args['actionurl'], $customdata);
187
 
188
    return $mform->render();
189
}