roles.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!--
  2. Copyright (C) 2019-2023 Nicola Murino
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as published
  5. by the Free Software Foundation, version 3.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with this program. If not, see <https://www.gnu.org/licenses/>.
  12. -->
  13. {{template "base" .}}
  14. {{define "title"}}{{.Title}}{{end}}
  15. {{define "extra_css"}}
  16. <link href="{{.StaticURL}}/vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
  17. <link href="{{.StaticURL}}/vendor/datatables/buttons.bootstrap4.min.css" rel="stylesheet">
  18. <link href="{{.StaticURL}}/vendor/datatables/fixedHeader.bootstrap4.min.css" rel="stylesheet">
  19. <link href="{{.StaticURL}}/vendor/datatables/responsive.bootstrap4.min.css" rel="stylesheet">
  20. <link href="{{.StaticURL}}/vendor/datatables/select.bootstrap4.min.css" rel="stylesheet">
  21. {{end}}
  22. {{define "page_body"}}
  23. <div id="errorMsg" class="alert alert-warning fade show" style="display: none;" role="alert">
  24. <span id="errorTxt"></span>
  25. <button type="button" class="close" aria-label="Close" onclick="dismissErrorMsg();">
  26. <span aria-hidden="true">&times;</span>
  27. </button>
  28. </div>
  29. <script type="text/javascript">
  30. function dismissErrorMsg(){
  31. $('#errorMsg').hide();
  32. }
  33. </script>
  34. <div class="card shadow mb-4">
  35. <div class="card-header py-3">
  36. <h6 class="m-0 font-weight-bold text-primary">View and manage roles</h6>
  37. </div>
  38. <div class="card-body">
  39. <div class="table-responsive">
  40. <table class="table table-hover nowrap" id="dataTable" width="100%" cellspacing="0">
  41. <thead>
  42. <tr>
  43. <th>Name</th>
  44. <th>Description</th>
  45. <th>Members</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. {{range .Roles}}
  50. <tr>
  51. <td>{{.Name}}</td>
  52. <td>{{.Description}}</td>
  53. <td>{{.GetMembersAsString}}</td>
  54. </tr>
  55. {{end}}
  56. </tbody>
  57. </table>
  58. </div>
  59. </div>
  60. </div>
  61. {{end}}
  62. {{define "dialog"}}
  63. <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel"
  64. aria-hidden="true">
  65. <div class="modal-dialog" role="document">
  66. <div class="modal-content">
  67. <div class="modal-header">
  68. <h5 class="modal-title" id="deleteModalLabel">
  69. Confirmation required
  70. </h5>
  71. <button class="close" type="button" data-dismiss="modal" aria-label="Close">
  72. <span aria-hidden="true">&times;</span>
  73. </button>
  74. </div>
  75. <div class="modal-body">Do you want to delete the selected role? It is not possible to remove a role that does contain administrators</div>
  76. <div class="modal-footer">
  77. <button class="btn btn-secondary" type="button" data-dismiss="modal">
  78. Cancel
  79. </button>
  80. <a class="btn btn-warning" href="#" onclick="deleteAction()">
  81. Delete
  82. </a>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. {{end}}
  88. {{define "extra_js"}}
  89. <script src="{{.StaticURL}}/vendor/datatables/jquery.dataTables.min.js"></script>
  90. <script src="{{.StaticURL}}/vendor/datatables/dataTables.bootstrap4.min.js"></script>
  91. <script src="{{.StaticURL}}/vendor/datatables/dataTables.buttons.min.js"></script>
  92. <script src="{{.StaticURL}}/vendor/datatables/buttons.bootstrap4.min.js"></script>
  93. <script src="{{.StaticURL}}/vendor/datatables/buttons.colVis.min.js"></script>
  94. <script src="{{.StaticURL}}/vendor/datatables/dataTables.fixedHeader.min.js"></script>
  95. <script src="{{.StaticURL}}/vendor/datatables/dataTables.responsive.min.js"></script>
  96. <script src="{{.StaticURL}}/vendor/datatables/responsive.bootstrap4.min.js"></script>
  97. <script src="{{.StaticURL}}/vendor/datatables/dataTables.select.min.js"></script>
  98. <script src="{{.StaticURL}}/vendor/datatables/ellipsis.js"></script>
  99. <script type="text/javascript">
  100. function deleteAction() {
  101. let table = $('#dataTable').DataTable();
  102. table.button('delete:name').enable(false);
  103. let roleName = table.row({ selected: true }).data()[0];
  104. let path = '{{.RoleURL}}' + "/" + fixedEncodeURIComponent(roleName);
  105. $('#deleteModal').modal('hide');
  106. $('#errorMsg').hide();
  107. $.ajax({
  108. url: path,
  109. type: 'DELETE',
  110. dataType: 'json',
  111. headers: {'X-CSRF-TOKEN' : '{{.CSRFToken}}'},
  112. timeout: 15000,
  113. success: function (result) {
  114. window.location.href = '{{.RolesURL}}';
  115. },
  116. error: function ($xhr, textStatus, errorThrown) {
  117. var txt = "Unable to delete the selected role";
  118. if ($xhr) {
  119. var json = $xhr.responseJSON;
  120. if (json) {
  121. if (json.message){
  122. txt += ": " + json.message;
  123. } else {
  124. txt += ": " + json.error;
  125. }
  126. }
  127. }
  128. $('#errorTxt').text(txt);
  129. $('#errorMsg').show();
  130. }
  131. });
  132. }
  133. $(document).ready(function () {
  134. $.fn.dataTable.ext.buttons.add = {
  135. text: '<i class="fas fa-plus"></i>',
  136. name: 'add',
  137. titleAttr: "Add",
  138. action: function (e, dt, node, config) {
  139. window.location.href = '{{.RoleURL}}';
  140. }
  141. };
  142. $.fn.dataTable.ext.buttons.edit = {
  143. text: '<i class="fas fa-pen"></i>',
  144. name: 'edit',
  145. titleAttr: "Edit",
  146. action: function (e, dt, node, config) {
  147. let roleName = table.row({ selected: true }).data()[0];
  148. let path = '{{.RoleURL}}' + "/" + fixedEncodeURIComponent(roleName);
  149. window.location.href = path;
  150. },
  151. enabled: false
  152. };
  153. $.fn.dataTable.ext.buttons.delete = {
  154. text: '<i class="fas fa-trash"></i>',
  155. name: 'delete',
  156. titleAttr: "Delete",
  157. action: function (e, dt, node, config) {
  158. $('#deleteModal').modal('show');
  159. },
  160. enabled: false
  161. };
  162. var table = $('#dataTable').DataTable({
  163. "select": {
  164. "style": "single",
  165. "blurable": true
  166. },
  167. "stateSave": true,
  168. "stateDuration": 0,
  169. "buttons": [
  170. {
  171. "text": "Column visibility",
  172. "extend": "colvis",
  173. "columns": ":not(.noVis)"
  174. }
  175. ],
  176. "columnDefs": [
  177. {
  178. "targets": [0],
  179. "className": "noVis"
  180. },
  181. {
  182. "targets": [2],
  183. "render": $.fn.dataTable.render.ellipsis(100, true)
  184. },
  185. ],
  186. "scrollX": false,
  187. "scrollY": false,
  188. "responsive": true,
  189. "language": {
  190. "emptyTable": "No role defined"
  191. },
  192. "order": [[0, 'asc']]
  193. });
  194. new $.fn.dataTable.FixedHeader( table );
  195. {{if .LoggedAdmin.HasPermission "manage_roles"}}
  196. table.button().add(0,'delete');
  197. table.button().add(0,'edit');
  198. table.button().add(0,'add');
  199. table.buttons().container().appendTo('.col-md-6:eq(0)', table.table().container());
  200. table.on('select deselect', function () {
  201. var selectedRows = table.rows({ selected: true }).count();
  202. table.button('delete:name').enable(selectedRows == 1);
  203. table.button('edit:name').enable(selectedRows == 1);
  204. });
  205. {{end}}
  206. });
  207. </script>
  208. {{end}}