-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchAllUsers-abstraction.py
56 lines (40 loc) · 1.56 KB
/
searchAllUsers-abstraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from ldap3 import Server, Connection, ObjectDef, AttrDef, Reader, Writer, ALL
import pprint
## Python replacement for SDI for verifying and manipulating LDAP and user data
# Abstraction https://ldap3.readthedocs.io/en/latest/abstraction.html
## Iterator
ldap_host = 'ldap://<server>:<port>'
bind_user = '<bind user>'
bind_pwd = '<bind_pwd>'
basepoint = '<basepoint>'
# Not that much error handling yet, a connection fail will end all or skip entry
# Error / Exception handling may need looking into
# Connect to server
server = Server(ldap_host, get_info=ALL)
with Connection(server, bind_user , bind_pwd, auto_bind=True) as conn:
# Do search using abstration layer
# Define object to be read back (i.e. objectclass)
# We want to work with persons objects
# In the background, this will fetch the schema from the server
obj_person = ObjectDef('inetorgperson', conn)
#print(obj_person)
# This reader object will will use
r = Reader(conn, obj_person, basepoint, '(uid=*)')
# Do the paged search
entries = r.search_paged(paged_size=2)
# Do the search (without pages)
#entries = r.search()
print(r)
for entry in entries:
#print(entry.entry_to_ldif())
print("The name is " + entry.sn.value + " for user [" + entry.uid.value + "]" )
'''
if (entry.uid.value == "ville"):
# Let's make the entry writeable
print("Found kalle")
e = entry.entry_writable()
e.sn="Viking"
print(e)
e.entry_commit_changes()
print(e)
'''