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 |
namespace tool_mfa\local\form;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
require_once($CFG->libdir . "/formslib.php");
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Setup factor form
|
|
|
25 |
*
|
|
|
26 |
* @package tool_mfa
|
|
|
27 |
* @author Mikhail Golenkov <golenkovm@gmail.com>
|
|
|
28 |
* @copyright Catalyst IT
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class setup_factor_form extends \moodleform {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* {@inheritDoc}
|
|
|
35 |
* @see moodleform::definition()
|
|
|
36 |
*/
|
|
|
37 |
public function definition(): void {
|
|
|
38 |
$mform = $this->_form;
|
|
|
39 |
// Indicate a factor id that will be replaced with this setup.
|
|
|
40 |
$replaceid = $this->_customdata['replaceid'] ?? null;
|
|
|
41 |
if (!empty($replaceid)) {
|
|
|
42 |
$mform->addelement('hidden', 'replaceid', $replaceid);
|
|
|
43 |
$mform->setType('replaceid', PARAM_INT);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
$factorname = $this->_customdata['factorname'];
|
|
|
47 |
$factor = \tool_mfa\plugininfo\factor::get_factor($factorname);
|
|
|
48 |
$mform = $factor->setup_factor_form_definition($mform);
|
|
|
49 |
$this->xss_whitelist_static_form_elements($mform);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Validates setup_factor form with given factor validation method.
|
|
|
54 |
*
|
|
|
55 |
* @param array $data
|
|
|
56 |
* @param array $files
|
|
|
57 |
* @return array
|
|
|
58 |
*/
|
|
|
59 |
public function validation($data, $files) {
|
|
|
60 |
$errors = parent::validation($data, $files);
|
|
|
61 |
|
|
|
62 |
$factorname = $this->_customdata['factorname'];
|
|
|
63 |
$factor = \tool_mfa\plugininfo\factor::get_factor($factorname);
|
|
|
64 |
$errors += $factor->setup_factor_form_validation($data);
|
|
|
65 |
|
|
|
66 |
return $errors;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Invokes factor setup_factor_form_definition_after_data() method after form data has been set.
|
|
|
71 |
*/
|
|
|
72 |
public function definition_after_data(): void {
|
|
|
73 |
$mform = $this->_form;
|
|
|
74 |
|
|
|
75 |
$factorname = $this->_customdata['factorname'];
|
|
|
76 |
$factor = \tool_mfa\plugininfo\factor::get_factor($factorname);
|
|
|
77 |
$mform = $factor->setup_factor_form_definition_after_data($mform);
|
|
|
78 |
$this->xss_whitelist_static_form_elements($mform);
|
|
|
79 |
$this->add_action_buttons(true, $factor->setup_factor_form_submit_button_string());
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Form elements clean up
|
|
|
84 |
*
|
|
|
85 |
* @param \HTML_QuickForm $mform
|
|
|
86 |
* @return void
|
|
|
87 |
*/
|
|
|
88 |
private function xss_whitelist_static_form_elements($mform): void {
|
|
|
89 |
if (!method_exists('MoodleQuickForm_static', 'set_allow_xss')) {
|
|
|
90 |
return;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$elements = $mform->_elements;
|
|
|
94 |
foreach ($elements as $element) {
|
|
|
95 |
if (is_a($element, 'MoodleQuickForm_static')) {
|
|
|
96 |
$element->set_allow_xss(true);
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
}
|