Ajout LDAP ok

TODO Connecteur generique, avec formulaire de definition du schema ?
This commit is contained in:
Paul Marillonnet 2017-02-27 16:27:26 +01:00
parent bf60d65925
commit 38cf2b64dd
2 changed files with 11 additions and 26 deletions

View File

@ -8,19 +8,17 @@ from ldap3 import Server, Connection, ALL, SUBTREE, ALL_ATTRIBUTES
wcs_base = "http://wcs.example.com"
form_slug = "/traitement/"
base = "ou=People,dc=entrouvert,dc=lan"
#scope = ldap.SCOPE_SUBTREE
scope = SUBTREE
pocform = 'traitement'
#ldap_attribute_mapping = { "nom":"",
# "c":"d"}
# Simple w.c.s. <-> Passerelle REST communication
def get_form_entry(wcs_entry_id):
data_uri = "/api/forms"+form_slug+str(wcs_entry_id)
geturl = wcs_base+data_uri
# Simple HTTP GET request:
req = urllib2.urlopen(geturl)
#return json.dumps(req.read())
return req.read()
# Bind to local OpenLDAP server
@ -29,8 +27,9 @@ def ldap_init():
who = "cn=admin,dc=entrouvert,dc=lan"
# Credentials: XXX
cred = "test"
# The local server:
server = Server('spare.entrouvert.lan')
# Authenticated binding:
conn = Connection(server, user=who, password=cred)
res = conn.bind()
return conn
@ -49,6 +48,7 @@ def ldap_max_uidnumber():
search_scope = scope,
attributes = 'uidNumber')
# What is the maximum uidNumber ?
max_uidnumber = 0
for entry in l.response:
if entry['attributes']['uidNumber'] > max_uidnumber:
@ -59,16 +59,16 @@ def ldap_max_uidnumber():
return max_uidnumber
def ldap_add_entry(id):
# uid and uidNumber on-the-fly production:
# Avoid any uidNumber conflict:
uidNumber = ldap_max_uidnumber()+1
#uid = ldap_craft_uid(id)
# The to-be-added entry DN.
# the "dn: " prefix musn't appear here:
# The newly created entry DN.
# (the "dn: " prefix musn't appear here):
dn = 'uid='+id['nameid']+","+base
objectClass = ['inetOrgPerson', 'organizationalPerson', 'person', 'posixAccount', 'top']
# All the entry attributes can be defind in a dictionary as below:
# All the entry attributes can be defined in a dictionary as below:
addmod = {}
addmod['cn'] = id['prenom']+" "+id['nom']
addmod['uid'] = id['nameid']
@ -77,17 +77,12 @@ def ldap_add_entry(id):
addmod['sn'] = id['nom']
addmod['homeDirectory'] = "/home/"+id['nameid']
# From here we get an addModlist intelligible to python-ldap:
#ldif = modlist.addModlist(addmod)
l = ldap_init()
# Do not forget the entry DN:
ret = l.add(dn, objectClass, addmod)
ldap_terminate(l)
# Temporary debug output
return str(dn)+"\n"+str(addmod)+"\n"+str(ret)
#return str(addmod)
return ret

View File

@ -23,17 +23,7 @@ def dummy_view(request):
def wcs(request, slug="", wcs_entry_id=0):
json_response = get_form_entry(wcs_entry_id)
# Initiate the LDAP connection:
#return HttpResponse("Querying w.c.s "+str(res))
#return HttpResponse(str(wcs_entry_id))
#return HttpResponse(str(json.dumps(json_response)))
dict = json.loads(json_response)
#return HttpResponse(str(dict['fields']))
#return HttpResponse(str("foo"))
#res = ldap_init() #OK
#res = ldap_max_uidnumber()
res = ldap_add_entry(dict['fields'])
return HttpResponse(str(res))