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

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