8570b8660f
Adds a normalized name field to the user record. Also uses normalized names conflicts to deny new user names.
43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from config import V5Config
|
|
from app.utils.unicode_names import normalize
|
|
import re
|
|
|
|
def valid_name(name, msg=False):
|
|
"""
|
|
Checks whether a string is a valid user name. The criteria are:
|
|
1. At least 3 characters and no longer than 32 characters
|
|
2. Only characters allowed in the [unicode_names] utility
|
|
3. At least one letter character (avoid complete garbage)
|
|
4. Not in forbidden user names
|
|
|
|
Returns True if the name is valid, otherwise a list of error messages
|
|
that can contain these errors:
|
|
"too-short", "too-long", "cant-normalize", "no-letter", "forbidden"
|
|
Otherwise, returns a bool.
|
|
"""
|
|
|
|
errors = []
|
|
|
|
# Rule 1
|
|
if len(name) < V5Config.USER_NAME_MINLEN:
|
|
errors.append("too-short")
|
|
|
|
if len(name) > V5Config.USER_NAME_MAXLEN:
|
|
errors.append("too-long")
|
|
|
|
# Rule 2
|
|
try:
|
|
normalize(name)
|
|
except ValueError:
|
|
errors.append("cant-normalize")
|
|
|
|
# Rule 3
|
|
if re.search(r'\w', name) is None:
|
|
errors.append("no-letter")
|
|
|
|
# Rule 4
|
|
if name in V5Config.FORBIDDEN_USERNAMES:
|
|
errors.append("forbidden")
|
|
|
|
return True if errors == [] else errors
|