From 2c6b0e3d30cc24bc5982b12680152e3eeaf0fd79 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Mon, 8 Feb 2021 17:04:55 +0100 Subject: [PATCH] implement user list, enable, disable --- owrx/controllers/session.py | 2 +- owrx/users.py | 12 ++++++++++++ owrxadmin/__main__.py | 12 ++++++++++-- owrxadmin/commands.py | 25 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/owrx/controllers/session.py b/owrx/controllers/session.py index ac38a43..9b45d11 100644 --- a/owrx/controllers/session.py +++ b/owrx/controllers/session.py @@ -45,7 +45,7 @@ class SessionController(WebpageController): if "user" in data and "password" in data: if data["user"] in userlist: user = userlist[data["user"]] - if user.password.is_valid(data["password"]): + if user.is_enabled() and user.password.is_valid(data["password"]): # TODO evaluate password force_change and redirect to password change key = SessionStorage.getSharedInstance().startSession({"user": user.name}) cookie = SimpleCookie() diff --git a/owrx/users.py b/owrx/users.py index 4ee0a50..06c9089 100644 --- a/owrx/users.py +++ b/owrx/users.py @@ -105,6 +105,15 @@ class User(object): def setPassword(self, password: Password): self.password = password + def is_enabled(self): + return self.enabled + + def enable(self): + self.enabled = True + + def disable(self): + self.enabled = False + class UserList(object): sharedInstance = None @@ -185,3 +194,6 @@ class UserList(object): raise KeyError("User {user} already exists".format(user=key)) self.users[key] = value self.store() + + def values(self): + return self.users.values() diff --git a/owrxadmin/__main__.py b/owrxadmin/__main__.py index ea38ca0..91f0d88 100644 --- a/owrxadmin/__main__.py +++ b/owrxadmin/__main__.py @@ -1,5 +1,5 @@ from owrx.version import openwebrx_version -from owrxadmin.commands import NewUser, DeleteUser, ResetPassword +from owrxadmin.commands import NewUser, DeleteUser, ResetPassword, ListUsers, DisableUser, EnableUser import argparse import sys import traceback @@ -9,12 +9,14 @@ def main(): print("OpenWebRX admin version {version}".format(version=openwebrx_version)) parser = argparse.ArgumentParser() - parser.add_argument("command", help="One of the following commands: adduser, removeuser, resetpassword") + parser.add_argument("command", help="""One of the following commands: + adduser, removeuser, listusers, resetpassword, enableuser, disableuser""") parser.add_argument( "--noninteractive", action="store_true", help="Don't ask for any user input (useful for automation)" ) parser.add_argument("--silent", action="store_true", help="Ignore errors (useful for automation)") parser.add_argument("-u", "--user", help="User name to perform action upon") + parser.add_argument("-a", "--all", action="store_true", help="Show all users") args = parser.parse_args() if args.command == "adduser": @@ -23,6 +25,12 @@ def main(): command = DeleteUser() elif args.command == "resetpassword": command = ResetPassword() + elif args.command == "listusers": + command = ListUsers() + elif args.command == "disableuser": + command = DisableUser() + elif args.command == "enableuser": + command = EnableUser() else: if not args.silent: print("Unknown command: {command}".format(command=args.command)) diff --git a/owrxadmin/commands.py b/owrxadmin/commands.py index 3793463..115b34e 100644 --- a/owrxadmin/commands.py +++ b/owrxadmin/commands.py @@ -76,3 +76,28 @@ class ResetPassword(UserCommand): # this is a change to an object in the list, not the list itself # in this case, store() is explicit userList.store() + + +class DisableUser(UserCommand): + def run(self, args): + username = self.getUser(args) + userList = UserList() + userList[username].disable() + userList.store() + + +class EnableUser(UserCommand): + def run(self, args): + username = self.getUser(args) + userList = UserList() + userList[username].enable() + userList.store() + + +class ListUsers(Command): + def run(self, args): + userList = UserList() + print("List of enabled users:") + for u in userList.values(): + if args.all or u.enabled: + print(" {name}".format(name=u.name))