Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Modal for confirming factor actions.
18
 *
19
 * @module     tool_mfa/confirmation_modal
20
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import ModalEvents from 'core/modal_events';
25
import ModalSaveCancel from 'core/modal_save_cancel';
26
import Notification from 'core/notification';
27
import {getString} from 'core/str';
28
import Url from 'core/url';
29
import Fragment from 'core/fragment';
30
import * as Prefetch from 'core/prefetch';
31
 
32
const SELECTORS = {
33
    ACTION: '.mfa-action-button',
34
};
35
 
36
/**
37
 * Entrypoint of the js.
38
 *
39
 * @method init
40
 * @param {Integer} contextId Context ID of the user.
41
 */
42
export const init = (contextId) => {
43
    // Prefetch the language strings.
44
    Prefetch.prefetchStrings('tool_mfa', [
45
        'yesremove',
46
        'yesreplace',
47
    ]);
48
    registerEventListeners(contextId);
49
};
50
 
51
/**
52
 * Register event listeners.
53
 *
54
 * @method registerEventListeners
55
 * @param {Integer} contextId Context ID of the user.
56
 */
57
const registerEventListeners = (contextId) => {
58
    document.addEventListener('click', (e) => {
59
        const action = e.target.closest(SELECTORS.ACTION);
60
        if (action) {
61
            buildModal(action, contextId).catch(Notification.exception);
62
        }
63
    });
64
};
65
 
66
/**
67
 * Build the modal with the provided data.
68
 *
69
 * @method buildModal
70
 * @param {HTMLElement} element The button element.
71
 * @param {Number} contextId Context ID of the user.
72
 */
73
const buildModal = async(element, contextId) => {
74
 
75
    // Prepare data for modal.
76
    const data = {
77
        action: element.getAttribute('data-action'),
78
        factor: element.getAttribute('data-factor'),
79
        factorid: element.getAttribute('data-factorid'),
80
        devicename: element.getAttribute('data-devicename'),
81
        actionurl: Url.relativeUrl('/admin/tool/mfa/action.php'),
82
    };
83
 
84
    // Customise modal depending on action being performed.
85
    if (data.action === 'revoke') {
86
        data.title = await getString('revokefactorconfirmation', 'factor_' + data.factor, data.devicename);
87
        data.buttontext = await getString('yesremove', 'tool_mfa');
88
 
89
    } else if (data.action === 'replace') {
90
        data.title = await getString('replacefactorconfirmation', 'factor_' + data.factor, data.devicename);
91
        data.buttontext = await getString('yesreplace', 'tool_mfa');
92
    }
93
 
94
    const modal = await ModalSaveCancel.create({
95
        title: data.title,
96
        body: Fragment.loadFragment('tool_mfa', 'factor_action_confirmation_form', contextId, data),
97
        show: true,
98
        buttons: {
99
            'save': data.buttontext,
100
            'cancel': getString('cancel', 'moodle'),
101
        },
102
    });
103
 
104
    modal.getRoot().on(ModalEvents.save, () => {
105
        modal.getRoot().find('form').submit();
106
    });
107
 
108
};