| 1 | efrain | 1 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 2 | //
 | 
        
           |  |  | 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.
 | 
        
           |  |  | 7 | //
 | 
        
           |  |  | 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.
 | 
        
           |  |  | 12 | //
 | 
        
           |  |  | 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/>.
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | /**
 | 
        
           |  |  | 17 |  * @copyright  2015 Martin Mastny <mastnym@vscht.cz>
 | 
        
           |  |  | 18 |  * @since      3.0
 | 
        
           |  |  | 19 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 20 |  */
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 |  /**
 | 
        
           |  |  | 23 |   * @module core/permissionmanager
 | 
        
           |  |  | 24 |   */
 | 
        
           |  |  | 25 | define(['jquery', 'core/config', 'core/notification', 'core/templates', 'core/yui'],
 | 
        
           |  |  | 26 |     function($, config, notification, templates, Y) {
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 |      /**
 | 
        
           |  |  | 29 |       * Used CSS selectors
 | 
        
           |  |  | 30 |       * @access private
 | 
        
           |  |  | 31 |       */
 | 
        
           |  |  | 32 |     var SELECTORS = {
 | 
        
           |  |  | 33 |         ADDROLE: 'a.allowlink, a.prohibitlink',
 | 
        
           |  |  | 34 |         REMOVEROLE: 'a.preventlink, a.unprohibitlink',
 | 
        
           |  |  | 35 |         UNPROHIBIT: 'a.unprohibitlink'
 | 
        
           |  |  | 36 |         };
 | 
        
           |  |  | 37 |     var rolesloadedevent = $.Event('rolesloaded');
 | 
        
           |  |  | 38 |     var contextid;
 | 
        
           |  |  | 39 |     var contextname;
 | 
        
           |  |  | 40 |     var adminurl;
 | 
        
           |  |  | 41 |     var overideableroles;
 | 
        
           |  |  | 42 |     var panel = null;
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 |     /**
 | 
        
           |  |  | 45 |      * Load all possible roles, which could be assigned from server
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * @access private
 | 
        
           |  |  | 48 |      * @method loadOverideableRoles
 | 
        
           |  |  | 49 |      */
 | 
        
           |  |  | 50 |     var loadOverideableRoles = function() {
 | 
        
           |  |  | 51 |         var params = {
 | 
        
           |  |  | 52 |             contextid: contextid,
 | 
        
           |  |  | 53 |             getroles: 1,
 | 
        
           |  |  | 54 |             sesskey: config.sesskey
 | 
        
           |  |  | 55 |         };
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |         // Need to tell jQuery to expect JSON as the content type may not be correct (MDL-55041).
 | 
        
           |  |  | 58 |         $.post(adminurl + 'roles/ajax.php', params, null, 'json')
 | 
        
           |  |  | 59 |             .done(function(data) {
 | 
        
           |  |  | 60 |               try {
 | 
        
           |  |  | 61 |                   overideableroles = data;
 | 
        
           |  |  | 62 |                   loadOverideableRoles = function() {
 | 
        
           |  |  | 63 |                       $('body').trigger(rolesloadedevent);
 | 
        
           |  |  | 64 |                   };
 | 
        
           |  |  | 65 |                   loadOverideableRoles();
 | 
        
           |  |  | 66 |               } catch (err) {
 | 
        
           |  |  | 67 |                   notification.exception(err);
 | 
        
           |  |  | 68 |               }
 | 
        
           |  |  | 69 |             })
 | 
        
           |  |  | 70 |             .fail(function(jqXHR, status, error) {
 | 
        
           |  |  | 71 |                 notification.exception(error);
 | 
        
           |  |  | 72 |             });
 | 
        
           |  |  | 73 |     };
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     /**
 | 
        
           |  |  | 76 |      * Perform the UI changes after server change
 | 
        
           |  |  | 77 |      *
 | 
        
           |  |  | 78 |      * @access private
 | 
        
           |  |  | 79 |      * @method changePermissions
 | 
        
           |  |  | 80 |      * @param {JQuery} row
 | 
        
           |  |  | 81 |      * @param {int} roleid
 | 
        
           |  |  | 82 |      * @param {string} action
 | 
        
           |  |  | 83 |      */
 | 
        
           |  |  | 84 |     var changePermissions = function(row, roleid, action) {
 | 
        
           |  |  | 85 |         var params = {
 | 
        
           |  |  | 86 |             contextid: contextid,
 | 
        
           |  |  | 87 |             roleid: roleid,
 | 
        
           |  |  | 88 |             sesskey: M.cfg.sesskey,
 | 
        
           |  |  | 89 |             action: action,
 | 
        
           |  |  | 90 |             capability: row.data('name')
 | 
        
           |  |  | 91 |         };
 | 
        
           |  |  | 92 |         $.post(adminurl + 'roles/ajax.php', params, null, 'json')
 | 
        
           |  |  | 93 |         .done(function(data) {
 | 
        
           |  |  | 94 |             var action = data;
 | 
        
           |  |  | 95 |             try {
 | 
        
           |  |  | 96 |                 var templatedata = {rolename: overideableroles[roleid],
 | 
        
           |  |  | 97 |                                     roleid: roleid,
 | 
        
           |  |  | 98 |                                     adminurl: adminurl,
 | 
        
           |  |  | 99 |                                     imageurl: M.util.image_url('t/delete', 'moodle')
 | 
        
           |  |  | 100 |                                     };
 | 
        
           |  |  | 101 |                 switch (action) {
 | 
        
           |  |  | 102 |                     case 'allow':
 | 
        
           |  |  | 103 |                         templatedata.spanclass = 'allowed';
 | 
        
           |  |  | 104 |                         templatedata.linkclass = 'preventlink';
 | 
        
           |  |  | 105 |                         templatedata.action = 'prevent';
 | 
        
           |  |  | 106 |                         templatedata.icon = 't/delete';
 | 
        
           |  |  | 107 |                         templatedata.iconalt = M.util.get_string('deletexrole', 'core_role', overideableroles[roleid]);
 | 
        
           |  |  | 108 |                         break;
 | 
        
           |  |  | 109 |                     case 'prohibit':
 | 
        
           |  |  | 110 |                         templatedata.spanclass = 'forbidden';
 | 
        
           |  |  | 111 |                         templatedata.linkclass = 'unprohibitlink';
 | 
        
           |  |  | 112 |                         templatedata.action = 'unprohibit';
 | 
        
           |  |  | 113 |                         templatedata.icon = 't/delete';
 | 
        
           |  |  | 114 |                         templatedata.iconalt = M.util.get_string('deletexrole', 'core_role', overideableroles[roleid]);
 | 
        
           |  |  | 115 |                         break;
 | 
        
           |  |  | 116 |                     case 'prevent':
 | 
        
           |  |  | 117 |                         row.find('a[data-role-id="' + roleid + '"]').first().closest('.allowed').remove();
 | 
        
           |  |  | 118 |                         return;
 | 
        
           |  |  | 119 |                     case 'unprohibit':
 | 
        
           |  |  | 120 |                         row.find('a[data-role-id="' + roleid + '"]').first().closest('.forbidden').remove();
 | 
        
           |  |  | 121 |                         return;
 | 
        
           |  |  | 122 |                     default:
 | 
        
           |  |  | 123 |                         return;
 | 
        
           |  |  | 124 |                 }
 | 
        
           |  |  | 125 |                 templates.render('core/permissionmanager_role', templatedata)
 | 
        
           |  |  | 126 |                 .done(function(content) {
 | 
        
           |  |  | 127 |                     if (action == 'allow') {
 | 
        
           |  |  | 128 |                         $(content).insertBefore(row.find('.allowmore').first());
 | 
        
           |  |  | 129 |                     } else if (action == 'prohibit') {
 | 
        
           |  |  | 130 |                         $(content).insertBefore(row.find('.prohibitmore').first());
 | 
        
           |  |  | 131 |                         // Remove allowed link
 | 
        
           |  |  | 132 |                         var allowedLink = row.find('.allowedroles').first().find('a[data-role-id="' + roleid + '"]');
 | 
        
           |  |  | 133 |                         if (allowedLink) {
 | 
        
           |  |  | 134 |                             allowedLink.first().closest('.allowed').remove();
 | 
        
           |  |  | 135 |                         }
 | 
        
           |  |  | 136 |                     }
 | 
        
           |  |  | 137 |                     panel.hide();
 | 
        
           |  |  | 138 |                 })
 | 
        
           |  |  | 139 |                 .fail(notification.exception);
 | 
        
           |  |  | 140 |             } catch (err) {
 | 
        
           |  |  | 141 |                 notification.exception(err);
 | 
        
           |  |  | 142 |             }
 | 
        
           |  |  | 143 |         })
 | 
        
           |  |  | 144 |         .fail(function(jqXHR, status, error) {
 | 
        
           |  |  | 145 |             notification.exception(error);
 | 
        
           |  |  | 146 |         });
 | 
        
           |  |  | 147 |     };
 | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 |     /**
 | 
        
           |  |  | 150 |      * Prompts user for selecting a role which is permitted
 | 
        
           |  |  | 151 |      *
 | 
        
           |  |  | 152 |      * @access private
 | 
        
           |  |  | 153 |      * @method handleAddRole
 | 
        
           |  |  | 154 |      * @param {event} e
 | 
        
           |  |  | 155 |      */
 | 
        
           |  |  | 156 |     var handleAddRole = function(e) {
 | 
        
           |  |  | 157 |         e.preventDefault();
 | 
        
           |  |  | 158 |   | 
        
           |  |  | 159 |         var link = $(e.currentTarget);
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |         // TODO: MDL-57778 Convert to core/modal.
 | 
        
           |  |  | 162 |         $('body').one('rolesloaded', function() {
 | 
        
           |  |  | 163 |             Y.use('moodle-core-notification-dialogue', function() {
 | 
        
           |  |  | 164 |                 var action = link.data('action');
 | 
        
           |  |  | 165 |                 var row = link.closest('tr.rolecap');
 | 
        
           |  |  | 166 |                 var confirmationDetails = {
 | 
        
           |  |  | 167 |                     cap: row.data('humanname'),
 | 
        
           |  |  | 168 |                     context: contextname
 | 
        
           |  |  | 169 |                 };
 | 
        
           |  |  | 170 |                 var message = M.util.get_string('role' + action + 'info', 'core_role', confirmationDetails);
 | 
        
           |  |  | 171 |                 if (panel === null) {
 | 
        
           |  |  | 172 |                     panel = new M.core.dialogue({
 | 
        
           |  |  | 173 |                         draggable: true,
 | 
        
           |  |  | 174 |                         modal: true,
 | 
        
           |  |  | 175 |                         closeButton: true,
 | 
        
           |  |  | 176 |                         width: '450px'
 | 
        
           |  |  | 177 |                     });
 | 
        
           |  |  | 178 |                 }
 | 
        
           |  |  | 179 |                 panel.set('headerContent', M.util.get_string('role' + action + 'header', 'core_role'));
 | 
        
           |  |  | 180 |   | 
        
           |  |  | 181 |                 var i, existingrolelinks;
 | 
        
           |  |  | 182 |   | 
        
           |  |  | 183 |                 var roles = [];
 | 
        
           |  |  | 184 |                 switch (action) {
 | 
        
           |  |  | 185 |                     case 'allow':
 | 
        
           |  |  | 186 |                         existingrolelinks = row.find(SELECTORS.REMOVEROLE);
 | 
        
           |  |  | 187 |                         break;
 | 
        
           |  |  | 188 |                     case 'prohibit':
 | 
        
           |  |  | 189 |                         existingrolelinks = row.find(SELECTORS.UNPROHIBIT);
 | 
        
           |  |  | 190 |                         break;
 | 
        
           |  |  | 191 |                 }
 | 
        
           |  |  | 192 |                 for (i in overideableroles) {
 | 
        
           |  |  | 193 |                     var disabled = '';
 | 
        
           |  |  | 194 |                     var disable = existingrolelinks.filter("[data-role-id='" + i + "']").length;
 | 
        
           |  |  | 195 |                     if (disable) {
 | 
        
           |  |  | 196 |                         disabled = 'disabled';
 | 
        
           |  |  | 197 |                     }
 | 
        
           |  |  | 198 |                     var roledetails = {roleid: i, rolename: overideableroles[i], disabled: disabled};
 | 
        
           |  |  | 199 |                     roles.push(roledetails);
 | 
        
           |  |  | 200 |                 }
 | 
        
           |  |  | 201 |   | 
        
           |  |  | 202 |                 templates.render('core/permissionmanager_panelcontent', {message: message, roles: roles})
 | 
        
           |  |  | 203 |                 .done(function(content) {
 | 
        
           |  |  | 204 |                     panel.set('bodyContent', content);
 | 
        
           |  |  | 205 |                     panel.show();
 | 
        
           |  |  | 206 |                     $('div.role_buttons').on('click', 'button', function(e) {
 | 
        
           |  |  | 207 |                         var roleid = $(e.currentTarget).data('role-id');
 | 
        
           |  |  | 208 |                         changePermissions(row, roleid, action);
 | 
        
           |  |  | 209 |                     });
 | 
        
           |  |  | 210 |                 })
 | 
        
           |  |  | 211 |                 .fail(notification.exception);
 | 
        
           |  |  | 212 |   | 
        
           |  |  | 213 |             });
 | 
        
           |  |  | 214 |         });
 | 
        
           |  |  | 215 |         loadOverideableRoles();
 | 
        
           |  |  | 216 |     };
 | 
        
           |  |  | 217 |   | 
        
           |  |  | 218 |     /**
 | 
        
           |  |  | 219 |      * Prompts user when removing permission
 | 
        
           |  |  | 220 |      *
 | 
        
           |  |  | 221 |      * @access private
 | 
        
           |  |  | 222 |      * @method handleRemoveRole
 | 
        
           |  |  | 223 |      * @param {event} e
 | 
        
           |  |  | 224 |      */
 | 
        
           |  |  | 225 |     var handleRemoveRole = function(e) {
 | 
        
           |  |  | 226 |         e.preventDefault();
 | 
        
           |  |  | 227 |         var link = $(e.currentTarget);
 | 
        
           |  |  | 228 |         $('body').one('rolesloaded', function() {
 | 
        
           |  |  | 229 |             var action = link.data('action');
 | 
        
           |  |  | 230 |             var roleid = link.data('role-id');
 | 
        
           |  |  | 231 |             var row = link.closest('tr.rolecap');
 | 
        
           |  |  | 232 |             var questionDetails = {
 | 
        
           |  |  | 233 |                 role: overideableroles[roleid],
 | 
        
           |  |  | 234 |                 cap: row.data('humanname'),
 | 
        
           |  |  | 235 |                 context: contextname
 | 
        
           |  |  | 236 |             };
 | 
        
           |  |  | 237 |   | 
        
           |  |  | 238 |             notification.confirm(M.util.get_string('confirmunassigntitle', 'core_role'),
 | 
        
           |  |  | 239 |                 M.util.get_string('confirmrole' + action, 'core_role', questionDetails),
 | 
        
           |  |  | 240 |                 M.util.get_string('confirmunassignyes', 'core_role'),
 | 
        
           |  |  | 241 |                 M.util.get_string('confirmunassignno', 'core_role'),
 | 
        
           |  |  | 242 |                 function() {
 | 
        
           |  |  | 243 |                    changePermissions(row, roleid, action);
 | 
        
           |  |  | 244 |                 }
 | 
        
           |  |  | 245 |             );
 | 
        
           |  |  | 246 |          });
 | 
        
           |  |  | 247 |         loadOverideableRoles();
 | 
        
           |  |  | 248 |     };
 | 
        
           |  |  | 249 |   | 
        
           |  |  | 250 |     return /** @alias module:core/permissionmanager */ {
 | 
        
           |  |  | 251 |         /**
 | 
        
           |  |  | 252 |          * Initialize permissionmanager
 | 
        
           |  |  | 253 |          * @access public
 | 
        
           |  |  | 254 |          * @param {Object} args
 | 
        
           |  |  | 255 |          */
 | 
        
           |  |  | 256 |         initialize: function(args) {
 | 
        
           |  |  | 257 |             contextid = args.contextid;
 | 
        
           |  |  | 258 |             contextname = args.contextname;
 | 
        
           |  |  | 259 |             adminurl = args.adminurl;
 | 
        
           |  |  | 260 |             var body = $('body');
 | 
        
           |  |  | 261 |             body.on('click', SELECTORS.ADDROLE, handleAddRole);
 | 
        
           |  |  | 262 |             body.on('click', SELECTORS.REMOVEROLE, handleRemoveRole);
 | 
        
           |  |  | 263 |         }
 | 
        
           |  |  | 264 |     };
 | 
        
           |  |  | 265 | });
 |