ulogger-server/js/admin.js

113 lines
4.6 KiB
JavaScript
Raw Normal View History

/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
2017-04-07 00:05:28 +02:00
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
2017-04-07 00:05:28 +02:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
function addUser() {
2017-04-12 20:16:39 +02:00
var form = '<form id="userForm" method="post" onsubmit="submitUser(\'add\'); return false">';
form += '<label><b>' + lang['username'] + '</b></label><input type="text" placeholder="' + lang['usernameenter'] + '" name="login" required>';
form += '<label><b>' + lang['password'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass" required>';
form += '<label><b>' + lang['passwordrepeat'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass2" required>';
form += '<div class="buttons"><button type="button" onclick="removeModal()">' + lang['cancel'] + '</button><button type="submit">' + lang['submit'] + '</button></div>';
form += '</form>';
showModal(form);
}
2017-04-12 20:16:39 +02:00
function editUser() {
var userForm = document.getElementsByName('user')[0];
var userLogin = userForm.options[userForm.selectedIndex].text;
if (userLogin == auth) {
2017-04-12 22:42:23 +02:00
alert(lang['selfeditwarn']);
2017-04-12 20:16:39 +02:00
return;
}
2017-04-14 16:00:53 +02:00
var message = '<div style="float:left">' + sprintf(lang['editinguser'], '<b>' + htmlEncode(userLogin) + '</b>') + '</div>';
2017-04-12 22:42:23 +02:00
message += '<div class="red-button"><b><a href="javascript:void(0);" onclick="submitUser(\'delete\'); return false">' + lang['deluser'] + '</a></b></div>';
2017-04-12 20:16:39 +02:00
message += '<div style="clear: both; padding-bottom: 1em;"></div>';
var form = '<form id="userForm" method="post" onsubmit="submitUser(\'update\'); return false">';
2017-04-14 16:00:53 +02:00
form += '<input type="hidden" name="login" value="' + htmlEncode(userLogin) + '">';
2017-04-12 20:16:39 +02:00
form += '<label><b>' + lang['password'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass" required>';
form += '<label><b>' + lang['passwordrepeat'] + '</b></label><input type="password" placeholder="' + lang['passwordenter'] + '" name="pass2" required>';
form += '<div class="buttons"><button type="button" onclick="removeModal()">' + lang['cancel'] + '</button><button type="submit">' + lang['submit'] + '</button></div>';
form += '</form>';
showModal(message + form);
}
function confirmedDelete(login) {
2017-04-12 22:42:23 +02:00
return confirm(sprintf(lang['deletewarn'], '"' + login + '"'));
2017-04-12 20:16:39 +02:00
}
function submitUser(action) {
var form = document.getElementById('userForm');
2017-04-14 16:00:53 +02:00
var login = form.elements['login'].value.trim();
2017-04-12 20:16:39 +02:00
if (!login) {
2017-04-12 22:42:23 +02:00
alert(lang['allrequired']);
2017-04-12 20:16:39 +02:00
return;
}
2017-04-12 20:16:39 +02:00
var pass = null;
var pass2 = null;
if (action != 'delete') {
pass = form.elements['pass'].value;
pass2 = form.elements['pass2'].value;
if (!pass || !pass2) {
2017-04-12 22:42:23 +02:00
alert(lang['allrequired']);
2017-04-12 20:16:39 +02:00
return;
}
if (pass != pass2) {
2017-04-12 22:42:23 +02:00
alert(lang['passnotmatch']);
2017-04-12 20:16:39 +02:00
return;
}
if (!pass_regex.test(pass)) {
alert(lang['passlenmin'] + '\n' + lang['passrules']);
return;
}
2017-04-12 20:16:39 +02:00
} else {
if (!confirmedDelete(login)) {
return;
}
}
var xhr = getXHR();
2017-04-12 20:16:39 +02:00
xhr.onreadystatechange = function() {
2017-04-09 23:35:55 +02:00
if (xhr.readyState == 4 && xhr.status == 200) {
var xml = xhr.responseXML;
var message = "";
if (xml) {
2017-04-09 23:35:55 +02:00
var root = xml.getElementsByTagName('root');
if (root.length && getNode(root[0], 'error') == 0) {
removeModal();
2017-04-12 22:42:23 +02:00
alert(lang['actionsuccess']);
2017-04-12 20:16:39 +02:00
if (action == 'delete') {
// select current user in users form
var f = document.getElementsByName('user')[0];
f.remove(f.selectedIndex);
selectUser(f);
}
return;
}
errorMsg = getNode(root[0], 'message');
if (errorMsg) { message = errorMsg; }
}
2017-04-12 22:42:23 +02:00
alert(lang['actionfailure'] + '\n' + message);
xhr = null;
}
}
2017-04-12 20:16:39 +02:00
xhr.open('POST', 'utils/handleuser.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
2017-04-12 20:16:39 +02:00
var params = 'action=' + action + '&login=' + encodeURIComponent(login) + '&pass=' + encodeURIComponent(pass);
params = params.replace(/%20/g, '+');
xhr.send(params);
return;
}