From 02871798a678f1c58b5e3de2636747c7e60afab7 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 11 Jan 2020 22:36:39 -0800 Subject: [PATCH] Minor fixes to user-add --- amanuensis/cli.py | 9 ++++++++- amanuensis/config/__init__.py | 2 +- amanuensis/user.py | 6 +++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/amanuensis/cli.py b/amanuensis/cli.py index 0b40807..362acc3 100644 --- a/amanuensis/cli.py +++ b/amanuensis/cli.py @@ -91,6 +91,9 @@ def command_init(args): os.mkdir(os.path.join(cfd, "lexicon")) if not os.path.isdir(os.path.join(cfd, "user")): os.mkdir(os.path.join(cfd, "user")) + if not os.path.isfile(os.path.join(cfd, 'user', 'index.json')): + with open(os.path.join(cfd, 'user', 'index.json'), 'w') as f: + json.dump({}, f) @no_argument def command_generate_secret(args): @@ -132,6 +135,9 @@ def command_user_add(args): if not user.valid_username(args.username): config.logger.error("Invalid username: usernames may only contain alphanumeric characters, dashes, and underscores") return -1 + if user.get_user_by_username(args.username) is not None: + config.logger.error("Invalid username: username is already taken") + return -1 if not args.displayname: args.displayname = args.username if not args.email: @@ -140,9 +146,10 @@ def command_user_add(args): config.logger.error("Invalid email") return -1 # Create user - new_user = user.create_user(args.username, args.displayname, args.email) + new_user, tmp_pw = user.create_user(args.username, args.displayname, args.email) with config.json_ro(new_user.config) as js: print(json.dumps(js, indent=2)) + print("Username: {}\nUser ID: {}\nPassword: {}".format(args.username, new_user.uid, tmp_pw)) @no_argument def command_user_list(args): diff --git a/amanuensis/config/__init__.py b/amanuensis/config/__init__.py index e48d15c..6f7142f 100644 --- a/amanuensis/config/__init__.py +++ b/amanuensis/config/__init__.py @@ -63,7 +63,7 @@ def new_user(user_json): json.dump(user_json, f, allow_nan=False, indent='\t') # Ensure index exists if not os.path.isdir(prepend('user', 'index.json')): - with open_ex(prepend('user', 'index.json'), 'w') as f: + with open_ex(os.path.join('user', 'index.json'), 'w') as f: json.dump({}, f) # Update index with json_rw('user', 'index.json') as j: diff --git a/amanuensis/user.py b/amanuensis/user.py index 117aa1e..f1935e5 100644 --- a/amanuensis/user.py +++ b/amanuensis/user.py @@ -36,6 +36,7 @@ def valid_email(email): def create_user(username, displayname, email): uid = uuid.uuid4().hex now = int(time.time()) + temp_pw = os.urandom(32).hex() user_json = { 'uid': uid, 'username': username, @@ -43,9 +44,12 @@ def create_user(username, displayname, email): 'email': email, 'password': None, 'created': now, + 'newPasswordRequired': True, } config.new_user(user_json) - return User(uid) + u = User(uid) + u.set_password(temp_pw) + return u, temp_pw def get_user_by_username(username): with config.json_ro('user', 'index.json') as index: