You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acl.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * ACL plugin script
  3. */
  4. if (window.rcmail) {
  5. rcmail.addEventListener('init', function() {
  6. if (rcmail.gui_objects.acltable) {
  7. rcmail.acl_list_init();
  8. // enable autocomplete on user input
  9. if (rcmail.env.acl_users_source) {
  10. var inst = rcmail.is_framed() ? parent.rcmail : rcmail;
  11. inst.init_address_input_events($('#acluser'), {action:'settings/plugin.acl-autocomplete'});
  12. // pass config settings and localized texts to autocomplete context
  13. inst.set_env({ autocomplete_max:rcmail.env.autocomplete_max, autocomplete_min_length:rcmail.env.autocomplete_min_length });
  14. inst.add_label('autocompletechars', rcmail.labels.autocompletechars);
  15. inst.add_label('autocompletemore', rcmail.labels.autocompletemore);
  16. // fix inserted value
  17. inst.addEventListener('autocomplete_insert', function(e) {
  18. if (e.field.id != 'acluser')
  19. return;
  20. e.field.value = e.insert.replace(/[ ,;]+$/, '');
  21. });
  22. }
  23. }
  24. rcmail.enable_command('acl-create', 'acl-save', 'acl-cancel', 'acl-mode-switch', true);
  25. rcmail.enable_command('acl-delete', 'acl-edit', false);
  26. if (rcmail.env.acl_advanced)
  27. $('#acl-switch').addClass('selected');
  28. });
  29. }
  30. // Display new-entry form
  31. rcube_webmail.prototype.acl_create = function()
  32. {
  33. this.acl_init_form();
  34. }
  35. // Display ACL edit form
  36. rcube_webmail.prototype.acl_edit = function()
  37. {
  38. // @TODO: multi-row edition
  39. var id = this.acl_list.get_single_selection();
  40. if (id)
  41. this.acl_init_form(id);
  42. }
  43. // ACL entry delete
  44. rcube_webmail.prototype.acl_delete = function()
  45. {
  46. var users = this.acl_get_usernames();
  47. if (users && users.length && confirm(this.get_label('acl.deleteconfirm'))) {
  48. this.http_post('settings/plugin.acl', {
  49. _act: 'delete',
  50. _user: users.join(','),
  51. _mbox: this.env.mailbox
  52. },
  53. this.set_busy(true, 'acl.deleting'));
  54. }
  55. }
  56. // Save ACL data
  57. rcube_webmail.prototype.acl_save = function()
  58. {
  59. var data, type, rights = '', user = $('#acluser', this.acl_form).val();
  60. $((this.env.acl_advanced ? '#advancedrights :checkbox' : '#simplerights :checkbox'), this.acl_form).map(function() {
  61. if (this.checked)
  62. rights += this.value;
  63. });
  64. if (type = $('input:checked[name=usertype]', this.acl_form).val()) {
  65. if (type != 'user')
  66. user = type;
  67. }
  68. if (!user) {
  69. alert(this.get_label('acl.nouser'));
  70. return;
  71. }
  72. if (!rights) {
  73. alert(this.get_label('acl.norights'));
  74. return;
  75. }
  76. data = {
  77. _act: 'save',
  78. _user: user,
  79. _acl: rights,
  80. _mbox: this.env.mailbox
  81. }
  82. if (this.acl_id) {
  83. data._old = this.acl_id;
  84. }
  85. this.http_post('settings/plugin.acl', data, this.set_busy(true, 'acl.saving'));
  86. }
  87. // Cancel/Hide form
  88. rcube_webmail.prototype.acl_cancel = function()
  89. {
  90. this.ksearch_blur();
  91. this.acl_popup.dialog('close');
  92. }
  93. // Update data after save (and hide form)
  94. rcube_webmail.prototype.acl_update = function(o)
  95. {
  96. // delete old row
  97. if (o.old)
  98. this.acl_remove_row(o.old);
  99. // make sure the same ID doesn't exist
  100. else if (this.env.acl[o.id])
  101. this.acl_remove_row(o.id);
  102. // add new row
  103. this.acl_add_row(o, true);
  104. // hide autocomplete popup
  105. this.ksearch_blur();
  106. // hide form
  107. this.acl_popup.dialog('close');
  108. }
  109. // Switch table display mode
  110. rcube_webmail.prototype.acl_mode_switch = function(elem)
  111. {
  112. this.env.acl_advanced = !this.env.acl_advanced;
  113. this.enable_command('acl-delete', 'acl-edit', false);
  114. this.http_request('settings/plugin.acl', '_act=list'
  115. + '&_mode='+(this.env.acl_advanced ? 'advanced' : 'simple')
  116. + '&_mbox='+urlencode(this.env.mailbox),
  117. this.set_busy(true, 'loading'));
  118. }
  119. // ACL table initialization
  120. rcube_webmail.prototype.acl_list_init = function()
  121. {
  122. var method = this.env.acl_advanced ? 'addClass' : 'removeClass';
  123. $('#acl-switch')[method]('selected');
  124. $(this.gui_objects.acltable)[method]('advanced');
  125. this.acl_list = new rcube_list_widget(this.gui_objects.acltable,
  126. {multiselect: true, draggable: false, keyboard: true});
  127. this.acl_list.addEventListener('select', function(o) { rcmail.acl_list_select(o); })
  128. .addEventListener('dblclick', function(o) { rcmail.acl_list_dblclick(o); })
  129. .addEventListener('keypress', function(o) { rcmail.acl_list_keypress(o); })
  130. .init();
  131. }
  132. // ACL table row selection handler
  133. rcube_webmail.prototype.acl_list_select = function(list)
  134. {
  135. rcmail.enable_command('acl-delete', list.selection.length > 0);
  136. rcmail.enable_command('acl-edit', list.selection.length == 1);
  137. list.focus();
  138. }
  139. // ACL table double-click handler
  140. rcube_webmail.prototype.acl_list_dblclick = function(list)
  141. {
  142. this.acl_edit();
  143. }
  144. // ACL table keypress handler
  145. rcube_webmail.prototype.acl_list_keypress = function(list)
  146. {
  147. if (list.key_pressed == list.ENTER_KEY)
  148. this.command('acl-edit');
  149. else if (list.key_pressed == list.DELETE_KEY || list.key_pressed == list.BACKSPACE_KEY)
  150. if (!this.acl_form || !this.acl_form.is(':visible'))
  151. this.command('acl-delete');
  152. }
  153. // Reloads ACL table
  154. rcube_webmail.prototype.acl_list_update = function(html)
  155. {
  156. $(this.gui_objects.acltable).html(html);
  157. this.acl_list_init();
  158. }
  159. // Returns names of users in selected rows
  160. rcube_webmail.prototype.acl_get_usernames = function()
  161. {
  162. var users = [], n, len, cell, row,
  163. list = this.acl_list,
  164. selection = list.get_selection();
  165. for (n=0, len=selection.length; n<len; n++) {
  166. if (this.env.acl_specials.length && $.inArray(selection[n], this.env.acl_specials) >= 0) {
  167. users.push(selection[n]);
  168. }
  169. else if (row = list.rows[selection[n]]) {
  170. cell = $('td.user', row.obj);
  171. if (cell.length == 1)
  172. users.push(cell.text());
  173. }
  174. }
  175. return users;
  176. }
  177. // Removes ACL table row
  178. rcube_webmail.prototype.acl_remove_row = function(id)
  179. {
  180. var list = this.acl_list;
  181. list.remove_row(id);
  182. list.clear_selection();
  183. // we don't need it anymore (remove id conflict)
  184. $('#rcmrow'+id).remove();
  185. this.env.acl[id] = null;
  186. this.enable_command('acl-delete', list.selection.length > 0);
  187. this.enable_command('acl-edit', list.selection.length == 1);
  188. }
  189. // Adds ACL table row
  190. rcube_webmail.prototype.acl_add_row = function(o, sel)
  191. {
  192. var n, len, ids = [], spec = [], id = o.id, list = this.acl_list,
  193. items = this.env.acl_advanced ? [] : this.env.acl_items,
  194. table = this.gui_objects.acltable,
  195. row = $('thead > tr', table).clone();
  196. // Update new row
  197. $('th', row).map(function() {
  198. var td = $('<td>'),
  199. title = $(this).attr('title'),
  200. cl = this.className.replace(/^acl/, '');
  201. if (title)
  202. td.attr('title', title);
  203. if (items && items[cl])
  204. cl = items[cl];
  205. if (cl == 'user')
  206. td.addClass(cl).append($('<a>').text(o.username));
  207. else
  208. td.addClass(this.className + ' ' + rcmail.acl_class(o.acl, cl)).text('');
  209. $(this).replaceWith(td);
  210. });
  211. row.attr('id', 'rcmrow'+id);
  212. row = row.get(0);
  213. this.env.acl[id] = o.acl;
  214. // sorting... (create an array of user identifiers, then sort it)
  215. for (n in this.env.acl) {
  216. if (this.env.acl[n]) {
  217. if (this.env.acl_specials.length && $.inArray(n, this.env.acl_specials) >= 0)
  218. spec.push(n);
  219. else
  220. ids.push(n);
  221. }
  222. }
  223. ids.sort();
  224. // specials on the top
  225. ids = spec.concat(ids);
  226. // find current id
  227. for (n=0, len=ids.length; n<len; n++)
  228. if (ids[n] == id)
  229. break;
  230. // add row
  231. if (n && n < len) {
  232. $('#rcmrow'+ids[n-1]).after(row);
  233. list.init_row(row);
  234. list.rowcount++;
  235. }
  236. else
  237. list.insert_row(row);
  238. if (sel)
  239. list.select_row(o.id);
  240. }
  241. // Initializes and shows ACL create/edit form
  242. rcube_webmail.prototype.acl_init_form = function(id)
  243. {
  244. var ul, row, td, val = '', type = 'user', li_elements, body = $('body'),
  245. adv_ul = $('#advancedrights'), sim_ul = $('#simplerights'),
  246. name_input = $('#acluser'), type_list = $('#usertype');
  247. if (!this.acl_form) {
  248. var fn = function () { $('input[value="user"]').prop('checked', true); };
  249. name_input.click(fn).keypress(fn);
  250. }
  251. this.acl_form = $('#aclform');
  252. // Hide unused items
  253. if (this.env.acl_advanced) {
  254. adv_ul.show();
  255. sim_ul.hide();
  256. ul = adv_ul;
  257. }
  258. else {
  259. sim_ul.show();
  260. adv_ul.hide();
  261. ul = sim_ul;
  262. }
  263. // initialize form fields
  264. li_elements = $(':checkbox', ul);
  265. li_elements.attr('checked', false);
  266. if (id && (row = this.acl_list.rows[id])) {
  267. row = row.obj;
  268. li_elements.map(function() {
  269. td = $('td.'+this.id, row);
  270. if (td.length && td.hasClass('enabled'))
  271. this.checked = true;
  272. });
  273. if (!this.env.acl_specials.length || $.inArray(id, this.env.acl_specials) < 0)
  274. val = $('td.user', row).text();
  275. else
  276. type = id;
  277. }
  278. // mark read (lrs) rights by default
  279. else {
  280. li_elements.filter(function() { return this.id.match(/^acl([lrs]|read)$/); }).prop('checked', true);
  281. }
  282. name_input.val(val);
  283. $('input[value='+type+']').prop('checked', true);
  284. this.acl_id = id;
  285. var buttons = {}, me = this, body = document.body;
  286. buttons[this.get_label('save')] = function(e) { me.command('acl-save'); };
  287. buttons[this.get_label('cancel')] = function(e) { me.command('acl-cancel'); };
  288. // display it as popup
  289. this.acl_popup = this.show_popup_dialog(
  290. this.acl_form.show(),
  291. id ? this.get_label('acl.editperms') : this.get_label('acl.newuser'),
  292. buttons,
  293. {
  294. button_classes: ['mainaction'],
  295. modal: true,
  296. closeOnEscape: true,
  297. close: function(e, ui) {
  298. (me.is_framed() ? parent.rcmail : me).ksearch_hide();
  299. me.acl_form.appendTo(body).hide();
  300. $(this).remove();
  301. window.focus(); // focus iframe
  302. }
  303. }
  304. );
  305. if (type == 'user')
  306. name_input.focus();
  307. else
  308. $('input:checked', type_list).focus();
  309. }
  310. // Returns class name according to ACL comparison result
  311. rcube_webmail.prototype.acl_class = function(acl1, acl2)
  312. {
  313. var i, len, found = 0;
  314. acl1 = String(acl1);
  315. acl2 = String(acl2);
  316. for (i=0, len=acl2.length; i<len; i++)
  317. if (acl1.indexOf(acl2[i]) > -1)
  318. found++;
  319. if (found == len)
  320. return 'enabled';
  321. else if (found)
  322. return 'partial';
  323. return 'disabled';
  324. }