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 # Try to come up with a username
first_name, last_name = get_first_and_last_name(user.name) first_name, last_name = get_first_and_last_name(user.name)
user.username = first_name.lower() + last_name.lower() user.username = first_name + last_name
user.username = "".join(user.username.split()) user.username = "".join(user.username.split()).lower()
user.username = "".join([char for char in user.username if char.isalnum()])
exist = True exist = True
while exist: while exist:
@ -91,14 +92,14 @@ def assign_username(user):
exist, entries = ldap_manager.check_user_exists(user.username) exist, entries = ldap_manager.check_user_exists(user.username)
if exist: if exist:
# If username exists in ldap, come up with a new user name and check it again # 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: else:
# If username does not exists in ldap, try to save it in database # If username does not exists in ldap, try to save it in database
try: try:
user.save() user.save()
except IntegrityError: except IntegrityError:
# If username exists in database then come up with a new username # 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): class CustomUser(AbstractBaseUser, PermissionsMixin):

View file

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