Enhance user registration: implement generation of truly anonymized usernames for new users
This commit is contained in:
+2
-2
@@ -7116,8 +7116,8 @@ def register():
|
|||||||
name = (request.form.get('name') or '').strip()
|
name = (request.form.get('name') or '').strip()
|
||||||
last_name = (request.form.get('last-name') or '').strip()
|
last_name = (request.form.get('last-name') or '').strip()
|
||||||
|
|
||||||
# Always generate username from abbreviation logic and auto-extend on collisions.
|
# Generate a truly anonymized username for new users.
|
||||||
username = us.build_unique_username_from_name(name, last_name)
|
username = us.build_anonymous_username()
|
||||||
|
|
||||||
permission_preset = (request.form.get('permission_preset') or 'standard_user').strip()
|
permission_preset = (request.form.get('permission_preset') or 'standard_user').strip()
|
||||||
use_custom_permissions = request.form.get('use_custom_permissions') == 'on'
|
use_custom_permissions = request.form.get('use_custom_permissions') == 'on'
|
||||||
|
|||||||
+24
@@ -13,6 +13,8 @@ Provides methods for creating, validating, and retrieving user information.
|
|||||||
import hashlib
|
import hashlib
|
||||||
import copy
|
import copy
|
||||||
import re
|
import re
|
||||||
|
import secrets
|
||||||
|
import string
|
||||||
from bson.objectid import ObjectId
|
from bson.objectid import ObjectId
|
||||||
import settings as cfg
|
import settings as cfg
|
||||||
from settings import MongoClient
|
from settings import MongoClient
|
||||||
@@ -57,6 +59,12 @@ def build_name_synonym(first_name, last_name=''):
|
|||||||
return combined[:6].title()
|
return combined[:6].title()
|
||||||
|
|
||||||
|
|
||||||
|
def _generate_anonymous_username(prefix='user', length=8):
|
||||||
|
"""Generate a random anonymized username using lowercase letters and digits."""
|
||||||
|
alphabet = string.ascii_lowercase + string.digits
|
||||||
|
return prefix + ''.join(secrets.choice(alphabet) for _ in range(length))
|
||||||
|
|
||||||
|
|
||||||
def build_username_from_name(first_name, last_name=''):
|
def build_username_from_name(first_name, last_name=''):
|
||||||
"""
|
"""
|
||||||
Build a deterministic username abbreviation from first and last name.
|
Build a deterministic username abbreviation from first and last name.
|
||||||
@@ -73,6 +81,22 @@ def build_username_from_name(first_name, last_name=''):
|
|||||||
return alias.lower()
|
return alias.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def build_anonymous_username():
|
||||||
|
"""
|
||||||
|
Build a truly anonymized username that is not derived from the user's
|
||||||
|
first name or last name.
|
||||||
|
"""
|
||||||
|
for _ in range(10):
|
||||||
|
candidate = _generate_anonymous_username()
|
||||||
|
if not get_user(candidate):
|
||||||
|
return candidate
|
||||||
|
|
||||||
|
suffix = 2
|
||||||
|
while get_user(f"user{suffix}"):
|
||||||
|
suffix += 1
|
||||||
|
return f"user{suffix}"
|
||||||
|
|
||||||
|
|
||||||
def build_unique_username_from_name(first_name, last_name=''):
|
def build_unique_username_from_name(first_name, last_name=''):
|
||||||
"""
|
"""
|
||||||
Build a unique username based on the abbreviation logic.
|
Build a unique username based on the abbreviation logic.
|
||||||
|
|||||||
Reference in New Issue
Block a user