Fix LDAP bugs and and remove_from_group

This commit is contained in:
Tanner Collin 2020-09-10 20:29:29 -06:00
parent 707e89595e
commit 6cd0ea7bd0

View File

@ -148,11 +148,32 @@ def add_to_group(groupname,username):
group_dn = find_group(groupname) group_dn = find_group(groupname)
user_dn = find_user(username) user_dn = find_user(username)
# -- TODO: Check to see if user is already a member, skip if not needed (Done)
if not is_member(groupname, username): if not is_member(groupname, username):
mod_acct = [(ldap.MOD_ADD, 'member', user_dn.encode())] mod_acct = [(ldap.MOD_ADD, 'member', user_dn.encode())]
ldap_conn.modify_s(group_dn, mod_acct) ldap_conn.modify_s(group_dn, mod_acct)
return(True) return True
else:
return False
finally:
ldap_conn.unbind()
def remove_from_group(groupname, username):
'''
Remove a user from a Group; required data is GroupName, Username
'''
ldap_conn = init_ldap()
try:
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
group_dn = find_group(groupname)
user_dn = find_user(username)
if is_member(groupname, username):
mod_acct = [(ldap.MOD_DELETE, 'member', user_dn.encode())]
ldap_conn.modify_s(group_dn, mod_acct)
return True
else:
return False
finally: finally:
ldap_conn.unbind() ldap_conn.unbind()
@ -161,7 +182,6 @@ def list_group(groupname):
''' '''
List users in a Group; required data is GroupName List users in a Group; required data is GroupName
''' '''
members = []
ldap_conn = init_ldap() ldap_conn = init_ldap()
try: try:
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD) ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
@ -169,12 +189,8 @@ def list_group(groupname):
criteria = '(&(objectClass=group)(sAMAccountName={}))'.format(groupname) criteria = '(&(objectClass=group)(sAMAccountName={}))'.format(groupname)
results = ldap_conn.search_s(BASE_GROUPS, ldap.SCOPE_SUBTREE, criteria, ['member'] ) results = ldap_conn.search_s(BASE_GROUPS, ldap.SCOPE_SUBTREE, criteria, ['member'] )
members_tmp = results[0][1]['member'] members_tmp = results[0][1]
for m in members_tmp: return members_tmp.get('member', [])
members.append(m)
return(members)
finally: finally:
ldap_conn.unbind() ldap_conn.unbind()
@ -186,37 +202,33 @@ def is_member(groupname,username):
try: try:
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD) ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
group_dn = find_group(groupname) group_dn = find_group(groupname)
user_dn = find_user(username) user_dn = find_user(username).encode()
memflag = False memflag = False
criteria = '(&(objectClass=group)(sAMAccountName={}))'.format(groupname) criteria = '(&(objectClass=group)(sAMAccountName={}))'.format(groupname)
results = ldap_conn.search_s(BASE_GROUPS, ldap.SCOPE_SUBTREE, criteria, ['member'] ) results = ldap_conn.search_s(BASE_GROUPS, ldap.SCOPE_SUBTREE, criteria, ['member'] )
members_tmp = results[0][1]['member'] members_tmp = results[0][1]
members = members_tmp.get('member', [])
for m in members_tmp: return user_dn in members
if m == user_dn:
memflag = True
return memflag
finally: finally:
ldap_conn.unbind() ldap_conn.unbind()
def abort(message):
print(message)
exit()
# =========================================================================== # ===========================================================================
if __name__ == '__main__': if __name__ == '__main__':
pass
#print(find_user('tanner.collin')) #print(find_user('tanner.collin'))
#print(set_password('tanner.collin', 'Supersecret@@')) #print(set_password('tanner.collin', 'Supersecret@@'))
print("============================================================") #print("============================================================")
print(create_group("newgroup","new group")) #print(create_group("newgroup", "new group"))
print(" ============== ") #print(" ============== ")
print(list_group("newgroup")) #print(list_group("newgroup"))
print(" ============== ") #print(" ============== ")
print(is_member('newgroup','tanner.collin')) #print(is_member('newgroup','tanner.collin'))
print(" ============== ") #print(" ============== ")
print(add_to_group('newgroup','tanner.collin')) #print(add_to_group('newgroup','tanner.collin'))
print(" ============== ") #print(" ============== ")
print(list_group("newgroup")) #print(list_group("newgroup"))
#print(" ============== ")
#print(remove_from_group('newgroup','tanner.collin'))
#print(" ============== ")
#print(list_group("newgroup"))