[ Index ]

PHP Cross Reference of moodle-2.8

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1