[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/roles/ -> permissions.php (source)

   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  $return     = optional_param('return', null, PARAM_ALPHANUMEXT);
  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  $PAGE->set_url($url);
  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_user::instance($user->id));
  65  } else {
  66      $PAGE->set_context($context);
  67  }
  68  
  69  $courseid = $course->id;
  70  
  71  
  72  // These are needed early because of tabs.php.
  73  $assignableroles = get_assignable_roles($context, ROLENAME_BOTH);
  74  list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context, ROLENAME_BOTH, true);
  75  if ($capability) {
  76      $capability = $DB->get_record('capabilities', array('name'=>$capability), '*', MUST_EXIST);
  77  }
  78  
  79  $allowoverrides     = has_capability('moodle/role:override', $context);
  80  $allowsafeoverrides = has_capability('moodle/role:safeoverride', $context);
  81  
  82  $contextname = $context->get_context_name();
  83  $title = get_string('permissionsincontext', 'core_role', $contextname);
  84  $straction = get_string('permissions', 'core_role'); // Used by tabs.php.
  85  $currenttab = 'permissions';
  86  
  87  $PAGE->set_pagelayout('admin');
  88  $PAGE->set_title($title);
  89  switch ($context->contextlevel) {
  90      case CONTEXT_SYSTEM:
  91          print_error('cannotoverridebaserole', 'error');
  92          break;
  93      case CONTEXT_USER:
  94          $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
  95          $PAGE->set_heading($fullname);
  96          $showroles = 1;
  97          break;
  98      case CONTEXT_COURSECAT:
  99          $PAGE->set_heading($SITE->fullname);
 100          break;
 101      case CONTEXT_COURSE:
 102          if ($isfrontpage) {
 103              $PAGE->set_heading(get_string('frontpage', 'admin'));
 104          } else {
 105              $PAGE->set_heading($course->fullname);
 106          }
 107          break;
 108      case CONTEXT_MODULE:
 109          $PAGE->set_heading($context->get_context_name(false));
 110          $PAGE->set_cacheable(false);
 111          break;
 112      case CONTEXT_BLOCK:
 113          $PAGE->set_heading($PAGE->course->fullname);
 114          break;
 115  }
 116  
 117  // Handle confirmations and actions.
 118  // We have a capability and overrides are allowed or safe overrides are allowed and this is safe.
 119  if ($capability && ($allowoverrides || ($allowsafeoverrides && is_safe_capability($capability)))) {
 120      // If we already know the the role ID, it is overrideable, and we are setting prevent or unprohibit.
 121      if (isset($overridableroles[$roleid]) && ($prevent || $unprohibit)) {
 122          // We are preventing.
 123          if ($prevent) {
 124              if ($confirm && data_submitted() && confirm_sesskey()) {
 125                  role_change_permission($roleid, $context, $capability->name, CAP_PREVENT);
 126                  redirect($PAGE->url);
 127  
 128              } else {
 129                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
 130                  $message = get_string('confirmroleprevent', 'core_role', $a);
 131                  $continueurl = new moodle_url($PAGE->url,
 132                      array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'prevent'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
 133              }
 134          }
 135          // We are unprohibiting.
 136          if ($unprohibit) {
 137              if ($confirm && data_submitted() && confirm_sesskey()) {
 138                  role_change_permission($roleid, $context, $capability->name, CAP_INHERIT);
 139                  redirect($PAGE->url);
 140              } else {
 141                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'role'=>$overridableroles[$roleid], 'context'=>$contextname);
 142                  $message = get_string('confirmroleunprohibit', 'core_role', $a);
 143                  $continueurl = new moodle_url($PAGE->url,
 144                      array('contextid'=>$context->id, 'roleid'=>$roleid, 'capability'=>$capability->name, 'unprohibit'=>1, 'sesskey'=>sesskey(), 'confirm'=>1));
 145              }
 146          }
 147          // Display and print.
 148          echo $OUTPUT->header();
 149          echo $OUTPUT->heading($title);
 150          echo $OUTPUT->confirm($message, $continueurl, $PAGE->url);
 151          echo $OUTPUT->footer();
 152          die;
 153      }
 154  
 155      if ($allow || $prohibit) {
 156          if ($allow) {
 157              $mform = new core_role_permission_allow_form(null, array($context, $capability, $overridableroles));
 158              if ($mform->is_cancelled()) {
 159                  redirect($PAGE->url);
 160              } else if ($data = $mform->get_data() and !empty($data->roleid)) {
 161                  $roleid = $data->roleid;
 162                  if (isset($overridableroles[$roleid])) {
 163                      role_change_permission($roleid, $context, $capability->name, CAP_ALLOW);
 164                  }
 165                  redirect($PAGE->url);
 166              } else {
 167                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
 168                  $message = get_string('roleallowinfo', 'core_role', $a);
 169              }
 170          }
 171          if ($prohibit) {
 172              $mform = new core_role_permission_prohibit_form(null, array($context, $capability, $overridableroles));
 173              if ($mform->is_cancelled()) {
 174                  redirect($PAGE->url);
 175              } else if ($data = $mform->get_data() and !empty($data->roleid)) {
 176                  $roleid = $data->roleid;
 177                  if (isset($overridableroles[$roleid])) {
 178                      role_change_permission($roleid, $context, $capability->name, CAP_PROHIBIT);
 179                  }
 180                  redirect($PAGE->url);
 181              } else {
 182                  $a = (object)array('cap'=>get_capability_docs_link($capability)." ($capability->name)", 'context'=>$contextname);
 183                  $message = get_string('roleprohibitinfo', 'core_role', $a);
 184              }
 185          }
 186          echo $OUTPUT->header();
 187          echo $OUTPUT->heading($title);
 188          echo $OUTPUT->box($message);
 189          $mform->display();
 190          echo $OUTPUT->footer();
 191          die;
 192      }
 193  }
 194  
 195  echo $OUTPUT->header();
 196  echo $OUTPUT->heading($title);
 197  
 198  $adminurl = new moodle_url('/admin/');
 199  $arguments = array('contextid' => $contextid,
 200                  'contextname' => $contextname,
 201                  'adminurl' => $adminurl->out());
 202  $PAGE->requires->strings_for_js(
 203                                  array('roleprohibitinfo', 'roleprohibitheader', 'roleallowinfo', 'roleallowheader',
 204                                      'confirmunassigntitle', 'confirmroleunprohibit', 'confirmroleprevent', 'confirmunassignyes',
 205                                      'confirmunassignno'), 'core_role');
 206  $PAGE->requires->js_call_amd('core/permissionmanager', 'initialize', array($arguments));
 207  $table = new core_role_permissions_table($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles);
 208  echo $OUTPUT->box_start('generalbox capbox');
 209  // Print link to advanced override page.
 210  if ($overridableroles) {
 211      $overrideurl = new moodle_url('/admin/roles/override.php', array('contextid' => $context->id));
 212      $select = new single_select($overrideurl, 'roleid', $nameswithcounts);
 213      $select->label = get_string('advancedoverride', 'core_role');
 214      echo html_writer::tag('div', $OUTPUT->render($select), array('class'=>'advancedoverride'));
 215  }
 216  $table->display();
 217  echo $OUTPUT->box_end();
 218  
 219  
 220  if ($context->contextlevel > CONTEXT_USER) {
 221  
 222      if ($context->contextlevel === CONTEXT_COURSECAT && $return === 'management') {
 223          $url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid));
 224      } else {
 225          $url = $context->get_url();
 226      }
 227  
 228      echo html_writer::start_tag('div', array('class'=>'backlink'));
 229      echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
 230      echo html_writer::end_tag('div');
 231  }
 232  
 233  echo $OUTPUT->footer($course);


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1