weekly release 5.0dev
[moodle.git] / admin / roles / ajax.php
blobd98e379c8c58602c57bb6c1dc016a26ad7087202
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.
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/>.
17 /**
18 * This file processes AJAX requests and returns JSON
20 * This is a server part of yui permissions manager module
22 * @package core_role
23 * @copyright 2015 Martin Mastny
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('AJAX_SCRIPT', true);
28 require(__DIR__ . '/../../config.php');
30 $contextid = required_param('contextid', PARAM_INT);
31 $getroles = optional_param('getroles', 0, PARAM_BOOL);
33 list($context, $course, $cm) = get_context_info_array($contextid);
35 $PAGE->set_context($context);
37 require_login($course, false, $cm);
38 require_capability('moodle/role:review', $context);
39 require_sesskey();
41 $OUTPUT->header();
43 list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context,
44 ROLENAME_BOTH, true);
46 if ($getroles) {
47 echo json_encode($overridableroles);
48 die();
51 $capability = required_param('capability', PARAM_CAPABILITY);
52 $roleid = required_param('roleid', PARAM_INT);
53 $action = required_param('action', PARAM_ALPHA);
55 $capability = $DB->get_record('capabilities', array('name' => $capability), '*', MUST_EXIST);
57 if (!isset($overridableroles[$roleid])) {
58 throw new moodle_exception('invalidarguments');
61 if (!has_capability('moodle/role:override', $context)) {
62 if (!has_capability('moodle/role:safeoverride', $context) || !is_safe_capability($capability)) {
63 require_capability('moodle/role:override', $context);
67 switch ($action) {
68 case 'allow':
69 role_change_permission($roleid, $context, $capability->name, CAP_ALLOW);
70 break;
71 case 'prevent':
72 role_change_permission($roleid, $context, $capability->name, CAP_PREVENT);
73 break;
74 case 'prohibit':
75 role_change_permission($roleid, $context, $capability->name, CAP_PROHIBIT);
76 break;
77 case 'unprohibit':
78 role_change_permission($roleid, $context, $capability->name, CAP_INHERIT);
79 break;
80 default:
81 throw new moodle_exception('invalidarguments');
84 echo json_encode($action);
85 die();