Refactor code

This commit is contained in:
PCoder 2019-02-03 13:31:36 +01:00
parent c72f97de42
commit 88a58595fb
2 changed files with 39 additions and 31 deletions

View file

@ -222,31 +222,9 @@ LDAP_MAX_UID_PATH = os.path.join(
LDAP_IPV6_WORK_USER_GROUP = config('LDAP_IPV6_WORK_USER_GROUP', cast=int) LDAP_IPV6_WORK_USER_GROUP = config('LDAP_IPV6_WORK_USER_GROUP', cast=int)
LDAP_DEFAULT_START_UID = config(
def set_max_uid(max_uid): 'LDAP_DEFAULT_START_UID', cast=int, default=10000
""" )
a utility function to save max_uid value to a file
:param max_uid: an integer representing the max uid
:return:
"""
with open(LDAP_MAX_UID_PATH, 'w+') as handler:
handler.write(max_uid)
def get_max_uid():
"""
A utility function to read the max uid value that was previously set
:return: An integer representing the max uid value that was previously set
"""
try:
with open(LDAP_MAX_UID_PATH, 'r+') as handler:
return int(handler.read())
except FileNotFoundError as fnfe:
logger.error("File not found : " + str(fnfe))
ret = config('DEFAULT_START_UID', cast=int, default=10000)
logger.error("So, returing UID={}".format(ret))
if config('ENABLE_DEBUG_LOG', cast=bool, default=False): if config('ENABLE_DEBUG_LOG', cast=bool, default=False):
loggers_dict = {} loggers_dict = {}

View file

@ -7,13 +7,16 @@ server = Server(settings.AUTH_LDAP_SERVER_URI)
def create_user(user, password, firstname, lastname, email): def create_user(user, password, firstname, lastname, email):
logger.debug("In create_user")
conn = Connection(server, settings.AUTH_LDAP_BIND_DN, conn = Connection(server, settings.AUTH_LDAP_BIND_DN,
settings.AUTH_LDAP_BIND_PASSWORD) settings.AUTH_LDAP_BIND_PASSWORD)
if not conn.bind(): if not conn.bind():
logger.error("conn.bind() returned False. Could not connect.") logger.error("conn.bind() returned False. Could not connect.")
raise Exception('Could not connect to LDAP Server') raise Exception('Could not connect to LDAP Server')
obj_new_user = ObjectDef(['inetOrgPerson', 'posixAccount'], conn) obj_new_user = ObjectDef(['inetOrgPerson', 'posixAccount'], conn)
uid = settings.get_max_uid() + 1 uidNumber = settings.get_max_uid() + 1
logger.debug("uidNumber={uidNumber}".format(uidNumber=uidNumber))
results = True results = True
while results: while results:
results = conn.search( results = conn.search(
@ -21,17 +24,17 @@ def create_user(user, password, firstname, lastname, email):
search_filter=( search_filter=(
'(&(objectClass=inetOrgPerson)(objectClass=posixAccount)' '(&(objectClass=inetOrgPerson)(objectClass=posixAccount)'
'(objectClass=top)(uidNumber={uidNumber}))'.format( '(objectClass=top)(uidNumber={uidNumber}))'.format(
uidNumber=uid uidNumber=uidNumber
) )
), ),
search_scope=SUBTREE, search_scope=SUBTREE,
attributes=['uidNumber'], attributes=['uidNumber'],
) )
if results: if results:
logger.debug("{uid} exists. Trying next.".format(uid=uid)) logger.debug("{uid} exists. Trying next.".format(uid=uidNumber))
uid += 1 uidNumber += 1
else: else:
logger.debug("{uid} does not exist. Using it".format(uid=uid)) logger.debug("{uid} does not exist. Using it".format(uid=uidNumber))
w = Writer(conn, obj_new_user) w = Writer(conn, obj_new_user)
dn = 'uid=%s,ou=users,dc=ungleich,dc=ch' % user dn = 'uid=%s,ou=users,dc=ungleich,dc=ch' % user
@ -42,12 +45,39 @@ def create_user(user, password, firstname, lastname, email):
w[0].mail = email w[0].mail = email
w[0].userPassword = password w[0].userPassword = password
w[0].gidNumber = settings.IPV6_WORK_USER_GROUP w[0].gidNumber = settings.IPV6_WORK_USER_GROUP
w[0].uidNumber = uid w[0].uidNumber = uidNumber
w[0].homeDirectory = "/home/" + user w[0].homeDirectory = "/home/" + user
if not w.commit(): if not w.commit():
conn.unbind() conn.unbind()
logger.error("w.commit() returned False. Could not write user.") logger.error("w.commit() returned False. Could not write user.")
raise Exception("Couldn't write user") raise Exception("Couldn't write user")
logger.debug("Created user {user} successfully.".format(user=user))
conn.unbind() conn.unbind()
return True return True
def set_max_uid(max_uid):
"""
a utility function to save max_uid value to a file
:param max_uid: an integer representing the max uid
:return:
"""
with open(settings.LDAP_MAX_UID_PATH, 'w+') as handler:
handler.write(max_uid)
def get_max_uid():
"""
A utility function to read the max uid value that was previously set
:return: An integer representing the max uid value that was previously set
"""
try:
with open(settings.LDAP_MAX_UID_PATH, 'r+') as handler:
return int(handler.read())
except FileNotFoundError as fnfe:
logger.error("File not found : " + str(fnfe))
ret = settings.LDAP_DEFAULT_START_UID
logger.error("So, returing UID={}".format(ret))