openwebrx-clone/owrx/admin/__init__.py

60 lines
2.5 KiB
Python
Raw Permalink Normal View History

2021-04-24 17:39:48 +00:00
from owrx.admin.commands import NewUser, DeleteUser, ResetPassword, ListUsers, DisableUser, EnableUser, HasUser
2021-02-06 17:04:32 +00:00
import sys
2021-02-06 17:57:51 +00:00
import traceback
2021-02-12 16:00:51 +00:00
2021-02-06 17:04:32 +00:00
2021-04-24 17:39:48 +00:00
def add_admin_parser(moduleparser):
subparsers = moduleparser.add_subparsers(title="Commands", dest="command")
2021-02-12 17:34:28 +00:00
adduser_parser = subparsers.add_parser("adduser", help="Add a new user")
adduser_parser.add_argument("user", help="Username to be added")
adduser_parser.set_defaults(cls=NewUser)
removeuser_parser = subparsers.add_parser("removeuser", help="Remove an existing user")
removeuser_parser.add_argument("user", help="Username to be remvoed")
removeuser_parser.set_defaults(cls=DeleteUser)
resetpassword_parser = subparsers.add_parser("resetpassword", help="Reset a user's password")
resetpassword_parser.add_argument("user", help="Username to be remvoed")
resetpassword_parser.set_defaults(cls=ResetPassword)
listusers_parser = subparsers.add_parser("listusers", help="List enabled users")
listusers_parser.add_argument("-a", "--all", action="store_true", help="Show all users (including disabled ones)")
listusers_parser.set_defaults(cls=ListUsers)
disableuser_parser = subparsers.add_parser("disableuser", help="Disable a user")
disableuser_parser.add_argument("user", help="Username to be disabled")
disableuser_parser.set_defaults(cls=DisableUser)
enableuser_parser = subparsers.add_parser("enableuser", help="Enable a user")
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)
2021-04-24 17:39:48 +00:00
moduleparser.add_argument(
2021-02-06 18:03:28 +00:00
"--noninteractive", action="store_true", help="Don't ask for any user input (useful for automation)"
)
2021-04-24 17:39:48 +00:00
moduleparser.add_argument("--silent", action="store_true", help="Ignore errors (useful for automation)")
2021-02-12 17:34:28 +00:00
2021-04-26 17:34:50 +00:00
2021-04-24 17:39:48 +00:00
def run_admin_action(parser, args):
2021-02-12 17:34:28 +00:00
if hasattr(args, "cls"):
command = args.cls()
2021-02-06 17:04:32 +00:00
else:
2021-04-26 17:34:50 +00:00
if not hasattr(args, "silent") or not args.silent:
2021-02-12 17:34:28 +00:00
parser.print_help()
2022-06-01 15:58:06 +00:00
return 1
return 0
2021-02-06 17:57:51 +00:00
try:
2022-06-01 15:58:06 +00:00
return command.run(args)
2021-02-06 17:57:51 +00:00
except Exception:
2021-04-26 17:27:12 +00:00
if not hasattr(args, "silent") or not args.silent:
2021-02-06 17:57:51 +00:00
print("Error running command:")
traceback.print_exc()
2022-06-01 15:58:06 +00:00
return 1
return 0