More clever username generation
This commit is contained in:
parent
e9e8fe8d56
commit
2335e9da04
1 changed files with 17 additions and 12 deletions
|
@ -70,21 +70,26 @@ class ENSCASBackend:
|
|||
worry about data races).
|
||||
"""
|
||||
|
||||
# Ideally we can have username = cas_login.
|
||||
if not UserModel.objects.filter(username=cas_login).exists():
|
||||
return cas_login
|
||||
|
||||
# Otherwise, add a numerical suffix.
|
||||
|
||||
# If there was a numerical suffix already, remove it:
|
||||
# https://git.eleves.ens.fr/klub-dev-ens/authens/issues/5
|
||||
i = len(cas_login) - 1
|
||||
while i >= 0 and cas_login[i] in "0123456789":
|
||||
i -= 1
|
||||
radical = cas_login[:i + 1]
|
||||
|
||||
# Find an integer i such that radical + str(i) is not taken.
|
||||
taken = UserModel.objects.values_list("username", flat=True)
|
||||
|
||||
# This should handle most cases and produce a nice username.
|
||||
prefered = [cas_login, "cas_" + cas_login]
|
||||
pref_taken = taken.filter(username__in=prefered)
|
||||
for name in prefered:
|
||||
if name not in pref_taken:
|
||||
return name
|
||||
|
||||
# Worst case: generate a username of the form cas_login + int
|
||||
taken = taken.filter(username__startswith=cas_login)
|
||||
taken = taken.filter(username__startswith=radical)
|
||||
i = 2
|
||||
while cas_login + str(i) in taken:
|
||||
while radical + str(i) in taken:
|
||||
i += 1
|
||||
return cas_login + str(i)
|
||||
return radical + str(i)
|
||||
|
||||
def _get_or_create(self, cas_login, entrance_year):
|
||||
"""Handles account retrieval, creation and invalidation as described above.
|
||||
|
|
Loading…
Reference in a new issue