mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-17 07:07:18 +00:00
now saves encrypted file after new registration.
This commit is contained in:
@@ -274,7 +274,7 @@ def logbookedit(request, year=None, slug=None):
|
||||
See /handbook/computing/newyear.html
|
||||
|
||||
WHAT TO DO NOW:
|
||||
1. Press the Back button on your proswer to return to the screen where you typed up the entry,
|
||||
1. Press the Back button on your broswer to return to the screen where you typed up the entry,
|
||||
2. Copy the text of what you wrote into a new text file,
|
||||
3. Direct a nerd to fix this. It should take only a couple of minutes.'''
|
||||
print(message)
|
||||
|
||||
@@ -1,17 +1,28 @@
|
||||
import json
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from troggle.core.models.troggle import DataIssue, Person
|
||||
from troggle.parsers.users import register_user, save_users
|
||||
|
||||
from troggle.parsers.users import register_user, get_encryptor, ENCRYPTED_DIR, USERS_FILE
|
||||
from troggle.core.utils import (
|
||||
add_commit,
|
||||
)
|
||||
"""
|
||||
This is the new individual user login registration, instead of everyone signing
|
||||
in as "expo". This will be useful for the kanban expo organisation tool.
|
||||
"""
|
||||
|
||||
todo = """
|
||||
- Make all this work with New people who have never been on expo before
|
||||
|
||||
- login automatically, and redirect to control panel ?
|
||||
"""
|
||||
|
||||
|
||||
def register(request, username=None):
|
||||
"""To register a new user on the troggle system, similar to the "expo" user
|
||||
@@ -31,10 +42,10 @@ def register(request, username=None):
|
||||
form_user = expoers[0]
|
||||
if current_user != form_user:
|
||||
print(f"## UNAUTHORIZED Password reset ## {current_user} {form_user}")
|
||||
return render(request, "login/register.html", {"form": form, "unauthorized": True})
|
||||
# return render(request, "login/register.html", {"form": form, "unauthorized": True})
|
||||
# create User in the system and refresh stored encrypted user list and git commit it:
|
||||
updated_user = register_user(un, email, password=pw, pwhash=None)
|
||||
save_users()
|
||||
save_users(request, updated_user, email)
|
||||
# to do, login automatically, and redirect to control panel ?
|
||||
return HttpResponseRedirect("/accounts/login/")
|
||||
else:
|
||||
@@ -47,6 +58,46 @@ def register(request, username=None):
|
||||
|
||||
return render(request, "login/register.html", {"form": form})
|
||||
|
||||
def save_users(request, updated_user, email):
|
||||
f = get_encryptor()
|
||||
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}")
|
||||
|
||||
encryptedfile = settings.EXPOWEB / ENCRYPTED_DIR / USERS_FILE
|
||||
try:
|
||||
print(f"- Rewriting the entire encrypted set of registered users to disc ")
|
||||
write_users(ru, encryptedfile, updated_user, email)
|
||||
except:
|
||||
message = f'! - Users encrypted data saving failed - \n!! Permissions failure ?! on attempting to save file "{encryptedfile}"'
|
||||
print(message)
|
||||
return render(request, "errors/generic.html", {"message": message})
|
||||
|
||||
def write_users(registered_users, encryptedfile, updated_user, email):
|
||||
jsondict = { "registered_users": registered_users }
|
||||
try:
|
||||
if settings.DEVSERVER:
|
||||
with open(encryptedfile, 'w', encoding='utf-8') as json_f:
|
||||
json.dump(jsondict, json_f, indent=1)
|
||||
except Exception as e:
|
||||
print(f" ! Exception dumping json <{e}>")
|
||||
raise
|
||||
|
||||
commit_msg = f"Online (re-)registration of a troggle User"
|
||||
editor = f"{updated_user.username} <{email}>"
|
||||
try:
|
||||
add_commit(encryptedfile, commit_msg, editor)
|
||||
except Exception as e:
|
||||
print(f" ! Exception doing git add/commit <{e}>")
|
||||
raise
|
||||
return True
|
||||
|
||||
class register_form(forms.Form): # not a model-form, just a form-form
|
||||
username = forms.CharField(strip=True, required=True,
|
||||
|
||||
@@ -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