usergroupmembershipplugin.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  3. *
  4. * @license GNU AGPL version 3 or any later version
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. (function() {
  21. OCA.WorkflowEngine = OCA.WorkflowEngine || {};
  22. OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
  23. OCA.WorkflowEngine.Plugins.UserGroupMembershipPlugin = {
  24. getCheck: function() {
  25. return {
  26. 'class': 'OCA\\WorkflowEngine\\Check\\UserGroupMembership',
  27. 'name': t('workflowengine', 'User group membership'),
  28. 'operators': [
  29. {'operator': 'is', 'name': t('workflowengine', 'is member of')},
  30. {'operator': '!is', 'name': t('workflowengine', 'is not member of')}
  31. ]
  32. };
  33. },
  34. render: function(element, check) {
  35. if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\UserGroupMembership') {
  36. return;
  37. }
  38. $(element).css('width', '400px');
  39. $(element).select2({
  40. ajax: {
  41. url: OC.generateUrl('settings/users/groups'),
  42. dataType: 'json',
  43. quietMillis: 100,
  44. data: function (term) {
  45. return {
  46. pattern: term, //search term
  47. filterGroups: true,
  48. sortGroups: 2 // by groupname
  49. };
  50. },
  51. results: function (response) {
  52. // TODO improve error case
  53. if (response.data === undefined) {
  54. console.error('Failure happened', response);
  55. return;
  56. }
  57. var results = [];
  58. // add admin groups
  59. $.each(response.data.adminGroups, function(id, group) {
  60. results.push({ id: group.id });
  61. });
  62. // add groups
  63. $.each(response.data.groups, function(id, group) {
  64. results.push({ id: group.id });
  65. });
  66. // TODO once limit and offset is implemented for groups we should paginate the search results
  67. return {
  68. results: results,
  69. more: false
  70. };
  71. }
  72. },
  73. initSelection: function (element, callback) {
  74. callback({id: element.val()});
  75. },
  76. formatResult: function (element) {
  77. return '<span>' + escapeHTML(element.id) + '</span>';
  78. },
  79. formatSelection: function (element) {
  80. return '<span title="'+escapeHTML(element.id)+'">'+escapeHTML(element.id)+'</span>';
  81. }
  82. });
  83. }
  84. };
  85. })();
  86. OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.UserGroupMembershipPlugin);