many bug fixes
This commit is contained in:
parent
80a64511cd
commit
a1a2bf34b3
55
models.py
55
models.py
@ -8,9 +8,9 @@ USERATTRIBUTES = ['cn' , 'sn', 'givenName', 'uid', 'uidNumber' , 'gidNumber', 'h
|
|||||||
|
|
||||||
|
|
||||||
class logserver():
|
class logserver():
|
||||||
def __init__(self, _host: str, admin_user: str, admin_pass: str, base: str, ssl: bool = True ):
|
def __init__(self, ldap_host: str, admin_user: str, admin_pass: str, base: str, ssl: bool = True ):
|
||||||
|
|
||||||
self._host = ldap_host
|
self.ldap_host = ldap_host
|
||||||
self.admin_user = admin_user
|
self.admin_user = admin_user
|
||||||
self.admin_pass = admin_pass
|
self.admin_pass = admin_pass
|
||||||
self.base = base
|
self.base = base
|
||||||
@ -21,11 +21,11 @@ class logserver():
|
|||||||
else:
|
else:
|
||||||
server = ldap3.Server(self.ldap_host)
|
server = ldap3.Server(self.ldap_host)
|
||||||
|
|
||||||
self.conn = ldap3.Connection(ldapserver, admin_user, admin_pass, auto_bind=True)
|
self.conn = ldap3.Connection(server, admin_user, admin_pass, auto_bind=True)
|
||||||
|
|
||||||
self.organization, self.dc, self.dcfull, self.domain = self.expandbase()
|
self.organization, self.dc, self.dcfull, self.domain = self.expandbase()
|
||||||
|
|
||||||
self.logbase = f'ou=log,{self.dc}'
|
self.logbase = f'ou=log,{self.dcfull}'
|
||||||
|
|
||||||
# Unique id of the log branch
|
# Unique id of the log branch
|
||||||
self.id = self.getid()
|
self.id = self.getid()
|
||||||
@ -35,6 +35,7 @@ class logserver():
|
|||||||
|
|
||||||
# How many changes are recoreded in total
|
# How many changes are recoreded in total
|
||||||
self.total = self.gettotal()
|
self.total = self.gettotal()
|
||||||
|
self.refreshtotal()
|
||||||
|
|
||||||
def expandbase(self, base: str = '')->(str,str,str,str):
|
def expandbase(self, base: str = '')->(str,str,str,str):
|
||||||
'''
|
'''
|
||||||
@ -42,7 +43,7 @@ class logserver():
|
|||||||
l dc values from base
|
l dc values from base
|
||||||
'''
|
'''
|
||||||
if base == '':
|
if base == '':
|
||||||
base = self.logbase
|
base = self.base
|
||||||
|
|
||||||
# Split base string with commas to find values of organization and dc
|
# Split base string with commas to find values of organization and dc
|
||||||
baselist = base.split(",")
|
baselist = base.split(",")
|
||||||
@ -158,7 +159,7 @@ l dc values from base
|
|||||||
if base == '':
|
if base == '':
|
||||||
base = self.logbase
|
base = self.logbase
|
||||||
|
|
||||||
self.settotal(self.findtotal(base))
|
self.settotal(self.findtotal(base), base)
|
||||||
return self.gettotal(base)
|
return self.gettotal(base)
|
||||||
|
|
||||||
def findtotal(self, base: str = '')->int:
|
def findtotal(self, base: str = '')->int:
|
||||||
@ -190,21 +191,29 @@ l dc values from base
|
|||||||
|
|
||||||
log = self.getlog(log_number, base)
|
log = self.getlog(log_number, base)
|
||||||
|
|
||||||
attributes = log['attribtes']
|
if log == []:
|
||||||
uid = attributes['uid']
|
return -1
|
||||||
|
|
||||||
|
attributes = log['attributes']
|
||||||
|
|
||||||
|
for key in attributes:
|
||||||
|
if isinstance(attributes[key], list):
|
||||||
|
attributes[key] = attributes[key][0]
|
||||||
|
|
||||||
|
uid = attributes['cn']
|
||||||
action = attributes['description']
|
action = attributes['description']
|
||||||
attributes['description'] = ''
|
del attributes['description']
|
||||||
|
|
||||||
if action == 'ADD':
|
if action == 'ADD':
|
||||||
self.conn.add(f'uid={uid},{base}', OBJECTCLASSES, attributes)
|
self.conn.add(f'uid={uid},{self.base}', OBJECTCLASSES, attributes)
|
||||||
elif action == 'DELETE':
|
elif action == 'DELETE':
|
||||||
self.conn.delete(f'uid={uid},{base}')
|
self.conn.delete(f'uid={uid},{self.base}')
|
||||||
elif action == 'CHANGEPASS':
|
elif action == 'CHANGEPASS':
|
||||||
self.conn.modify(f'uid={uid},{base}', {'userPassword': (MODIFY_REPLACE,attributes['userPassword'])})
|
self.conn.modify(f'uid={uid},{self.base}', {'userPassword': (ldap3.MODIFY_REPLACE,attributes['userPassword'])})
|
||||||
else:
|
else:
|
||||||
return f'Error: Unrecognized action in log entry: {action}'
|
return f'Error: Unrecognized action in log entry: {action}'
|
||||||
|
|
||||||
self.setloaded(self.getloaded() + 1, base)
|
self.setloaded(self.getloaded(base) + 1, base)
|
||||||
return self.conn.response
|
return self.conn.response
|
||||||
|
|
||||||
def getlog(self, log_number: int, base: str = ''):
|
def getlog(self, log_number: int, base: str = ''):
|
||||||
@ -212,13 +221,20 @@ l dc values from base
|
|||||||
base = self.logbase
|
base = self.logbase
|
||||||
|
|
||||||
self.conn.search(search_base=f'uid={log_number},{base}',search_filter = '(objectClass=person)', attributes = USERATTRIBUTES)
|
self.conn.search(search_base=f'uid={log_number},{base}',search_filter = '(objectClass=person)', attributes = USERATTRIBUTES)
|
||||||
return self.conn.response[0]
|
|
||||||
|
|
||||||
|
response = self.conn.response
|
||||||
|
|
||||||
|
if response == []:
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
return response[0]
|
||||||
|
|
||||||
def setlog(self, attributes: dict, base: str = '', log_number: int = -1):
|
def setlog(self, attributes: dict, base: str = '', log_number: int = -1):
|
||||||
if base == '':
|
if base == '':
|
||||||
base = self.logbase
|
base = self.logbase
|
||||||
|
|
||||||
if log_number == -1:
|
if log_number == -1:
|
||||||
|
self.refreshtotal(base)
|
||||||
log_number = self.gettotal(base) + 1
|
log_number = self.gettotal(base) + 1
|
||||||
|
|
||||||
self.conn.add(f'uid={log_number},{base}', OBJECTCLASSES, attributes)
|
self.conn.add(f'uid={log_number},{base}', OBJECTCLASSES, attributes)
|
||||||
@ -229,7 +245,7 @@ l dc values from base
|
|||||||
Gets the base of a log copy of remote server on the local server
|
Gets the base of a log copy of remote server on the local server
|
||||||
'''
|
'''
|
||||||
remoteid = remote.getid()
|
remoteid = remote.getid()
|
||||||
localdcfull = self.dcfull()
|
localdcfull = self.dcfull
|
||||||
|
|
||||||
basecopy = f'ou=sync-{remoteid},{localdcfull}'
|
basecopy = f'ou=sync-{remoteid},{localdcfull}'
|
||||||
|
|
||||||
@ -241,18 +257,25 @@ l dc values from base
|
|||||||
return basecopy
|
return basecopy
|
||||||
|
|
||||||
def pullfrom(self, source)->int:
|
def pullfrom(self, source)->int:
|
||||||
|
pulled = 0
|
||||||
basecopy = self.getbasecopy(source)
|
basecopy = self.getbasecopy(source)
|
||||||
|
|
||||||
|
self.refreshtotal(basecopy)
|
||||||
|
source.refreshtotal()
|
||||||
|
|
||||||
total_dest = self.gettotal(basecopy)
|
total_dest = self.gettotal(basecopy)
|
||||||
total_src = source.gettotal()
|
total_src = source.gettotal()
|
||||||
|
|
||||||
for log_number in range(total_dest + 1, total_src + 1):
|
for log_number in range(total_dest + 1, total_src + 1):
|
||||||
log = source.getlog(log_number)
|
log = source.getlog(log_number)
|
||||||
|
if log == []:
|
||||||
|
break
|
||||||
attributes = log['attributes']
|
attributes = log['attributes']
|
||||||
|
|
||||||
self.setlog(attributes, basecopy)
|
self.setlog(attributes, basecopy)
|
||||||
|
pulled += 1
|
||||||
|
|
||||||
return total_src - total_dest
|
return pulled
|
||||||
|
|
||||||
def applyfrom(self, source)->int:
|
def applyfrom(self, source)->int:
|
||||||
basecopy = self.getbasecopy(source)
|
basecopy = self.getbasecopy(source)
|
||||||
|
Loading…
Reference in New Issue
Block a user