lib/amd/src/permissionmanager.js

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