Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Change permissions.
  19   *
  20   * @package    core_role
  21   * @copyright  2009 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require('../../config.php');
  26  
  27  $contextid  = required_param('contextid', PARAM_INT);
  28  
  29  $roleid     = optional_param('roleid', 0, PARAM_INT);
  30  $capability = optional_param('capability', false, PARAM_CAPABILITY);
  31  $confirm    = optional_param('confirm', 0, PARAM_BOOL);
  32  $prevent    = optional_param('prevent', 0, PARAM_BOOL);
  33  $allow      = optional_param('allow', 0, PARAM_BOOL);
  34  $unprohibit = optional_param('unprohibit', 0, PARAM_BOOL);
  35  $prohibit   = optional_param('prohibit', 0, PARAM_BOOL);
  36  $returnurl  = optional_param('returnurl', null, PARAM_LOCALURL);
  37  
  38  list($context, $course, $cm) = get_context_info_array($contextid);
  39  
  40  $url = new moodle_url('/admin/roles/permissions.php', array('contextid' => $contextid));
  41  
  42  if ($course) {
  43      $isfrontpage = ($course->id == SITEID);
  44  } else {
  45      $isfrontpage = false;
  46      if ($context->contextlevel == CONTEXT_USER) {
  47          $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
  48          $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
  49          $url->param('courseid', $course->id);
  50          $url->param('userid', $user->id);
  51      } else {
  52          $course = $SITE;
  53      }
  54  }
  55  
  56  // Security first.
  57  require_login($course, false, $cm);
  58  require_capability('moodle/role:review', $context);
  59  
  60  navigation_node::override_active_url($url);
  61  $pageurl = new moodle_url($url);
  62  if ($returnurl) {
  63      $pageurl->param('returnurl', $returnurl);
  64  }
  65  $PAGE->set_url($pageurl);
  66  
  67  if ($context->contextlevel == CONTEXT_USER and $USER->id != $context->instanceid) {
  68      $PAGE->navbar->includesettingsbase = true;
  69      $PAGE->navigation->extend_for_user($user);
  70      $PAGE->set_context(context_user::instance($user->id));
  71  } else {
  72      $PAGE->set_context($context);
  73  }
  74  
  75  $courseid = $course->id;
  76  
  77  
  78  // These are needed early because of tabs.php.
  79  $assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
  80  list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH, true);
  81  if ($capability) {
  82      $capability = $DB->get_record('capabilities', array('name'=>$capability), '*', MUST_EXIST);
  83  }
  84  
  85  $allowoverrides     = has_capability('moodle/role:override', $context);
  86  $allowsafeoverrides = has_capability('moodle/role:safeoverride', $context);
  87  
  88  $contextname = $context->get_context_name();
  89  $title = get_string('permissionsincontext', 'core_role', $contextname);
  90  $straction = get_string('permissions', 'core_role'); // Used by tabs.php.
  91  $currenttab = 'permissions';
  92  
  93  $PAGE->set_pagelayout('admin');
  94  if ($context->contextlevel == CONTEXT_BLOCK) {
  95      // Do not show blocks when changing block's settings, it is confusing.
  96      $PAGE->blocks->show_only_fake_blocks(true);
  97  }
  98  
  99  $PAGE->set_title($title);
 100  switch ($context->contextlevel) {
 101      case CONTEXT_SYSTEM:
 102          print_error('cannotoverridebaserole', 'error');
 103          break;
 104      case CONTEXT_USER:
 105          $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
 106          $PAGE->set_heading($fullname);
 107          $showroles = 1;
 108          break;
 109      case CONTEXT_COURSECAT:
 110          $PAGE->set_heading($SITE->fullname);
 111          break;
 112      case CONTEXT_COURSE:
 113          if ($isfrontpage) {
 114              $PAGE->set_heading(get_string('frontpage', 'admin'));
 115          } else {
 116              $PAGE->set_heading($course->fullname);
 117          }
 118          break;
 119      case CONTEXT_MODULE:
 120          $PAGE->set_heading($context->get_context_name(false));
 121          $PAGE->set_cacheable(false);
 122          break;
 123      case CONTEXT_BLOCK:
 124          $PAGE->set_heading($PAGE->course->fullname);
 125          break;
 126  }
 127  
 128  // Handle confirmations and actions.
 129  // We have a capability and overrides are allowed or safe overrides are allowed and this is safe.
 130  if ($capability && ($allowoverrides || ($allowsafeoverrides && is_safe_capability($capability)))) {
 131      // If we already know the the role ID, it is overrideable, and we are setting prevent or unprohibit.
 132      if (isset($overridableroles[$roleid]) && ($prevent || $unprohibit)) {
 133          // We are preventing.
 134          if ($prevent) {
 135              if ($confirm && data_submitted() && confirm_sesskey()) {
 136                  role_change_permission($roleid, $context, $capability->name, CAP_PREVENT);
 137                  redirect($PAGE->url);
 138  
 139              } else {
 140                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
 141                  $message = get_string('confirmroleprevent', 'core_role', $a);
 142                  $continueurl = new moodle_url($PAGE->url,
 143                      array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'prevent'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
 144              }
 145          }
 146          // We are unprohibiting.
 147          if ($unprohibit) {
 148              if ($confirm && data_submitted() && confirm_sesskey()) {
 149                  role_change_permission($roleid, $context, $capability->name, CAP_INHERIT);
 150                  redirect($PAGE->url);
 151              } else {
 152                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
 153                  $message = get_string('confirmroleunprohibit', 'core_role', $a);
 154                  $continueurl = new moodle_url($PAGE->url,
 155                      array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'unprohibit'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
 156              }
 157          }
 158          // Display and print.
 159          echo $OUTPUT->header();
 160          echo $OUTPUT->heading($title);
 161          echo $OUTPUT->confirm($message, $continueurl, $PAGE->url);
 162          echo $OUTPUT->footer();
 163          die;
 164      }
 165  
 166      if ($allow || $prohibit) {
 167          if ($allow) {
 168              $mform = new core_role_permission_allow_form(null, array($context, $capability, $overridableroles));
 169              if ($mform->is_cancelled()) {
 170                  redirect($PAGE->url);
 171              } else if ($data = $mform->get_data() and !empty($data->roleid)) {
 172                  $roleid = $data->roleid;
 173                  if (isset($overridableroles[$roleid])) {
 174                      role_change_permission($roleid, $context, $capability->name, CAP_ALLOW);
 175                  }
 176                  redirect($PAGE->url);
 177              } else {
 178                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
 179                  $message = get_string('roleallowinfo', 'core_role', $a);
 180              }
 181          }
 182          if ($prohibit) {
 183              $mform = new core_role_permission_prohibit_form(null, array($context, $capability, $overridableroles));
 184              if ($mform->is_cancelled()) {
 185                  redirect($PAGE->url);
 186              } else if ($data = $mform->get_data() and !empty($data->roleid)) {
 187                  $roleid = $data->roleid;
 188                  if (isset($overridableroles[$roleid])) {
 189                      role_change_permission($roleid, $context, $capability->name, CAP_PROHIBIT);
 190                  }
 191                  redirect($PAGE->url);
 192              } else {
 193                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
 194                  $message = get_string('roleprohibitinfo', 'core_role', $a);
 195              }
 196          }
 197          echo $OUTPUT->header();
 198          echo $OUTPUT->heading($title);
 199          echo $OUTPUT->box($message);
 200          $mform->display();
 201          echo $OUTPUT->footer();
 202          die;
 203      }
 204  }
 205  
 206  echo $OUTPUT->header();
 207  echo $OUTPUT->heading($title);
 208  
 209  $adminurl = new moodle_url('/admin/');
 210  $arguments = array('contextid' => $contextid,
 211                  'contextname' => $contextname,
 212                  'adminurl' => $adminurl->out());
 213  $PAGE->requires->strings_for_js(
 214                                  array('roleprohibitinfo', 'roleprohibitheader', 'roleallowinfo', 'roleallowheader',
 215                                      'confirmunassigntitle', 'confirmroleunprohibit', 'confirmroleprevent', 'confirmunassignyes',
 216                                      'confirmunassignno', 'deletexrole'), 'core_role');
 217  $PAGE->requires->js_call_amd('core/permissionmanager', 'initialize', array($arguments));
 218  $table = new core_role_permissions_table($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles);
 219  echo $OUTPUT->box_start('generalbox capbox');
 220  // Print link to advanced override page.
 221  if ($overridableroles) {
 222      $overrideurl = new moodle_url('/admin/roles/override.php', array('contextid' => $context->id));
 223      $select = new single_select($overrideurl, 'roleid', $nameswithcounts);
 224      $select->label = get_string('advancedoverride', 'core_role');
 225      echo html_writer::tag('div', $OUTPUT->render($select), array('class'=>'advancedoverride'));
 226  }
 227  $table->display();
 228  echo $OUTPUT->box_end();
 229  
 230  
 231  if ($context->contextlevel > CONTEXT_USER) {
 232  
 233      if ($returnurl) {
 234          $url = new moodle_url($returnurl);
 235      } else {
 236          $url = $context->get_url();
 237      }
 238  
 239      echo html_writer::start_tag('div', array('class'=>'backlink'));
 240      echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
 241      echo html_writer::end_tag('div');
 242  }
 243  
 244  echo $OUTPUT->footer($course);