暫無描述

manage.users.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. let current_users_list = [];
  2. let current_customers_list = [];
  3. let data_dc = [];
  4. function add_user() {
  5. url = 'users/add/modal' + case_param();
  6. $('#modal_access_control').load(url, function (response, status, xhr) {
  7. if (status !== "success") {
  8. ajax_notify_error(xhr, url);
  9. return false;
  10. }
  11. $('#submit_new_user').on("click", function () {
  12. var data_sent = $('#form_new_user').serializeObject()
  13. clear_api_error();
  14. post_request_api('/manage/users/add', JSON.stringify(data_sent), true)
  15. .done((data) => {
  16. if(notify_auto_api(data)) {
  17. refresh_users();
  18. $('#modal_access_control').modal('hide');
  19. }
  20. });
  21. return false;
  22. })
  23. });
  24. $('#modal_access_control').modal({ show: true });
  25. }
  26. manage_users_table = $('#users_table').dataTable( {
  27. "order": [[ 1, "asc" ]],
  28. "autoWidth": false,
  29. "language": {
  30. "emptyTable": "Loading users..."
  31. },
  32. "columns": [
  33. {
  34. "data": "user_id",
  35. "render": function ( data, type, row ) {
  36. if (type === 'display') {
  37. data = sanitizeHTML(data)
  38. return '<a href="#" onclick="user_detail(\'' + row["user_id"] + '\');">' + data +'</a>';
  39. }
  40. return data;
  41. }
  42. },
  43. { "data": "user_name",
  44. "render": function ( data, type, row ) {
  45. if (type === 'display') {
  46. data = sanitizeHTML(data)
  47. return '<a href="#" onclick="user_detail(\'' + row["user_id"] + '\');">' + data +'</a>';
  48. }
  49. return data;
  50. }
  51. },
  52. { "data": "user_login",
  53. "render": function (data, type, row, meta) {
  54. if (type === 'display') { data = sanitizeHTML(data);}
  55. return data;
  56. }
  57. },
  58. { "data": "user_email",
  59. "render": function (data, type, row, meta) {
  60. if (type === 'display') { data = sanitizeHTML(data);}
  61. return data;
  62. }
  63. },
  64. { "data": "user_active",
  65. "render": function (data, type, row, meta) {
  66. if (type === 'display') {
  67. if (data == true) {
  68. data = '<span class="badge ml-2 badge-success">Active</span>';
  69. } else {
  70. data = '<span class="badge ml-2 badge-warning">Disabled</span>';
  71. }
  72. }
  73. return data;
  74. }
  75. },
  76. { "data": "user_is_service_account",
  77. "render": function (data, type, row, meta) {
  78. if (type === 'display') {
  79. if (data == true) {
  80. data = '<i class="fa fa-check text-success"></i>';
  81. } else {
  82. data = '<i class="fa fa-xmark text-danger"></i>';
  83. }
  84. }
  85. return data;
  86. }
  87. }
  88. ]
  89. }
  90. );
  91. function refresh_users(do_notify) {
  92. get_request_api('users/list')
  93. .done((data) => {
  94. if (api_request_failed(data)) {
  95. return true;
  96. }
  97. current_users_list = data.data;
  98. manage_users_table.api().clear().rows.add(data.data).draw();
  99. if (do_notify !== undefined) {
  100. notify_success("Refreshed");
  101. }
  102. });
  103. }
  104. /* Fetch the details of an user and allow modification */
  105. function user_detail(user_id, goto_tab) {
  106. url = 'users/' + user_id + '/modal' + case_param();
  107. $('#modal_access_control').load(url, function (response, status, xhr) {
  108. if (status !== "success") {
  109. ajax_notify_error(xhr, url);
  110. return false;
  111. }
  112. $('#submit_new_user').on("click", function () {
  113. clear_api_error();
  114. var data_sent = $('#form_new_user').serializeObject();
  115. post_request_api(`/manage/users/update/${user_id}`, JSON.stringify(data_sent), true)
  116. .done((data) => {
  117. if(notify_auto_api(data)) {
  118. refresh_users();
  119. $('#modal_access_control').modal('hide');
  120. }
  121. });
  122. return false;
  123. })
  124. if (goto_tab !== undefined) {
  125. $('.nav-pills a[href="#'+ goto_tab +'"]').tab('show');
  126. }
  127. $('#modal_access_control').modal({ show: true });
  128. });
  129. }
  130. function refresh_user_ac(user_id) {
  131. var ori_txt = $('#users_refresh_ac_btn').text();
  132. $('#users_refresh_ac_btn').text('Refreshing..');
  133. get_request_api(`/manage/access-control/recompute-effective-user-ac/${user_id}`)
  134. .done((data) => {
  135. notify_auto_api(data);
  136. }).always(() => {
  137. $('#users_refresh_ac_btn').text(ori_txt);
  138. });
  139. }
  140. function reset_user_mfa(user_id) {
  141. let users_refresh_mfa_btn = $('#users_refresh_mfa_btn');
  142. let ori_txt = users_refresh_mfa_btn.text();
  143. users_refresh_mfa_btn.text('Resetting..');
  144. get_request_api(`/manage/access-control/reset-mfa/${user_id}`)
  145. .done((data) => {
  146. notify_auto_api(data);
  147. }).always(() => {
  148. users_refresh_mfa_btn.text(ori_txt);
  149. });
  150. }
  151. function renew_api_for_user(user_id) {
  152. var ori_txt = $('#users_renew_api_btn').text();
  153. $('#users_renew_api_btn').text('Renewing..');
  154. post_request_api(`/manage/users/renew-api-key/${user_id}`)
  155. .done((data) => {
  156. if (notify_auto_api(data)) {
  157. $('#userApiKey').val(data.data.api_key);
  158. }
  159. }).always(() => {
  160. $('#users_renew_api_btn').text(ori_txt);
  161. });
  162. }
  163. function delete_user(id) {
  164. swal({
  165. title: "Are you sure?",
  166. text: "You won't be able to revert this !",
  167. icon: "warning",
  168. buttons: true,
  169. dangerMode: true,
  170. confirmButtonColor: '#3085d6',
  171. cancelButtonColor: '#d33',
  172. confirmButtonText: 'Yes, delete it!'
  173. })
  174. .then((willDelete) => {
  175. if (willDelete) {
  176. var data_sent = {
  177. 'csrf_token': $('#csrf_token').val()
  178. }
  179. post_request_api(`/manage/users/delete/${id}`, JSON.stringify(data_sent))
  180. .done((data) => {
  181. if(notify_auto_api(data)) {
  182. refresh_users();
  183. $('#modal_access_control').modal('hide');
  184. }
  185. });
  186. } else {
  187. swal("Pfew, that was close");
  188. }
  189. });
  190. }
  191. function activate_user(user_id) {
  192. get_request_api(`/manage/users/activate/${user_id}`)
  193. .done((data) => {
  194. if(notify_auto_api(data)) {
  195. user_detail(user_id);
  196. refresh_users();
  197. }
  198. });
  199. }
  200. function deactivate_user(user_id) {
  201. get_request_api(`/manage/users/deactivate/${user_id}`)
  202. .done((data) => {
  203. if(notify_auto_api(data)) {
  204. user_detail(user_id);
  205. refresh_users();
  206. }
  207. });
  208. }
  209. function remove_member_from_org_wrap(org_id, user_id) {
  210. remove_members_from_org(org_id, user_id, function() {
  211. user_detail(user_id, 'user_orgs_tab');
  212. });
  213. }
  214. function remove_member_from_group_wrap(group_id, user_id) {
  215. remove_members_from_group(group_id, user_id, function() {
  216. user_detail(user_id, 'user_groups_tab');
  217. });
  218. }
  219. function manage_user_groups(user_id) {
  220. let url = 'users/' + user_id + '/groups/modal' + case_param();
  221. $('#modal_ac_additional').load(url, function (response, status, xhr) {
  222. if (status !== "success") {
  223. ajax_notify_error(xhr, url);
  224. return false;
  225. }
  226. $('#modal_ac_additional').modal({ show: true });
  227. $('#save_user_groups_membership').on("click", function () {
  228. clear_api_error();
  229. let data_sent = Object();
  230. data_sent['groups_membership'] = $('#user_groups_membership').val();
  231. data_sent['csrf_token'] = $('#csrf_token').val();
  232. post_request_api(`/manage/users/${user_id}/groups/update`, JSON.stringify(data_sent))
  233. .done((data) => {
  234. if(notify_auto_api(data)) {
  235. refresh_groups();
  236. user_detail(user_id, 'user_groups_tab');
  237. }
  238. });
  239. });
  240. });
  241. }
  242. function update_customers_membership_modal(user_customers) {
  243. for (let index in current_customers_list) {
  244. data_dc.push({
  245. label: current_customers_list[index].customer_name,
  246. value: current_customers_list[index].customer_id
  247. });
  248. }
  249. let us_customer = $('#user_customers_membership');
  250. us_customer.multiselect({
  251. buttonWidth: 400,
  252. nonSelectedText: 'Select customers',
  253. includeSelectAllOption: true,
  254. enableFiltering: true,
  255. enableCaseInsensitiveFiltering: true,
  256. filterPlaceholder: 'Search',
  257. filterBehavior: 'both',
  258. widthSynchronizationMode: 'ifPopupIsSmaller'
  259. });
  260. us_customer.multiselect('dataprovider', data_dc );
  261. us_customer.multiselect('select', user_customers);
  262. us_customer.multiselect('refresh')
  263. }
  264. async function refresh_customers() {
  265. await get_request_api('/manage/customers/list')
  266. .done((data) => {
  267. if (api_request_failed(data)) {
  268. return;
  269. }
  270. current_customers_list = data.data;
  271. });
  272. }
  273. function manage_user_clients(user_id) {
  274. let url = 'users/' + user_id + '/customers/modal' + case_param();
  275. $('#modal_ac_additional').load(url, function (response, status, xhr) {
  276. if (status !== "success") {
  277. ajax_notify_error(xhr, url);
  278. return false;
  279. }
  280. $('#modal_ac_additional').modal({ show: true });
  281. $('#save_user_customers_membership').on("click", function () {
  282. clear_api_error();
  283. let data_sent = Object();
  284. data_sent['customers_membership'] = $('#user_customers_membership').val();
  285. data_sent['csrf_token'] = $('#csrf_token').val();
  286. post_request_api(`/manage/users/${user_id}/customers/update`, JSON.stringify(data_sent))
  287. .done((data) => {
  288. if(notify_auto_api(data)) {
  289. user_detail(user_id, 'user_clients_tab');
  290. }
  291. });
  292. });
  293. });
  294. }
  295. function manage_user_organisations(user_id) {
  296. url = 'users/' + user_id + '/organisations/modal' + case_param();
  297. $('#modal_ac_additional').load(url, function (response, status, xhr) {
  298. if (status !== "success") {
  299. ajax_notify_error(xhr, url);
  300. return false;
  301. }
  302. $('#modal_ac_additional').modal({ show: true });
  303. $('#save_user_orgs_membership').on("click", function () {
  304. clear_api_error();
  305. var data_sent = Object();
  306. data_sent['orgs_membership'] = $('#user_orgs_membership').val();
  307. data_sent['csrf_token'] = $('#csrf_token').val();
  308. post_request_api(`/manage/users/${user_id}/organisations/update`, JSON.stringify(data_sent))
  309. .done((data) => {
  310. if(notify_auto_api(data)) {
  311. user_detail(user_id, 'user_orgs_tab');
  312. }
  313. });
  314. });
  315. });
  316. }
  317. function refresh_user_cac(user_id) {
  318. if (modal_user_cac_table !== undefined) {
  319. get_request_api(`/manage/users/${user_id}`)
  320. .done((data) => {
  321. if(notify_auto_api(data)) {
  322. current_user_cases_access_list = data.data.user_cases_access;
  323. modal_user_cac_table.clear();
  324. modal_user_cac_table.rows.add(current_user_cases_access_list).draw();
  325. }
  326. });
  327. }
  328. }
  329. function manage_user_cac(user_id) {
  330. url = 'users/' + user_id + '/cases-access/modal' + case_param();
  331. $('#manage_user_cac_button').text('Loading manager...');
  332. $('#modal_ac_additional').load(url, function (response, status, xhr) {
  333. $('#manage_user_cac_button').text('Set case access');
  334. if (status !== "success") {
  335. ajax_notify_error(xhr, url);
  336. return false;
  337. }
  338. $('#grant_case_access_to_user').on("click", function () {
  339. clear_api_error();
  340. var data_sent = Object();
  341. data_sent['cases_list'] = $('#user_case_access_select').val();
  342. data_sent['access_level'] = parseInt($('#user_case_ac_select').val());
  343. data_sent['csrf_token'] = $('#csrf_token').val();
  344. post_request_api(`/manage/users/${user_id}/cases-access/update`, JSON.stringify(data_sent))
  345. .done((data) => {
  346. if(notify_auto_api(data)) {
  347. refresh_user_cac(user_id);
  348. $('#modal_ac_additional').modal('hide');
  349. }
  350. });
  351. return false;
  352. });
  353. $('#modal_ac_additional').modal({ show: true });
  354. });
  355. }
  356. function remove_cases_access_from_user_table(org_id, rows) {
  357. cases = [];
  358. for (cid in rows) {
  359. cases.push(rows[cid].case_id);
  360. }
  361. remove_cases_access_user(org_id, cases);
  362. }
  363. $(document).ready(function () {
  364. refresh_users();
  365. });