'use strict' /* global __, xhr, dijit, Notify, Tables, App, fox */ const Users = { reload: function(sort) { return new Promise((resolve, reject) => { const user_search = App.byId("user_search"); const search = user_search ? user_search.value : ""; xhr.post("backend.php", { op: "Pref_Users", sort: sort, search: search }, (reply) => { dijit.byId('usersTab').attr('content', reply); Notify.close(); resolve(); }, (e) => { reject(e) }); }); }, add: function() { const login = prompt(__("Please enter username:"), ""); if (login) { Notify.progress("Adding user..."); xhr.post("backend.php", {op: "Pref_Users", method: "add", login: login}, (reply) => { Users.reload().then(() => { Notify.info(reply); }) }); } }, edit: function(id) { xhr.json('backend.php', {op: 'Pref_Users', method: 'edit', id: id}, (reply) => { const user = reply.user; const admin_disabled = (user.id == 1); const dialog = new fox.SingleUseDialog({ id: "userEditDlg", title: __("Edit user"), execute: function () { if (this.validate()) { Notify.progress("Saving data...", true); xhr.post("backend.php", this.attr('value'), (reply) => { dialog.hide(); Users.reload().then(() => { Notify.info(reply); }); }); } }, content: `
${App.FormFields.hidden_tag('id', user.id.toString())} ${App.FormFields.hidden_tag('op', 'Pref_Users')} ${App.FormFields.hidden_tag('method', 'editSave')}
${admin_disabled ? App.FormFields.hidden_tag("login", user.login) : ''}

${App.FormFields.select_hash("access_level", user.access_level, reply.access_level_names, {disabled: admin_disabled.toString()}, "", {numeric_sort: true})} ${admin_disabled ? App.FormFields.hidden_tag("access_level", user.access_level.toString()) : ''}

${__("Loading, please wait...")}
` }); dialog.show(); }); }, resetSelected: function() { const rows = this.getSelection(); if (rows.length == 0) { alert(__("No users selected.")); return; } if (rows.length > 1) { alert(__("Please select one user.")); return; } if (confirm(__("Reset password of selected user?"))) { Notify.progress("Resetting password for selected user..."); const id = rows[0]; xhr.post("backend.php", {op: "Pref_Users", method: "resetPass", id: id}, (reply) => { Notify.close(); Notify.info(reply, true); }); } }, removeSelected: function() { const sel_rows = this.getSelection(); if (sel_rows.length > 0) { if (confirm(__("Remove selected users? Neither default admin nor your account will be removed."))) { Notify.progress("Removing selected users..."); const query = { op: "Pref_Users", method: "remove", ids: sel_rows.toString() }; xhr.post("backend.php", query, () => { this.reload(); }); } } else { alert(__("No users selected.")); } }, getSelection :function() { return Tables.getSelected("users-list"); } }