Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

   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   * Shows the result of has_capability for every capability for a user in a context.
  19   *
  20   * @package    core_role
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../config.php');
  26  
  27  $contextid = required_param('contextid', PARAM_INT);
  28  $returnurl  = optional_param('returnurl', null, PARAM_LOCALURL);
  29  
  30  list($context, $course, $cm) = get_context_info_array($contextid);
  31  
  32  $url = new moodle_url('/admin/roles/check.php', array('contextid' => $contextid));
  33  
  34  if ($course) {
  35      $isfrontpage = ($course->id == SITEID);
  36  } else {
  37      $isfrontpage = false;
  38      if ($context->contextlevel == CONTEXT_USER) {
  39          $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
  40          $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
  41          $url->param('courseid', $course->id);
  42          $url->param('userid', $user->id);
  43      } else {
  44          $course = $SITE;
  45      }
  46  }
  47  
  48  // Security first.
  49  require_login($course, false, $cm);
  50  if (!has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override', 'moodle/role:manage'), $context)) {
  51      throw new \moodle_exception('nopermissions', 'error', '', get_string('checkpermissions', 'core_role'));
  52  }
  53  
  54  navigation_node::override_active_url($url);
  55  $pageurl = new moodle_url($url);
  56  if ($returnurl) {
  57      $pageurl->param('returnurl', $returnurl);
  58  }
  59  $PAGE->set_url($pageurl);
  60  
  61  if ($context->contextlevel == CONTEXT_USER and $USER->id != $context->instanceid) {
  62      $PAGE->navbar->includesettingsbase = true;
  63      $PAGE->navigation->extend_for_user($user);
  64      $PAGE->set_context(context_course::instance($course->id));
  65  } else {
  66      $PAGE->set_context($context);
  67  }
  68  
  69  $PAGE->set_context($context);
  70  
  71  $courseid = $course->id;
  72  $contextname = $context->get_context_name();
  73  
  74  // Get the user_selector we will need.
  75  // Teachers within a course just get to see the same list of enrolled users.
  76  // Admins (people with moodle/role:manage) can run this report for any user.
  77  $options = array('accesscontext' => $context);
  78  $userselector = new core_role_check_users_selector('reportuser', $options);
  79  $userselector->set_rows(20);
  80  
  81  // Work out an appropriate page title.
  82  $title = get_string('checkpermissionsin', 'core_role', $contextname);
  83  
  84  $PAGE->set_pagelayout('admin');
  85  if ($context->contextlevel == CONTEXT_BLOCK) {
  86      // Do not show blocks when changing block's settings, it is confusing.
  87      $PAGE->blocks->show_only_fake_blocks(true);
  88  }
  89  $PAGE->set_title($title);
  90  $PAGE->activityheader->disable();
  91  
  92  switch ($context->contextlevel) {
  93      case CONTEXT_SYSTEM:
  94          require_once($CFG->libdir.'/adminlib.php');
  95          admin_externalpage_setup('checkpermissions', '', array('contextid' => $contextid));
  96          break;
  97      case CONTEXT_USER:
  98          $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
  99          $PAGE->set_heading($fullname);
 100          $showroles = 1;
 101          break;
 102      case CONTEXT_COURSECAT:
 103          core_course_category::page_setup();
 104          break;
 105      case CONTEXT_COURSE:
 106          if ($isfrontpage) {
 107              $PAGE->set_heading(get_string('frontpage', 'admin'));
 108          } else {
 109              $PAGE->set_heading($course->fullname);
 110          }
 111          break;
 112      case CONTEXT_MODULE:
 113          $PAGE->set_heading($context->get_context_name(false));
 114          $PAGE->set_cacheable(false);
 115          break;
 116      case CONTEXT_BLOCK:
 117          $PAGE->set_heading($PAGE->course->fullname);
 118          break;
 119  }
 120  
 121  // Get the list of the reported-on user's role assignments - must be after
 122  // the page setup code above, or the language might be wrong.
 123  $reportuser = $userselector->get_selected_user();
 124  if (!is_null($reportuser)) {
 125      $roleassignments = get_user_roles_with_special($context, $reportuser->id);
 126      $rolenames = role_get_names($context);
 127  }
 128  
 129  $PAGE->set_navigation_overflow_state(false);
 130  
 131  echo $OUTPUT->header();
 132  if (in_array($context->contextlevel, [CONTEXT_COURSE, CONTEXT_MODULE, CONTEXT_COURSECAT])) {
 133      echo $OUTPUT->render_participants_tertiary_nav($course);
 134  }
 135  
 136  // Print heading.
 137  echo $OUTPUT->heading($title);
 138  
 139  // If a user has been chosen, show all the permissions for this user.
 140  if (!is_null($reportuser)) {
 141      echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
 142  
 143      if (!empty($roleassignments)) {
 144          echo $OUTPUT->heading(get_string('rolesforuser', 'core_role', fullname($reportuser)), 3);
 145          echo html_writer::start_tag('ul');
 146  
 147          $systemcontext = context_system::instance();
 148          foreach ($roleassignments as $ra) {
 149              $racontext = context::instance_by_id($ra->contextid);
 150              $link = html_writer::link($racontext->get_url(), $racontext->get_context_name());
 151  
 152              $rolename = $rolenames[$ra->roleid]->localname;
 153              if (has_capability('moodle/role:manage', $systemcontext)) {
 154                  $rolename = html_writer::link(new moodle_url('/admin/roles/define.php',
 155                          array('action' => 'view', 'roleid' => $ra->roleid)), $rolename);
 156              }
 157  
 158              echo html_writer::tag('li', get_string('roleincontext', 'core_role',
 159                      array('role' => $rolename, 'context' => $link)));
 160          }
 161          echo html_writer::end_tag('ul');
 162      }
 163  
 164      echo $OUTPUT->heading(get_string('permissionsforuser', 'core_role', fullname($reportuser)), 3);
 165      $table = new core_role_check_capability_table($context, $reportuser, $contextname);
 166      $table->display();
 167      echo $OUTPUT->box_end();
 168  
 169      $selectheading = get_string('selectanotheruser', 'core_role');
 170  } else {
 171      $selectheading = get_string('selectauser', 'core_role');
 172  }
 173  
 174  // Show UI for choosing a user to report on.
 175  echo $OUTPUT->box_start('generalbox boxwidthnormal boxaligncenter', 'chooseuser');
 176  echo '<form method="post" action="' . $PAGE->url . '" >';
 177  
 178  // User selector.
 179  echo $OUTPUT->heading('<label for="reportuser">' . $selectheading . '</label>', 3);
 180  $userselector->display();
 181  
 182  // Submit button and the end of the form.
 183  echo '<p id="chooseusersubmit"><input type="submit" value="' . get_string('showthisuserspermissions', 'core_role') . '" ' .
 184       'class="btn btn-primary"/></p>';
 185  echo '</form>';
 186  echo $OUTPUT->box_end();
 187  
 188  // Appropriate back link.
 189  if (!$PAGE->has_secondary_navigation() && $context->contextlevel > CONTEXT_USER) {
 190      echo html_writer::start_tag('div', array('class'=>'backlink'));
 191      if ($returnurl) {
 192          $backurl = new moodle_url($returnurl);
 193      } else {
 194          $backurl = $context->get_url();
 195      }
 196      echo html_writer::link($backurl, get_string('backto', '', $contextname));
 197      echo html_writer::end_tag('div');
 198  }
 199  
 200  echo $OUTPUT->footer();
 201