| 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 | 
            * Configure user factor page
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @package     tool_mfa
  | 
        
        
            | 
            | 
           20 | 
            * @author      Mikhail Golenkov <golenkovm@gmail.com>
  | 
        
        
            | 
            | 
           21 | 
            * @copyright   Catalyst IT
  | 
        
        
            | 
            | 
           22 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           require_once(__DIR__ . '/../../../config.php');
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           use tool_mfa\local\form\setup_factor_form;
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           require_login(null, false);
  | 
        
        
            | 
            | 
           30 | 
           if (isguestuser()) {
  | 
        
        
            | 
            | 
           31 | 
               throw new require_login_exception('error:isguestuser', 'tool_mfa');
  | 
        
        
            | 
            | 
           32 | 
           }
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           $action = optional_param('action', '', PARAM_ALPHANUMEXT);
  | 
        
        
            | 
            | 
           35 | 
           $factor = optional_param('factor', '', PARAM_ALPHANUMEXT);
  | 
        
        
            | 
            | 
           36 | 
           $factorid = optional_param('factorid', '', PARAM_INT);
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
           $params = ['action' => $action, 'factor' => $factor, 'factorid' => $factorid];
  | 
        
        
            | 
            | 
           39 | 
           $currenturl = new moodle_url('/admin/tool/mfa/action.php', $params);
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
           $returnurl = new moodle_url('/admin/tool/mfa/user_preferences.php');
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           if (empty($factor) || empty($action)) {
  | 
        
        
            | 
            | 
           44 | 
               throw new moodle_exception('error:directaccess', 'tool_mfa', $returnurl);
  | 
        
        
            | 
            | 
           45 | 
           }
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
           if (!\tool_mfa\plugininfo\factor::factor_exists($factor)) {
  | 
        
        
            | 
            | 
           48 | 
               throw new moodle_exception('error:factornotfound', 'tool_mfa', $returnurl, $factor);
  | 
        
        
            | 
            | 
           49 | 
           }
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
           if (!in_array($action, \tool_mfa\plugininfo\factor::get_factor_actions())) {
  | 
        
        
            | 
            | 
           52 | 
               throw new moodle_exception('error:actionnotfound', 'tool_mfa', $returnurl, $action);
  | 
        
        
            | 
            | 
           53 | 
           }
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
           if (!empty($factorid) && !\tool_mfa\manager::is_factorid_valid($factorid, $USER)) {
  | 
        
        
            | 
            | 
           56 | 
               throw new moodle_exception('error:incorrectfactorid', 'tool_mfa', $returnurl, $factorid);
  | 
        
        
            | 
            | 
           57 | 
           }
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
           $factorobject = \tool_mfa\plugininfo\factor::get_factor($factor);
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
           $context = context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           62 | 
           $PAGE->set_context($context);
  | 
        
        
            | 
            | 
           63 | 
           $PAGE->set_url('/admin/tool/mfa/action.php');
  | 
        
        
            | 
            | 
           64 | 
           $PAGE->set_pagelayout('standard');
  | 
        
        
            | 
            | 
           65 | 
           $PAGE->set_cacheable(false);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
           if ($node = $PAGE->settingsnav->find('usercurrentsettings', null)) {
  | 
        
        
            | 
            | 
           68 | 
               $PAGE->navbar->add($node->get_content(), $node->action());
  | 
        
        
            | 
            | 
           69 | 
           }
  | 
        
        
            | 
            | 
           70 | 
           $PAGE->navbar->add(get_string('preferences:header', 'tool_mfa'), new \moodle_url('/admin/tool/mfa/user_preferences.php'));
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
           switch ($action) {
  | 
        
        
            | 
            | 
           73 | 
               case 'setup':
  | 
        
        
            | 
            | 
           74 | 
                   if (!$factorobject || !$factorobject->has_setup()) {
  | 
        
        
            | 
            | 
           75 | 
                       redirect($returnurl);
  | 
        
        
            | 
            | 
           76 | 
                   }
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
                   $PAGE->set_title(get_string('setupfactor', 'tool_mfa'));
  | 
        
        
            | 
            | 
           79 | 
                   $PAGE->navbar->add($factorobject->get_setup_string());
  | 
        
        
            | 
            | 
           80 | 
                   $OUTPUT = $PAGE->get_renderer('tool_mfa');
  | 
        
        
            | 
            | 
           81 | 
                   $form = new setup_factor_form($currenturl, ['factorname' => $factor]);
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
                   if ($form->is_submitted()) {
  | 
        
        
            | 
            | 
           84 | 
                       $form->is_validated();
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
                       if ($form->is_cancelled()) {
  | 
        
        
            | 
            | 
           87 | 
                           $factorobject->setup_factor_form_is_cancelled($factorid);
  | 
        
        
            | 
            | 
           88 | 
                           redirect($returnurl);
  | 
        
        
            | 
            | 
           89 | 
                       }
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                       if ($data = $form->get_data()) {
  | 
        
        
            | 
            | 
           92 | 
                           $record = $factorobject->setup_user_factor($data);
  | 
        
        
            | 
            | 
           93 | 
                           if (!empty($record)) {
  | 
        
        
            | 
            | 
           94 | 
                               $factorobject->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
  | 
        
        
            | 
            | 
           95 | 
                               $finalurl = new moodle_url($returnurl, ['action' => 'setup', 'factorid' => $record->id]);
  | 
        
        
            | 
            | 
           96 | 
                               redirect($finalurl);
  | 
        
        
            | 
            | 
           97 | 
                           }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                           throw new moodle_exception('error:setupfactor', 'tool_mfa', $returnurl);
  | 
        
        
            | 
            | 
           100 | 
                       }
  | 
        
        
            | 
            | 
           101 | 
                   }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
                   echo $OUTPUT->header();
  | 
        
        
            | 
            | 
           104 | 
                   $form->display();
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   break;
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
               case 'replace':
  | 
        
        
            | 
            | 
           109 | 
                   // Replace works much the same as setup.
  | 
        
        
            | 
            | 
           110 | 
                   if (!$factorobject || !$factorobject->has_replace()) {
  | 
        
        
            | 
            | 
           111 | 
                       redirect($returnurl);
  | 
        
        
            | 
            | 
           112 | 
                   }
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
                   $PAGE->set_title(get_string('replacefactor', 'tool_mfa'));
  | 
        
        
            | 
            | 
           115 | 
                   $PAGE->navbar->add($factorobject->get_setup_string());
  | 
        
        
            | 
            | 
           116 | 
                   $OUTPUT = $PAGE->get_renderer('tool_mfa');
  | 
        
        
            | 
            | 
           117 | 
                   // Use setup factor form, but pass in additional id for replacement.
  | 
        
        
            | 
            | 
           118 | 
                   $form = new setup_factor_form($currenturl, ['factorname' => $factor, 'replaceid' => $factorid]);
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                   if ($form->is_submitted()) {
  | 
        
        
            | 
            | 
           121 | 
                       $form->is_validated();
  | 
        
        
            | 
            | 
           122 | 
              | 
        
        
            | 
            | 
           123 | 
                       if ($form->is_cancelled()) {
  | 
        
        
            | 
            | 
           124 | 
                           $factorobject->setup_factor_form_is_cancelled($factorid);
  | 
        
        
            | 
            | 
           125 | 
                           redirect($returnurl);
  | 
        
        
            | 
            | 
           126 | 
                       }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                       if ($data = $form->get_data()) {
  | 
        
        
            | 
            | 
           129 | 
                           $record = $factorobject->replace_user_factor($data, $factorid);
  | 
        
        
            | 
            | 
           130 | 
                           if (!empty($record)) {
  | 
        
        
            | 
            | 
           131 | 
                               $factorobject->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
  | 
        
        
            | 
            | 
           132 | 
                               $finalurl = new moodle_url($returnurl, ['action' => 'setup', 'factorid' => $record->id]);
  | 
        
        
            | 
            | 
           133 | 
                               redirect($finalurl);
  | 
        
        
            | 
            | 
           134 | 
                           }
  | 
        
        
            | 
            | 
           135 | 
              | 
        
        
            | 
            | 
           136 | 
                           throw new moodle_exception('error:setupfactor', 'tool_mfa', $returnurl);
  | 
        
        
            | 
            | 
           137 | 
                       }
  | 
        
        
            | 
            | 
           138 | 
                   }
  | 
        
        
            | 
            | 
           139 | 
              | 
        
        
            | 
            | 
           140 | 
                   echo $OUTPUT->header();
  | 
        
        
            | 
            | 
           141 | 
                   $form->display();
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
                   break;
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
               case 'revoke':
  | 
        
        
            | 
            | 
           146 | 
                   // Ensure sesskey is valid.
  | 
        
        
            | 
            | 
           147 | 
                   require_sesskey();
  | 
        
        
            | 
            | 
           148 | 
                   $PAGE->set_title(get_string('revokefactor', 'tool_mfa'));
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
                   if (!$factorobject || !$factorobject->has_revoke()) {
  | 
        
        
            | 
            | 
           151 | 
                       throw new moodle_exception('error:revoke', 'tool_mfa', $returnurl);
  | 
        
        
            | 
            | 
           152 | 
                   }
  | 
        
        
            | 
            | 
           153 | 
              | 
        
        
            | 
            | 
           154 | 
                   if ($factorobject->revoke_user_factor($factorid)) {
  | 
        
        
            | 
            | 
           155 | 
                       $finalurl = new moodle_url($returnurl, ['action' => 'revoked', 'factorid' => $factorid]);
  | 
        
        
            | 
            | 
           156 | 
                       redirect($finalurl);
  | 
        
        
            | 
            | 
           157 | 
                   }
  | 
        
        
            | 
            | 
           158 | 
              | 
        
        
            | 
            | 
           159 | 
                   throw new moodle_exception('error:revoke', 'tool_mfa', $returnurl);
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
                   break;
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
               case 'manage':
  | 
        
        
            | 
            | 
           164 | 
              | 
        
        
            | 
            | 
           165 | 
                   $PAGE->set_title(get_string('managefactor', 'tool_mfa'));
  | 
        
        
            | 
            | 
           166 | 
                   $PAGE->navbar->add(get_string('action:manage', 'factor_'.$factor));
  | 
        
        
            | 
            | 
           167 | 
                   $OUTPUT = $PAGE->get_renderer('tool_mfa');
  | 
        
        
            | 
            | 
           168 | 
                   echo $OUTPUT->header();
  | 
        
        
            | 
            | 
           169 | 
                   echo $OUTPUT->heading(get_string('managefactor', 'factor_' . $factorobject->name));
  | 
        
        
            | 
            | 
           170 | 
                   echo $OUTPUT->active_factors($factor);
  | 
        
        
            | 
            | 
           171 | 
                   echo $OUTPUT->single_button($returnurl, get_string('back'));
  | 
        
        
            | 
            | 
           172 | 
                   // JS for modal confirming replace and revoke actions.
  | 
        
        
            | 
            | 
           173 | 
                   $PAGE->requires->js_call_amd('tool_mfa/confirmation_modal', 'init', [$context->id]);
  | 
        
        
            | 
            | 
           174 | 
              | 
        
        
            | 
            | 
           175 | 
                   break;
  | 
        
        
            | 
            | 
           176 | 
              | 
        
        
            | 
            | 
           177 | 
               default:
  | 
        
        
            | 
            | 
           178 | 
                   break;
  | 
        
        
            | 
            | 
           179 | 
           }
  | 
        
        
            | 
            | 
           180 | 
              | 
        
        
            | 
            | 
           181 | 
           echo $OUTPUT->footer();
  |