implement user deletion

This commit is contained in:
Jakob Ketterl 2021-02-06 18:15:02 +01:00
parent 99fe232a21
commit d72027e630
3 changed files with 35 additions and 8 deletions

View File

@ -113,6 +113,19 @@ class UserList(object):
def addUser(self, user: User): def addUser(self, user: User):
self[user.name] = 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): def __getitem__(self, item):
return self.users[item] return self.users[item]

View File

@ -1,5 +1,5 @@
from owrx.version import openwebrx_version from owrx.version import openwebrx_version
from owrxadmin.commands import NewUserCommand from owrxadmin.commands import NewUser, DeleteUser
import argparse import argparse
import sys import sys
@ -14,9 +14,9 @@ def main():
args = parser.parse_args() args = parser.parse_args()
if args.command == "adduser": if args.command == "adduser":
NewUserCommand().run(args) NewUser().run(args)
elif args.command == "removeuser": elif args.command == "removeuser":
print("removing user") DeleteUser().run(args)
else: else:
print("Unknown command: {command}".format(command=args.command)) print("Unknown command: {command}".format(command=args.command))
sys.exit(1) sys.exit(1)

View File

@ -1,4 +1,4 @@
from abc import ABC, abstractmethod from abc import ABC, ABCMeta, abstractmethod
from getpass import getpass from getpass import getpass
from owrx.users import UserList, User, CleartextPassword from owrx.users import UserList, User, CleartextPassword
import sys import sys
@ -12,16 +12,22 @@ class Command(ABC):
pass pass
class NewUserCommand(Command): class UserCommand(Command, metaclass=ABCMeta):
def run(self, args): def getUser(self, args):
if args.user: if args.user:
username = args.user return args.user
else: else:
if args.noninteractive: if args.noninteractive:
print("ERROR: User name not specified") print("ERROR: User name not specified")
sys.exit(1) sys.exit(1)
else: 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: if args.noninteractive:
print("Generating password for user {username}...".format(username=username)) print("Generating password for user {username}...".format(username=username))
password = self.getRandomPassword() password = self.getRandomPassword()
@ -44,3 +50,11 @@ class NewUserCommand(Command):
def getRandomPassword(self, length=10): def getRandomPassword(self, length=10):
printable = list(string.ascii_letters) + list(string.digits) printable = list(string.ascii_letters) + list(string.digits)
return ''.join(random.choices(printable, k=length)) 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)