Minor fixes to user-add

This commit is contained in:
Tim Van Baak 2020-01-11 22:36:39 -08:00
parent 02ca885599
commit 02871798a6
3 changed files with 14 additions and 3 deletions

View File

@ -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):

View File

@ -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:

View File

@ -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: