From 06f3a9b50b29dda243c0b551649ac66cf41517bb Mon Sep 17 00:00:00 2001
From: Alexander Schlemmer <alexander@mail-schlemmer.de>
Date: Wed, 24 Feb 2021 07:28:39 +0100
Subject: [PATCH] ENH: add some convenience options to account generation

---
 src/caosdb/utils/caosdb_admin.py | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/caosdb/utils/caosdb_admin.py b/src/caosdb/utils/caosdb_admin.py
index ff06624e..c2e751d9 100755
--- a/src/caosdb/utils/caosdb_admin.py
+++ b/src/caosdb/utils/caosdb_admin.py
@@ -116,13 +116,15 @@ def _promt_for_pw():
 
 
 def do_create_user(args):
-    password = None
+    password = args.user_password
 
     if args.ask_password is True:
         password = _promt_for_pw()
     try:
         admin._insert_user(name=args.user_name,
                            email=args.user_email, password=password)
+        if args.activate_user:
+            do_activate_user(args)
     except HTTPClientError as e:
         print(e.msg)
 
@@ -136,7 +138,10 @@ def do_deactivate_user(args):
 
 
 def do_set_user_password(args):
-    password = _promt_for_pw()
+    if args.user_password is None:
+        password = _promt_for_pw()
+    else:
+        password = args.user_password
     admin._update_user(name=args.user_name, password=password)
 
 
@@ -305,6 +310,8 @@ USAGE
     subparser.set_defaults(call=do_create_user)
     subparser.add_argument("-a", "--ask-password",
                            help="Prompt for a password.", action="store_true")
+    subparser.add_argument("-c", "--activate-user",
+                           help="Activate the user after creation.", action="store_true")
     subparser.add_argument(
         metavar='USERNAME',
         dest="user_name",
@@ -314,6 +321,12 @@ USAGE
         nargs='?',
         dest="user_email",
         help="The email address of the new user.")
+    subparser.add_argument(
+        metavar='PASSWORD',
+        nargs="?",
+        dest="user_password",
+        default=None,
+        help="The new user's password. This is overwritten by the -a option.")
 
     subparser = subparsers.add_parser(
         "activate_user", help="(Re-)activate an inactive (but existing) user.")
@@ -331,12 +344,18 @@ USAGE
 
     subparser = subparsers.add_parser(
         "set_user_password",
-        help="Set a new password for a user. The password is not to be given on the command line for security reasons. You will be prompted for the password.")
+        help="Set a new password for a user. You will be prompted for the password if PASSWORD is not given.")
     subparser.set_defaults(call=do_set_user_password)
     subparser.add_argument(
         metavar='USERNAME',
         dest="user_name",
         help="The name of the user who's password is to be set.")
+    subparser.add_argument(
+        metavar='PASSWORD',
+        nargs="?",
+        dest="user_password",
+        default=None,
+        help="The user's new password.")
 
     subparser = subparsers.add_parser(
         "set_user_entity",
-- 
GitLab