username would consist of only alphanumerics, ldap fields are encoded in utf-8

This commit is contained in:
ahmadbilalkhalid 2019-12-13 17:52:00 +05:00
parent c96aff16af
commit b4995336c6
3 changed files with 15 additions and 13 deletions

View file

@ -1 +1 @@
10178
10185

View file

@ -82,8 +82,9 @@ def assign_username(user):
# 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())
user.username = first_name + last_name
user.username = "".join(user.username.split()).lower()
user.username = "".join([char for char in user.username if char.isalnum()])
exist = True
while exist:
@ -91,14 +92,14 @@ def assign_username(user):
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))
user.username = user.username + str(random.randint(0, 2 ** 10))
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))
user.username = user.username + str(random.randint(0, 2 ** 10))
class CustomUser(AbstractBaseUser, PermissionsMixin):

View file

@ -88,23 +88,23 @@ class LdapManager:
logger.debug("{uid} does not exist. Using it".format(uid=uidNumber))
self._set_max_uid(uidNumber)
try:
uid = user
uid = user.encode("utf-8")
conn.add("uid={uid},{customer_dn}".format(
uid=uid, customer_dn=settings.LDAP_CUSTOMER_DN
),
["inetOrgPerson", "posixAccount", "ldapPublickey"],
{
"uid": [uid],
"sn": [lastname],
"givenName": [firstname],
"sn": [lastname.encode("utf-8")],
"givenName": [firstname.encode("utf-8")],
"cn": [uid],
"displayName": ["{} {}".format(firstname, lastname)],
"displayName": ["{} {}".format(firstname, lastname).encode("utf-8")],
"uidNumber": [str(uidNumber)],
"gidNumber": [str(settings.LDAP_CUSTOMER_GROUP_ID)],
"loginShell": ["/bin/bash"],
"homeDirectory": ["/home/{}".format(user)],
"mail": email,
"userPassword": [password]
"homeDirectory": ["/home/{}".format(user).encode("utf-8")],
"mail": email.encode("utf-8"),
"userPassword": [password.encode("utf-8")]
}
)
logger.debug('Created user %s %s' % (user.encode('utf-8'),
@ -139,7 +139,7 @@ class LdapManager:
{
"userpassword": (
ldap3.MODIFY_REPLACE,
[new_password]
[new_password.encode("utf-8")]
)
}
)
@ -151,6 +151,7 @@ class LdapManager:
conn.unbind()
return return_val
def change_user_details(self, uid, details):
"""
Updates the user details as per given values in kwargs of the user