|
| 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 | + * Master Password Settings |
| 19 | + * |
| 20 | + * @package auth_basic |
| 21 | + * @copyright 2018 Nathan Nguyen <nathannguyen@catalyst-au.nete> |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +require_once(__DIR__.'/../../config.php'); |
| 26 | +require_once($CFG->libdir.'/adminlib.php'); |
| 27 | +require_once('./classes/form/savepassword.php'); |
| 28 | +require_once($CFG->libdir.'/tablelib.php'); |
| 29 | + |
| 30 | +require_login(); |
| 31 | +require_capability('moodle/site:config', context_system::instance()); |
| 32 | + |
| 33 | +admin_externalpage_setup('auth_basic_masterpassword'); |
| 34 | +$thispage = '/auth/basic/masterpassword.php'; |
| 35 | + |
| 36 | +$PAGE->set_url(new moodle_url($thispage)); |
| 37 | + |
| 38 | +echo $OUTPUT->header(); |
| 39 | +echo $OUTPUT->heading(get_string('masterpassword', 'auth_basic')); |
| 40 | + |
| 41 | +if (!isset($CFG->auth_basic_enabled_master_password)) { |
| 42 | + echo $OUTPUT->notification(get_string('masterpassword_not_enabled', 'auth_basic'), 'notifyproblem'); |
| 43 | +} |
| 44 | + |
| 45 | +if (!is_enabled_auth('basic')) { |
| 46 | + echo $OUTPUT->notification(get_string('auth_basic_not_enabled', 'auth_basic'), 'notifyproblem'); |
| 47 | +} |
| 48 | + |
| 49 | +$whitelist = $CFG->auth_basic_whitelist_ips; |
| 50 | +if (!isset($whitelist)) { |
| 51 | + echo $OUTPUT->notification(get_string('whitelist_not_set', 'auth_basic'), 'notifyproblem'); |
| 52 | +} else { |
| 53 | + echo $OUTPUT->notification(get_string('whitelistonly', 'auth_basic', $whitelist), 'notifysuccess'); |
| 54 | +} |
| 55 | + |
| 56 | +// Save Password Form. |
| 57 | +$password = time().uniqid(); |
| 58 | +$mform = new savepassword(null, array('password' => $password)); |
| 59 | + |
| 60 | +if ($formdata = $mform->get_data()) { |
| 61 | + $record = new stdClass(); |
| 62 | + $record->password = $formdata->password; |
| 63 | + $record->userid = $USER->id; |
| 64 | + $record->usage = 0; |
| 65 | + $record->timecreated = time(); |
| 66 | + $record->timeexpired = time() + DAYSECS; |
| 67 | + $DB->insert_record('auth_basic_master_password', $record); |
| 68 | + redirect(new moodle_url($thispage)); |
| 69 | +} else { |
| 70 | + $mform->set_data($toform); |
| 71 | + $mform->display(); |
| 72 | +} |
| 73 | + |
| 74 | +// Master Password Table. |
| 75 | +echo $OUTPUT->heading(get_string('generated_masterpassword', 'auth_basic')); |
| 76 | + |
| 77 | +$sql = "SELECT COUNT(*) FROM {auth_basic_master_password}"; |
| 78 | +$record = $DB->get_record_sql($sql); |
| 79 | +if (!empty($record) && ($total = $record->count) > 0) { |
| 80 | + $perpage = 20; |
| 81 | + $page = optional_param('page', 0, PARAM_INT); |
| 82 | + $offset = $page * $perpage; |
| 83 | + |
| 84 | + $sql = "SELECT p.*, u.firstname, u.lastname |
| 85 | + FROM {auth_basic_master_password} p |
| 86 | + JOIN {user} u on u.id = p.userid |
| 87 | + ORDER BY p.timecreated DESC"; |
| 88 | + $records = $DB->get_records_sql($sql, null, $offset, $perpage); |
| 89 | + |
| 90 | + if (!empty($records) && count($records) > 0) { |
| 91 | + $table = new html_table(); |
| 92 | + $table->attributes['class'] = 'generaltable catalystadmins'; |
| 93 | + $table->head = array( |
| 94 | + get_string('username', 'auth_basic'), |
| 95 | + get_string('password', 'auth_basic'), |
| 96 | + get_string('usage', 'auth_basic'), |
| 97 | + get_string('timecreated', 'auth_basic'), |
| 98 | + get_string('timeexpired', 'auth_basic'), |
| 99 | + ); |
| 100 | + |
| 101 | + foreach ($records as $record) { |
| 102 | + $row = array(); |
| 103 | + $row[] = fullname($record); |
| 104 | + if ($record->userid == $USER->id) { |
| 105 | + $row[] = $record->password; |
| 106 | + } else { |
| 107 | + $row[] = "*hidden*"; |
| 108 | + } |
| 109 | + |
| 110 | + $row[] = $record->usage; |
| 111 | + $row[] = userdate($record->timecreated, get_string('strftimerecentfull')); |
| 112 | + $row[] = userdate($record->timeexpired, get_string('strftimerecentfull')); |
| 113 | + $table->data[] = $row; |
| 114 | + } |
| 115 | + echo html_writer::table($table); |
| 116 | + $baseurl = new moodle_url('/auth/basic/masterpassword.php', array('perpage' => $perpage)); |
| 117 | + echo $OUTPUT->paging_bar($total, $page, $perpage, $baseurl); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +echo $OUTPUT->footer(); |
0 commit comments