1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * @class permissionmanager
18 * @copyright 2015 Martin Mastny <mastnym@vscht.cz>
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @module admin/permissionmanager
26 define(['jquery', 'core/config', 'core/notification', 'core/templates', 'core/yui'],
27 function($, config, notification, templates, Y) {
34 ADDROLE: 'a.allowlink, a.prohibitlink',
35 REMOVEROLE: 'a.preventlink, a.unprohibitlink',
36 UNPROHIBIT: 'a.unprohibitlink'
38 var rolesloadedevent = $.Event('rolesloaded');
46 * Load all possible roles, which could be assigned from server
49 * @method loadOverideableRoles
51 var loadOverideableRoles = function() {
55 sesskey: config.sesskey
58 // Need to tell jQuery to expect JSON as the content type may not be correct (MDL-55041).
59 $.post(adminurl + 'roles/ajax.php', params, null, 'json')
60 .done(function(data) {
62 overideableroles = data;
63 loadOverideableRoles = function() {
64 $('body').trigger(rolesloadedevent);
66 loadOverideableRoles();
68 notification.exception(err);
71 .fail(function(jqXHR, status, error) {
72 notification.exception(error);
77 * Perform the UI changes after server change
80 * @method changePermissions
83 * @param {string} action
85 var changePermissions = function(row, roleid, action) {
89 sesskey: M.cfg.sesskey,
91 capability: row.data('name')
93 $.post(adminurl + 'roles/ajax.php', params, null, 'json')
94 .done(function(data) {
97 var templatedata = {rolename: overideableroles[roleid],
100 imageurl: M.util.image_url('t/delete', 'moodle')
104 templatedata.spanclass = 'allowed';
105 templatedata.linkclass = 'preventlink';
106 templatedata.action = 'prevent';
107 templatedata.icon = 't/delete';
110 templatedata.spanclass = 'forbidden';
111 templatedata.linkclass = 'unprohibitlink';
112 templatedata.action = 'unprohibit';
113 templatedata.icon = 't/delete';
116 row.find('a[data-role-id="' + roleid + '"]').first().closest('.allowed').remove();
119 row.find('a[data-role-id="' + roleid + '"]').first().closest('.forbidden').remove();
124 templates.render('core/permissionmanager_role', templatedata)
125 .done(function(content) {
126 if (action == 'allow') {
127 $(content).insertBefore(row.find('.allowmore:first'));
128 } else if (action == 'prohibit') {
129 $(content).insertBefore(row.find('.prohibitmore:first'));
130 // Remove allowed link
131 var allowedLink = row.find('.allowedroles').first().find('a[data-role-id="' + roleid + '"]');
133 allowedLink.first().closest('.allowed').remove();
138 .fail(notification.exception);
140 notification.exception(err);
143 .fail(function(jqXHR, status, error) {
144 notification.exception(error);
149 * Prompts user for selecting a role which is permitted
152 * @method handleAddRole
155 var handleAddRole = function(e) {
158 // TODO: MDL-57778 Convert to core/modal.
159 Y.use('moodle-core-notification-dialogue', function() {
160 $('body').one('rolesloaded', function() {
161 var link = $(e.currentTarget);
162 var action = link.data('action');
163 var row = link.closest('tr.rolecap');
164 var confirmationDetails = {
165 cap: row.data('humanname'),
168 var message = M.util.get_string('role' + action + 'info', 'core_role', confirmationDetails);
169 if (panel === null) {
170 panel = new M.core.dialogue({
177 panel.set('headerContent', M.util.get_string('role' + action + 'header', 'core_role'));
179 var i, existingrolelinks;
184 existingrolelinks = row.find(SELECTORS.REMOVEROLE);
187 existingrolelinks = row.find(SELECTORS.UNPROHIBIT);
190 for (i in overideableroles) {
192 var disable = existingrolelinks.filter("[data-role-id='" + i + "']").length;
194 disabled = 'disabled';
196 var roledetails = {roleid: i, rolename: overideableroles[i], disabled: disabled};
197 roles.push(roledetails);
200 templates.render('core/permissionmanager_panelcontent', {message: message, roles: roles})
201 .done(function(content) {
202 panel.set('bodyContent', content);
204 $('div.role_buttons').delegate('input', 'click', function(e) {
205 var roleid = $(e.currentTarget).data('role-id');
206 changePermissions(row, roleid, action);
209 .fail(notification.exception);
213 loadOverideableRoles();
217 * Prompts user when removing permission
220 * @method handleRemoveRole
223 var handleRemoveRole = function(e) {
225 $('body').one('rolesloaded', function() {
226 var link = $(e.currentTarget);
227 var action = link.data('action');
228 var roleid = link.data('role-id');
229 var row = link.closest('tr.rolecap');
230 var questionDetails = {
231 role: overideableroles[roleid],
232 cap: row.data('humanname'),
236 notification.confirm(M.util.get_string('confirmunassigntitle', 'core_role'),
237 M.util.get_string('confirmrole' + action, 'core_role', questionDetails),
238 M.util.get_string('confirmunassignyes', 'core_role'),
239 M.util.get_string('confirmunassignno', 'core_role'),
241 changePermissions(row, roleid, action);
245 loadOverideableRoles();
248 return /** @alias module:core/permissionmanager */ {
250 * Initialize permissionmanager
252 * @param {Object} args
254 initialize: function(args) {
255 contextid = args.contextid;
256 contextname = args.contextname;
257 adminurl = args.adminurl;
258 var body = $('body');
259 body.delegate(SELECTORS.ADDROLE, 'click', handleAddRole);
260 body.delegate(SELECTORS.REMOVEROLE, 'click', handleRemoveRole);