mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-17 06:17:27 +00:00
now saves encrypted file after new registration.
This commit is contained in:
@@ -8,18 +8,18 @@ from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
|
||||
from troggle.core.models.troggle import DataIssue, Expedition, Person, PersonExpedition
|
||||
from troggle.core.models.troggle import DataIssue
|
||||
|
||||
"""This imports the registered troggle users, who are nearly-all, but not quite, Persons.
|
||||
exceptions are "expo" and "expoadmin" which are created by the databaseReset.py import program.
|
||||
|
||||
This imports unencrypted email addresses but never exports them.
|
||||
This can import unencrypted email addresses but never exports them.
|
||||
|
||||
Passwords are only ever stored as hashes using the standard Django functions.
|
||||
"""
|
||||
|
||||
todo = """
|
||||
- [copy these from paper notes]
|
||||
- Make all this work with New people who have never been on expo before
|
||||
"""
|
||||
|
||||
USERS_FILE = "users.json"
|
||||
@@ -48,20 +48,19 @@ def register_user(u, email, password=None, pwhash=None):
|
||||
raise
|
||||
return user
|
||||
|
||||
def get_key():
|
||||
def get_encryptor():
|
||||
key = settings.LONGTERM_SECRET_KEY # Django generated
|
||||
k = base64.urlsafe_b64encode(key.encode("utf8")[:32]) # make Fernet compatible
|
||||
f = Fernet(k)
|
||||
return f
|
||||
|
||||
|
||||
|
||||
def load_users():
|
||||
"""These are the previously registered users of the troggle system.
|
||||
"""
|
||||
PARSER_USERS = "_users"
|
||||
DataIssue.objects.filter(parser=PARSER_USERS).delete()
|
||||
|
||||
f = get_key()
|
||||
f = get_encryptor()
|
||||
|
||||
jsonfile = settings.EXPOWEB / ENCRYPTED_DIR / USERS_FILE
|
||||
jsonurl = "/" + str(Path(ENCRYPTED_DIR) / USERS_FILE)
|
||||
@@ -116,25 +115,7 @@ def load_users():
|
||||
new_user = register_user(u, email, pwhash=pwhash)
|
||||
else:
|
||||
new_user = register_user(u, email)
|
||||
save_users()
|
||||
# save_users() no need on initial parsing
|
||||
|
||||
def save_users():
|
||||
f = get_key()
|
||||
ru = []
|
||||
print(f"\n + Saving users, encrypted emails, and password hashes")
|
||||
for u in User.objects.all():
|
||||
if u.username in ["expo", "expoadmin"]:
|
||||
continue
|
||||
e_email = f.encrypt(u.email.encode("utf8")).decode()
|
||||
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(f" - {u.username} - {original}")
|
||||
|
||||
jsondict = { "registered_users": ru }
|
||||
encryptedfile = settings.EXPOWEB / ENCRYPTED_DIR / USERS_FILE
|
||||
if settings.DEVSERVER:
|
||||
with open(encryptedfile, 'w', encoding='utf-8') as json_f:
|
||||
json.dump(jsondict, json_f, indent=1)
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user