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 tool_mfa\table;
18
 
19
use stdClass;
20
use tool_mfa\local\factor\object_factor_base;
21
 
22
/**
23
 * Admin setting for MFA.
24
 *
25
 * @package     tool_mfa
26
 * @copyright   Meirza <meirza.arson@moodle.com>
27
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class admin_setting_managemfa extends \core_admin\table\plugin_management_table {
30
 
31
    #[\Override]
32
    protected function get_plugintype(): string {
33
        return 'factor';
34
    }
35
 
36
    #[\Override]
37
    public function guess_base_url(): void {
38
        $this->define_baseurl(
39
            new \moodle_url('/admin/settings.php', ['section' => 'managemfa'])
40
        );
41
    }
42
 
43
    #[\Override]
44
    protected function get_action_url(array $params = []): \moodle_url {
45
        return new \moodle_url('/admin/tool/mfa/index.php', $params);
46
    }
47
 
48
    #[\Override]
49
    protected function get_column_list(): array {
50
        $columns = [
51
            'name' => get_string('factor', 'tool_mfa'),
52
        ];
53
 
54
        if ($this->supports_disabling()) {
55
            $columns['enabled'] = get_string('pluginenabled', 'core_plugin');
56
        }
57
 
58
        if ($this->supports_ordering()) {
59
            $columns['order'] = get_string('order', 'core');
60
        }
61
 
62
        $columns['weight'] = get_string('weight', 'tool_mfa');
63
        $columns['settings'] = get_string('settings', 'core');
64
 
65
        return $columns;
66
    }
67
 
68
    #[\Override]
69
    protected function col_settings(stdClass $row): string {
70
        if ($settingsurl = $row->plugininfo->get_settings_url()) {
71
            $factor = $row->plugininfo->get_factor($row->plugininfo->name);
72
            return \html_writer::link(
73
                url: $settingsurl,
74
                text: get_string('settings'),
75
                attributes: ["title" => get_string('editfactor', 'tool_mfa', $factor->get_display_name())],
76
            );
77
        }
78
 
79
        return '';
80
    }
81
 
82
    #[\Override]
83
    public function wrap_html_finish() {
84
        $this->output_factor_combinations_table();
85
    }
86
 
87
    /**
88
     * Show the name & short description column content.
89
     *
90
     * @param stdClass $row
91
     * @return string
92
     */
93
    protected function col_name(stdClass $row): string {
94
        global $OUTPUT;
95
        $factor = $row->plugininfo->get_factor($row->plugininfo->name);
96
        $params = [
97
            'name' => $factor->get_display_name(),
98
            'description' => $factor->get_short_description(),
99
        ];
100
 
101
        return $OUTPUT->render_from_template('core_admin/table/namedesc', $params);
102
    }
103
 
104
    /**
105
     * Show the weight column content.
106
     *
107
     * @param stdClass $row
108
     * @return string
109
     */
110
    protected function col_weight(stdClass $row): string {
111
        $factor = $row->plugininfo->get_factor($row->plugininfo->name);
112
        return $factor->get_weight();
113
    }
114
 
115
    /**
116
     * Defines supplementary table that shows available combinations of factors enough for successful authentication.
117
     */
118
    public function output_factor_combinations_table(): void {
119
        global $OUTPUT;
120
 
121
        $factors = \tool_mfa\plugininfo\factor::get_enabled_factors();
122
        $combinations = $this->get_factor_combinations($factors, 0, count($factors) - 1);
123
 
124
        echo \html_writer::tag('h3', get_string('settings:combinations', 'tool_mfa'));
125
 
126
        if (empty($combinations)) {
127
            echo $OUTPUT->notification(get_string('error:notenoughfactors', 'tool_mfa'), 'notifyproblem');
128
            return;
129
        }
130
 
131
        $txt = get_strings(['combination', 'totalweight'], 'tool_mfa');
132
        $table = new \html_table();
133
        $table->id = 'mfacombinations';
134
        $table->attributes['class'] = 'admintable generaltable table table-bordered';
135
        $table->head  = [$txt->combination, $txt->totalweight];
136
        $table->data  = [];
137
 
138
        $factorstringconnector = get_string('connector', 'tool_mfa');
139
        foreach ($combinations as $combination) {
140
            $factorstrings = array_map(static function(object_factor_base $factor): string {
141
                return $factor->get_summary_condition() . ' <sup>' . $factor->get_weight() . '</sup>';
142
            }, $combination['combination']);
143
 
144
            $string = implode(" {$factorstringconnector} ", $factorstrings);
145
            $table->data[] = new \html_table_row([$string, $combination['totalweight']]);
146
        }
147
 
148
        echo \html_writer::table($table);
149
    }
150
 
151
    /**
152
     * Recursive method to get all possible combinations of given factors.
153
     * Output is filtered by combination total weight (should be greater than 100).
154
     *
155
     * @param array $allfactors initial array of factor objects
156
     * @param int $start start position in initial array
157
     * @param int $end end position in initial array
158
     * @param int $totalweight total weight of combination
159
     * @param array $combination combination candidate
160
     * @param array $result array that includes combination total weight and subarray of factors combination
161
     *
162
     * @return array
163
     */
164
    public function get_factor_combinations($allfactors, $start = 0, $end = 0,
165
        $totalweight = 0, $combination = [], $result = []): array {
166
 
167
        if ($totalweight >= 100) {
168
            // Ensure this is a valid combination before appending result.
169
            $valid = true;
170
            foreach ($combination as $factor) {
171
                if (!$factor->check_combination($combination)) {
172
                    $valid = false;
173
                }
174
            }
175
            if ($valid) {
176
                $result[] = ['totalweight' => $totalweight, 'combination' => $combination];
177
            }
178
            return $result;
179
        } else if ($start > $end) {
180
            return $result;
181
        }
182
 
183
        $combinationnext = $combination;
184
        $combinationnext[] = $allfactors[$start];
185
 
186
        $result = $this->get_factor_combinations(
187
            allfactors: $allfactors,
188
            start: $start + 1,
189
            end: $end,
190
            totalweight: $totalweight + $allfactors[$start]->get_weight(),
191
            combination: $combinationnext,
192
            result: $result,
193
        );
194
 
195
        $result = $this->get_factor_combinations(
196
            allfactors: $allfactors,
197
            start: $start + 1,
198
            end: $end,
199
            totalweight: $totalweight,
200
            combination: $combination,
201
            result: $result,
202
        );
203
 
204
        return $result;
205
    }
206
}