-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
86 lines (66 loc) · 2.26 KB
/
util.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import json
import ldap
def dn2rdns(dn):
rdns = {}
r = ldap.dn.str2dn(dn)
for rdn in r:
a, v, t = rdn[0]
rdns.setdefault(a, []).append(v)
return rdns
def find_cos(c, service):
cos = {}
r = c.find(None, "(&(objectClass=organization)(labeledURI={}))".format(service),
['o', 'dnQualifier', 'description'])
for dn, entry in r.items():
rdns = dn2rdns(dn)
j = entry.get('description', None)
description = {}
if j:
try:
description = json.loads(j[0])
except Exception as e:
print("find_cos: {}".format(e))
if entry.get('dnQualifier', None):
cos[rdns['o'][0]] = entry['dnQualifier']
elif description.get('identifier', None):
cos[rdns['o'][0]] = [description['identifier']]
elif description.get('comanage_id', None):
cos[rdns['o'][0]] = [description['comanage_id']]
else:
cos[rdns['o'][0]] = entry['o']
return cos
def find_services(c):
services = []
r = c.find(None, '(&(objectClass=organization)(labeledURI=*))', ['labeledURI'])
for dn, entry in r.items():
services.extend(entry['labeledURI'])
return list(set(services))
def find_ordered_services(c):
services = []
r = c.find(None, '(objectClass=dcObject)', ['dc'], scope=ldap.SCOPE_ONELEVEL)
for dn, entry in r.items():
services.extend(entry['dc'])
return list(set(services))
def find_collaborations(c, services):
col = {}
for service in services:
col[service] = []
cos = find_cos(c, service)
for co in cos.items():
col[service].append(co)
return col
def find_ordered_collaborations(c, services):
col = {}
for service in services:
col[service] = []
cos = c.rfind(f"dc=ordered,dc={service}", "(objectClass=organization)", ['o'], scope=ldap.SCOPE_ONELEVEL)
for co, entry in cos.items():
col[service].append(entry['o'][0])
return col
def uid(user):
username = user.get('uid', None)
if not username:
raise Exception("User: {} does not contain username")
if isinstance(username, list):
username = username[0]
return ldap.dn.escape_dn_chars(username)