diff --git a/dynamicweb/settings/ldap_max_uid_file b/dynamicweb/settings/ldap_max_uid_file index 9c1cfb87..4c2a2049 100644 --- a/dynamicweb/settings/ldap_max_uid_file +++ b/dynamicweb/settings/ldap_max_uid_file @@ -1 +1 @@ -10173 \ No newline at end of file +10178 \ No newline at end of file diff --git a/membership/models.py b/membership/models.py index 99180715..ea761d99 100644 --- a/membership/models.py +++ b/membership/models.py @@ -1,4 +1,5 @@ import logging +import random from datetime import datetime from django.conf import settings @@ -77,24 +78,27 @@ def get_first_and_last_name(full_name): def assign_username(user): if not user.username: + ldap_manager = LdapManager() + + # Try to come up with a username first_name, last_name = get_first_and_last_name(user.name) user.username = first_name.lower() + last_name.lower() user.username = "".join(user.username.split()) - try: - user.save() - except IntegrityError: - try: - user.username = user.username + str(user.id) - user.save() - except IntegrityError: - while True: + + exist = True + while exist: + # Check if it exists + exist, entries = ldap_manager.check_user_exists(user.username) + if exist: + # If username exists in ldap, come up with a new user name and check it again + user.username = user.username + str(random.randint(0, 2 ** 50)) + else: + # If username does not exists in ldap, try to save it in database + try: + user.save() + except IntegrityError: + # If username exists in database then come up with a new username user.username = user.username + str(random.randint(0, 2 ** 50)) - try: - user.save() - except IntegrityError: - continue - else: - break class CustomUser(AbstractBaseUser, PermissionsMixin):