diff options
Diffstat (limited to 'parsers/users.py')
-rw-r--r-- | parsers/users.py | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/parsers/users.py b/parsers/users.py index de0ce38..75145eb 100644 --- a/parsers/users.py +++ b/parsers/users.py @@ -24,6 +24,29 @@ todo = """ USERS_FILE = "users_e.json" ENCRYPTED_DIR = "encrypted" + +def register_user(u, email, password=None, pwhash=None): + try: + if existing_user := User.objects.filter(username=u): # WALRUS + print(f" - deleting existing user '{existing_user[0]}' before importing") + existing_user[0].delete() + user = User.objects.create_user(u, email) + if pwhash: + user.password = pwhash + elif password: + user.set_password(password) # function creates hash and stores hash + print(f" # hashing provided clear-text password {password} to make pwhash for user {u}") + else: + user.set_password('secret') # function creates hash and stores hash + print(f" # hashing 'secret' password to make pwhash for user {u}") + user.is_staff = False + user.is_superuser = False + user.save() + print(f" - receated and reset user '{user}'") + except Exception as e: + print(f"Exception <{e}>\n{len(User.objects.all())} users now in db:\n{User.objects.all()}") + raise + def load_users(): """These are the previously registered users of the troggle system. """ @@ -71,34 +94,30 @@ def load_users(): if userdata["encrypted"]: email = f.decrypt(email).decode() print(f" - user: '{u} <{email}>' ") - if existing_user := User.objects.filter(username=userdata["username"]): # WALRUS - # print(f" - deleting existing user '{existing_user[0]}' before importing") - existing_user[0].delete() - user = User.objects.create_user(userdata["username"], email, "secret") - user.set_password = "secret" # special attribute stores hash not password - user.is_staff = False - user.is_superuser = False - user.save() except Exception as e: - print(f"Exception <{e}>\n{len(User.objects.all())} users now in db:\n{User.objects.all()}") + print(f"Exception <{e}>\n") formatted_json = json.dumps(userdata, indent=4) print(formatted_json) raise return None + if "pwhash" in userdata: + pwhash = userdata["pwhash"] + register_user(u, email, pwhash=pwhash) + else: + register_user(u, email) else: print(f" - user: BAD username for {userdata} ") ru = [] + print(f"\n + Exporting users, encrypted emails, and password hashes") for u in User.objects.all(): - if u.username == "expo": - continue - if u.username == "expoadmin": + if u.username in ["expo", "expoadmin"]: continue e_email = f.encrypt(u.email.encode("utf8")).decode() - ru.append({"username":u.username, "email": e_email, "password": u.password, "encrypted": True}) + ru.append({"username":u.username, "email": e_email, "pwhash": u.password, "encrypted": True}) # print(u.username, e_email) original = f.decrypt(e_email).decode() - print(u.username, original) + print(f" - {u.username} - {original}") jsondict = { "registered_users": ru } encryptedfile = settings.EXPOWEB / ENCRYPTED_DIR / "encrypt.json" |