Merge branch 'kerl/username_generation' into 'master'

Génération de username plus intelligente

Closes #6 and #5

See merge request klub-dev-ens/authens!8
This commit is contained in:
Ludovic Stephan 2020-05-24 22:52:38 +02:00
commit f43036d6c0

View file

@ -74,21 +74,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.