From c09f17579c613b9cd533490337da1bacb897013b Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Thu, 18 Feb 2021 15:42:12 +0100 Subject: [PATCH] implement a command to test for a user's existence --- owrxadmin/__main__.py | 6 +++++- owrxadmin/commands.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/owrxadmin/__main__.py b/owrxadmin/__main__.py index f92233c..36f7b6e 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, ListUsers, DisableUser, EnableUser +from owrxadmin.commands import NewUser, DeleteUser, ResetPassword, ListUsers, DisableUser, EnableUser, HasUser import argparse import sys import traceback @@ -36,6 +36,10 @@ def main(): enableuser_parser.add_argument("user", help="Username to be enabled") enableuser_parser.set_defaults(cls=EnableUser) + hasuser_parser = subparsers.add_parser("hasuser", help="Test if a user exists") + hasuser_parser.add_argument("user", help="Username to be checked") + hasuser_parser.set_defaults(cls=HasUser) + parser.add_argument("-v", "--version", action="store_true", help="Show the software version") parser.add_argument( "--noninteractive", action="store_true", help="Don't ask for any user input (useful for automation)" diff --git a/owrxadmin/commands.py b/owrxadmin/commands.py index 69aad1d..8eca85e 100644 --- a/owrxadmin/commands.py +++ b/owrxadmin/commands.py @@ -97,3 +97,19 @@ class ListUsers(Command): for u in userList.values(): if args.all or u.enabled: print(" {name}".format(name=u.name)) + + +class HasUser(Command): + """ + internal command used by the debian config scripts to test if the admin user has already been created + """ + def run(self, args): + userList = UserList() + if args.user in userList: + if not args.silent: + print('User "{name}" exists.'.format(name=args.user)) + else: + if not args.silent: + print('User "{name}" does not exist.'.format(name=args.user)) + # in bash, a return code > 0 is interpreted as "false" + sys.exit(1)