-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_faculty.py
More file actions
71 lines (56 loc) · 1.81 KB
/
sync_faculty.py
File metadata and controls
71 lines (56 loc) · 1.81 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from ldap3 import Server, Connection, ALL
from peewee import *
from api.everything import *
cfg = load_config('api/config.yaml')
# skt = load_config('api/secret_config.yaml')
theStaticDB = SqliteDatabase(cfg['databases']['static'])
theStaticDB.drop_table(LDAPFaculty)
server = Server ('berea.edu', port=389, use_ssl=False, get_info='ALL')
# conn = Connection (server, user=skt['ldap']['user'], password=skt['ldap']['pass'])
conn = Connection (server, user="BPLP", password="Ol1v4r!Tun4bp")
if not conn.bind():
print('error in bind', conn.result)
# search_base and search_filter are the parameters
conn.search('dc=berea,dc=edu',
'(description=Faculty)',
attributes = ['samaccountname', 'givenname', 'sn', 'employeeid']
)
print ("Found {0} faculty.".format(len(conn.entries)))
# print (conn.entries[0])
def safe (d, k):
result = ""
try:
if k in d:
result = d[k]
# If we can't find a key, skip it.
except:
pass
return result
# Recreate the table.
theStaticDB.create_table(LDAPFaculty)
faculty = conn.entries
for fac in faculty:
print ("Faculty: {0}".format(fac.samaccountname))
o = LDAPFaculty (
lastname = safe (fac, 'sn'),
firstname = safe (fac, 'givenname'),
username = fac.samaccountname,
bnumber = safe (fac, 'employeeid'),
)
o.save()
# Now do Staff
conn.search('dc=berea,dc=edu',
'(description=Staff)',
attributes = ['samaccountname', 'givenname', 'sn', 'employeeid']
)
print ("Found {0} staff.".format(len(conn.entries)))
faculty = conn.entries #actually staff, but who cares?
for fac in faculty:
print ("Staff: {0}".format(fac.samaccountname))
o = LDAPFaculty (
lastname = safe (fac, 'sn'),
firstname = safe (fac, 'givenname'),
username = fac.samaccountname,
bnumber = safe (fac, 'employeeid'),
)
o.save()