User selection from a list in multiuser local mode

This commit is contained in:
shortcutme 2019-10-06 03:05:39 +02:00
parent 57c0daa294
commit 15fca6bd12
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE

View file

@ -8,7 +8,6 @@ from Crypt import CryptBitcoin
from . import UserPlugin from . import UserPlugin
from util.Flag import flag from util.Flag import flag
# We can only import plugin host clases after the plugins are loaded # We can only import plugin host clases after the plugins are loaded
@PluginManager.afterLoad @PluginManager.afterLoad
def importPluginnedClasses(): def importPluginnedClasses():
@ -144,6 +143,52 @@ class UiWebsocketPlugin(object):
else: else:
self.response(to, "User not found") self.response(to, "User not found")
@flag.admin
def actionUserSet(self, to, master_address):
user_manager = UserManager.user_manager
user = user_manager.get(master_address)
if not user:
raise Exception("No user found")
script = "document.cookie = 'master_address=%s;path=/;max-age=2592000;';" % master_address
script += "zeroframe.cmd('wrapperReload', ['login=done']);"
self.cmd("notification", ["done", "Successful login, reloading page..."])
self.cmd("injectScript", script)
self.response(to, "ok")
@flag.admin
def actionUserSelectForm(self, to):
if not config.multiuser_local:
raise Exception("Only allowed in multiuser local mode")
user_manager = UserManager.user_manager
body = "<span style='padding-bottom: 5px; display: inline-block'>" + "Change account:" + "</span>"
for master_address, user in user_manager.list().items():
is_active = self.user.master_address == master_address
if user.certs:
first_cert = next(iter(user.certs.keys()))
title = "%s@%s" % (user.certs[first_cert]["auth_user_name"], first_cert)
else:
title = user.master_address
if len(user.sites) < 2 and not is_active: # Avoid listing ad-hoc created users
continue
if is_active:
css_class = "active"
else:
css_class = "noclass"
body += "<a href='#Select+user' class='select select-close user %s' title='%s'>%s</a>" % (css_class, user.master_address, title)
script = """
$(".notification .select.user").on("click", function() {
$(".notification .select").removeClass('active')
zeroframe.response(%s, this.title)
return false
})
""" % self.next_message_id
self.cmd("notification", ["ask", body], lambda master_address: self.actionUserSet(to, master_address))
self.cmd("injectScript", script)
# Show login form # Show login form
def actionUserLoginForm(self, to): def actionUserLoginForm(self, to):
self.cmd("prompt", ["<b>Login</b><br>Your private key:", "password", "Login"], self.responseUserLogin) self.cmd("prompt", ["<b>Login</b><br>Your private key:", "password", "Login"], self.responseUserLogin)