diff --git a/luser/models.py b/luser/models.py index b4244e5..4be8f93 100644 --- a/luser/models.py +++ b/luser/models.py @@ -13,9 +13,27 @@ class LUSER(): ''' + def findlastuid(self): + ''' + Return the largest uidNumber attribute of all users in base + ''' + self.ldapconnection.search(search_base=self.base,search_filter=f'(objectClass=inetOrgPerson)', attributes=['uidNumber']) + + alluids = self.ldapconnection.response + + max = 0 + + for i in alluids: + i_uid = i['attributes']['uidNumber'] + if i_uid > max: + max = i_uid + + return max + def expandbase(self, basealt = ''): ''' Extract orgnaization, name of dc object and full domain part with all dc values from base + basealt := string base in LDAP system where users are made, if not set the function uses base specified on creation of LUSER instance (self.base) ''' # Split base string with commas to find values of organization and dc if basealt == '': @@ -52,7 +70,7 @@ class LUSER(): return organization, dc, dcfull, domain - def __init__(self, ldap_host, admin_user, admin_pass, base, basealt='', autoconnect=True): + def __init__(self, ldap_host, admin_user, admin_pass, base, basealt='', autoconnect=True, lastUID = 1000): self.ldap_host = ldap_host self.admin_user = admin_user self.admin_pass = admin_pass @@ -63,12 +81,21 @@ class LUSER(): self.alt = True self.autoconnect = autoconnect ldapserver = Server(ldap_host, use_ssl=True) + lastuidfound = 0 if self.autoconnect: self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=True) + # uid and gid of most recently registered users + lastuidfound = self.findlastuid() + else: + self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=False) + + if lastuidfound == 0: + self.lastuid = lastUID + self.lastgid = lastUID + else: + self.lastuid = lastuidfound + self.lastgid = lastuidfound - # uid and gid of most recently registered users - self.lastuid = 1337 - self.lastgid = 1337 # Set alt boolean to false if basealt not set if basealt == '': @@ -115,7 +142,6 @@ class LUSER(): self.lastuid += 1 self.lastgid += 1 - # Add user to base id = f"uid={user}"