From d72027e630d763aab6964237188523b36225d3db Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sat, 6 Feb 2021 18:15:02 +0100 Subject: [PATCH] implement user deletion --- owrx/users.py | 13 +++++++++++++ owrxadmin/__main__.py | 6 +++--- owrxadmin/commands.py | 24 +++++++++++++++++++----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/owrx/users.py b/owrx/users.py index 6c4e518..86173e1 100644 --- a/owrx/users.py +++ b/owrx/users.py @@ -113,6 +113,19 @@ class UserList(object): def addUser(self, user: User): self[user.name] = user + def deleteUser(self, user): + if isinstance(user, User): + username = user.name + else: + username = user + del self[username] + + def __delitem__(self, key): + if key not in self.users: + raise KeyError("User {user} doesn't exist".format(user=key)) + del self.users[key] + self._store() + def __getitem__(self, item): return self.users[item] diff --git a/owrxadmin/__main__.py b/owrxadmin/__main__.py index 227d881..1e3a653 100644 --- a/owrxadmin/__main__.py +++ b/owrxadmin/__main__.py @@ -1,5 +1,5 @@ from owrx.version import openwebrx_version -from owrxadmin.commands import NewUserCommand +from owrxadmin.commands import NewUser, DeleteUser import argparse import sys @@ -14,9 +14,9 @@ def main(): args = parser.parse_args() if args.command == "adduser": - NewUserCommand().run(args) + NewUser().run(args) elif args.command == "removeuser": - print("removing user") + DeleteUser().run(args) else: print("Unknown command: {command}".format(command=args.command)) sys.exit(1) diff --git a/owrxadmin/commands.py b/owrxadmin/commands.py index 182a10e..bf2f1b8 100644 --- a/owrxadmin/commands.py +++ b/owrxadmin/commands.py @@ -1,4 +1,4 @@ -from abc import ABC, abstractmethod +from abc import ABC, ABCMeta, abstractmethod from getpass import getpass from owrx.users import UserList, User, CleartextPassword import sys @@ -12,16 +12,22 @@ class Command(ABC): pass -class NewUserCommand(Command): - def run(self, args): +class UserCommand(Command, metaclass=ABCMeta): + def getUser(self, args): if args.user: - username = args.user + return args.user else: if args.noninteractive: print("ERROR: User name not specified") sys.exit(1) else: - username = input("Please enter the user name: ") + return input("Please enter the user name: ") + + +class NewUser(UserCommand): + def run(self, args): + username = self.getUser(args) + if args.noninteractive: print("Generating password for user {username}...".format(username=username)) password = self.getRandomPassword() @@ -44,3 +50,11 @@ class NewUserCommand(Command): def getRandomPassword(self, length=10): printable = list(string.ascii_letters) + list(string.digits) return ''.join(random.choices(printable, k=length)) + + +class DeleteUser(UserCommand): + def run(self, args): + username = self.getUser(args) + print("Deleting user {username}...".format(username=username)) + userList = UserList() + userList.deleteUser(username)