// Copyright (c) 2021, Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de) // // This file is part of the WebLDAPPasswd application. // // The WebLDAPPasswd application is free software: you can redistribute it // and/or modify it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the License or // (at your option) any later version. // // The WebLDAPPasswd application 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 Affero // General Public License for more details. // // You should have received a copy of the GNU General Affero Public License // along with the WebLDAPPasswd. If not, see . MODAL_STATES = { "Wait" : { button_disabled : true, message : "Waiting for server response ...", icon_url : "hourglass.svg", }, "Success" : { button_disabled : false, message : "Password changed", icon_url : "checkmark.svg", }, "FetchError" : { button_disabled : false, message : function(parameters) { `An error occurred while contacting the server ({parameters.statusCode})` }, }, "ServerError" : { button_disabled : false, message : "A server error occurred. Password was not changed.", icon_url : "cross.svg", }, "InvalidCredentials" : { button_disabled : false, message : "Invalid credentials provided. Password was not changed.", icon_url : "cross.svg", }, } function showModal(state_name, parameters) { let state = MODAL_STATES[state_name]; let message = document.getElementById("message"); let img = message.childNodes[0]; img.src = state.icon_url; let text = message.childNodes[1]; text.textContent = " " + (state.message instanceof Function ? state.message(parameters) : state.message); let button = document.getElementById("close_button"); button.disabled = state.button_disabled; let modal = document.getElementById("modal"); modal.style.display = 'flex'; } function hideModal() { let modal = document.getElementById("modal"); modal.style.display = 'none'; } FIELDS = ['username', 'old_password', 'new_password'] function changePasswords() { let query = {}; for (field in FIELDS) { query[FIELDS[field]] = document.querySelectorAll('input[name=' + FIELDS[field] + ']')[0].value; } showModal("Wait"); fetch("update", { method: "POST", body: JSON.stringify(query), }).then(response => { if(response.status == 200) { return response.json(); } else { showModal("FetchError", { statusCode : response.status }); } }).then(response => { if(response.success) { showModal("Success"); } else { showModal(response.message); } }); }