Gitlab CSE Unil

Commit b223a426 authored by Julien Furrer's avatar Julien Furrer
Browse files

Bug fix (adim_ttp): problème de longueur du username si email > 30

Le user est cherché sur email (plus sur username). Le username est une version tronquée à 30 de email.
Unicité de username est garantie, username modifié si un autre existait déjà (incrémentation -> valeur unique)
parent 67a85396
......@@ -13,6 +13,31 @@ except ImportError:
ShibbUserAttributes = None
def validate_username(email):
Return a unique username based on the email truncated to 30 char.
If a username already exists, try to build a new one by incrementing last char
:param email:
candidate = email[:30]
if not User.objects.filter(username=candidate).exists():
return candidate
# A user exists with that username, change it until it's unique
found = False
inc = 0
while not found:
inc += 1
if inc > 2000:
raise Exception("Unexpected error, max iteration reached")
candidate = candidate[:-len(str(inc))] + str(inc)
if not User.objects.filter(username=candidate).exists():
found = True
return candidate
def get_or_create_user(attp_user):
Try to find an existing user corresponding to the parameters passed by attp_user.
......@@ -23,7 +48,8 @@ def get_or_create_user(attp_user):
# attp_attr is the name of the attribute in attp_user,
# model_attr is the corresponding attribute name on the User model,
# given in attr_choice as (attp_attr, model_attr)
attr_choice = (('id', 'pk'), ('uniqueId', 'shibbuserattributes__uid'), ('email', 'username'))
attr_choice = (('id', 'pk'), ('uniqueId', 'shibbuserattributes__uid'), ('email', 'email'))
# Lookup the user, using the various attributes
user = None
......@@ -46,7 +72,7 @@ def get_or_create_user(attp_user):
# If the user could not be found, create it
if user is None:
user = User(username=attp_user.get('email'), email=attp_user.get('email'))
user = User(username=validate_username(attp_user.get('email')), email=attp_user.get('email'))
user_changed = True
# Update User data
......@@ -59,7 +85,8 @@ def get_or_create_user(attp_user):
user_changed = True
if attp_user.get('email') and != attp_user['email']: # Update email and username if provided = user.username = attp_user['email'] = attp_user['email']
user.username = validate_username(attp_user.get('email'))
user_changed = True
if user_changed:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment