From ccec8faff6e74e27aa21d53f39cf6bc916780b13 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Wed, 26 Aug 2020 18:06:27 +0200 Subject: [PATCH] Implement demo mode --- usr/lib/python3.8/vmmgr/wsgiapp.py | 27 +++++++++++++++++++++++++++ usr/lib/python3.8/vmmgr/wsgilang.py | 1 + usr/share/vmmgr/static/js/admin.js | 6 +++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/usr/lib/python3.8/vmmgr/wsgiapp.py b/usr/lib/python3.8/vmmgr/wsgiapp.py index c717fc4..ac3def0 100644 --- a/usr/lib/python3.8/vmmgr/wsgiapp.py +++ b/usr/lib/python3.8/vmmgr/wsgiapp.py @@ -26,6 +26,7 @@ logging.basicConfig(level=logging.WARNING) class WSGIApp: def __init__(self): + self.demo = config.get_common().get('demo') self.queue = ActionQueue() self.jinja_env = Environment(loader=FileSystemLoader('/usr/share/vmmgr/templates'), autoescape=True, lstrip_blocks=True, trim_blocks=True) self.url_map = Map(( @@ -289,6 +290,9 @@ class WSGIApp: return self.render_html('setup-remote.html', request, authorized_keys=authorized_keys, wg_conf=wg_conf, message=message) def update_host_action(self, request): + # Do nothing in demo + if self.demo: + return self.render_json({'error': request.session.lang.not_available_in_demo()}) # Update domain and port, then restart nginx domain = request.form['domain'] port = request.form['port'] @@ -339,6 +343,9 @@ class WSGIApp: return self.render_json({'ok': request.session.lang.http_hosts_ok(port)}) def update_cert_action(self, request): + # Do nothing in demo + if self.demo: + return self.render_json({'error': request.session.lang.not_available_in_demo()}) # Update certificate - either request via Let's Encrypt or manually upload files if request.form['method'] == 'selfsigned': vmmgr.create_selfsigned_cert() @@ -361,6 +368,10 @@ class WSGIApp: return self.render_json({'ok': request.session.lang.cert_installed(url, url)}) def update_common_action(self, request): + # Do nothing in demo + if self.demo: + request.session['msg'] = f'common:error:{request.session.lang.not_available_in_demo()}' + return redirect('/setup-apps') # Update common settings shared between apps - admin e-mail address, Google Maps API key email = request.form['email'] if not validator.is_valid_email(email): @@ -371,6 +382,10 @@ class WSGIApp: return redirect('/setup-apps') def update_repo_action(self, request): + # Do nothing in demo + if self.demo: + request.session['msg'] = f'repo:error:{request.session.lang.not_available_in_demo()}' + return redirect('/setup-apps') # Update repository URL and credentials url = request.form['repourl'] if not validator.is_valid_repo_url(url): @@ -428,6 +443,9 @@ class WSGIApp: return self.render_json({'ok': self.render_setup_apps_table(request)}) def update_password_action(self, request): + # Do nothing in demo + if self.demo: + return self.render_json({'error': request.session.lang.not_available_in_demo()}) # Updates password for both HDD encryption (LUKS-on-LVM) and web interface admin account if request.form['newpassword'] != request.form['newpassword2']: return self.render_json({'error': request.session.lang.password_mismatch()}) @@ -441,18 +459,27 @@ class WSGIApp: return self.render_json({'ok': request.session.lang.password_changed()}) def reboot_vm_action(self, request): + # Do nothing in demo + if self.demo: + return self.render_json({'error': request.session.lang.not_available_in_demo()}) # Reboots VM response = self.render_json({'ok': request.session.lang.reboot_initiated()}) response.call_on_close(vmmgr.reboot_vm) return response def shutdown_vm_action(self, request): + # Do nothing in demo + if self.demo: + return self.render_json({'error': request.session.lang.not_available_in_demo()}) # Shuts down VM response = self.render_json({'ok': request.session.lang.shutdown_initiated()}) response.call_on_close(vmmgr.shutdown_vm) return response def update_ssh_keys_action(self, request): + # Do nothing in demo + if self.demo: + return self.render_json({'error': request.session.lang.not_available_in_demo()}) # Update authorized_keys file remote.set_authorized_keys(request.form['ssh-keys'].replace('\r', '')) request.session['msg'] = f'ssh:info:{request.session.lang.ssh_keys_installed()}' diff --git a/usr/lib/python3.8/vmmgr/wsgilang.py b/usr/lib/python3.8/vmmgr/wsgilang.py index a7a26ee..7aa8011 100644 --- a/usr/lib/python3.8/vmmgr/wsgilang.py +++ b/usr/lib/python3.8/vmmgr/wsgilang.py @@ -54,6 +54,7 @@ class WSGILang: 'action_update': 'Aktualizovat', 'ssh_keys_installed': 'SSH klíče byly úspěšně změněny.', 'vpn_updated': 'Nastavení VPN bylo úspěšně změněno.', + 'not_available_in_demo': 'Tato funkce není v demo verzi povolena', } def __getattr__(self, key): diff --git a/usr/share/vmmgr/static/js/admin.js b/usr/share/vmmgr/static/js/admin.js index c339f71..6a1abd1 100644 --- a/usr/share/vmmgr/static/js/admin.js +++ b/usr/share/vmmgr/static/js/admin.js @@ -205,7 +205,11 @@ function update_password() { function _do_vm(action) { $.get('/'+action+'-vm', function(data) { - $('#vm-message').attr('class','info').html(data.ok).show(); + if (data.error) { + $('#vm-message').attr('class','error').html(data.error).show(); + } else { + $('#vm-message').attr('class','info').html(data.ok).show(); + } }).fail(lost_connection_alert); }