/* μlogger * * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net) * * This is free software; you can redistribute it and/or modify it under * 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ import { config, lang } from './constants.js'; import uModal from './modal.js'; import uUtils from './utils.js'; export default class UserDialog { /** * @param {string} type: edit, add, pass * @param {uUser=} user Update existing user if supplied */ constructor(type, user) { this.type = type; this.user = user; this.dialog = new uModal(this.getHtml()); this.form = this.dialog.modal.querySelector('#userForm'); this.form.onsubmit = () => false; } /** * @return {string} */ getHtml() { let deleteButton = ''; let header = ''; let action; let fields; switch (this.type) { case 'add': action = 'add'; header = ` `; fields = ` `; break; case 'edit': action = 'update'; deleteButton = `
${uUtils.sprintf(lang.strings['editinguser'], `${uUtils.htmlEncode(this.user.login)}`)}
${lang.strings['deluser']}
`; fields = ` `; break; case 'pass': action = 'update'; fields = ` `; break; default: throw new Error(`Unknown dialog type: ${this.type}`); } return `${deleteButton}
${header} ${fields}
`; } /** * Show edit user dialog * @see {uModal} * @returns {Promise} */ show() { return new Promise((resolve) => { this.resolveModal(resolve); }); } /** * @param {ModalCallback} resolve */ resolveModal(resolve) { this.dialog.show().then((result) => { if (result.cancelled) { return this.hide(); } if (result.action === 'update' || result.action === 'add') { if (!this.validate()) { return this.resolveModal(resolve); } result.data = this.getData(); } return resolve(result); }); } /** * Hide dialog */ hide() { this.dialog.hide(); } /** * Get data from track form * @return {boolean|{login: string, password: string, oldPassword: ?string}} */ getData() { let login; if (this.type === 'add') { login = this.form.elements['login'].value.trim(); } else { login = this.user.login; } let oldPass = null; if (this.type === 'pass') { oldPass = this.form.elements['oldpass'].value.trim(); } const pass = this.form.elements['pass'].value.trim(); return { login: login, password: pass, oldPassword: oldPass }; } /** * Validate form * @return {boolean} True if valid */ validate() { if (this.type === 'add') { const login = this.form.elements['login'].value.trim(); if (!login) { alert(lang.strings['allrequired']); return false; } } else if (this.type === 'pass') { const oldPass = this.form.elements['oldpass'].value.trim(); if (!oldPass) { alert(lang.strings['allrequired']); return false; } } const pass = this.form.elements['pass'].value.trim(); const pass2 = this.form.elements['pass2'].value.trim(); if (!pass || !pass2) { alert(lang.strings['allrequired']); return false; } if (pass !== pass2) { alert(lang.strings['passnotmatch']); return false; } if (!config.pass_regex.test(pass)) { alert(lang.strings['passlenmin'] + '\n' + lang.strings['passrules']); return false; } return true; } }