Debut reimplementation connexion LDAP avec ldap3

This commit is contained in:
Paul Marillonnet 2017-02-27 15:56:14 +01:00
parent 4e346d6151
commit a0f7cb7a3d
2 changed files with 49 additions and 9 deletions

View File

@ -3,12 +3,16 @@ import urllib
import urllib2
import urlparse
from ldap3 import Server, Connection, ALL
from ldap3 import Server, Connection, ALL, SUBTREE, ALL_ATTRIBUTES
wcs_base = "http://wcs.example.com"
form_slug = "/traitement/"
ldap_attribute_mapping = { "a":"b",
"c":"d"}
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
@ -21,7 +25,39 @@ def get_form_entry(wcs_entry_id):
# Bind to local OpenLDAP server
def ldap_init():
# Admin DN:
who = "cn=admin,dc=entrouvert,dc=lan"
# Credentials: XXX
cred = "test"
server = Server('spare.entrouvert.lan')
conn = Connection(server)
conn = Connection(server, user=who, password=cred)
res = conn.bind()
return res
return conn
def ldap_terminate(conn):
conn.unbind()
return 0
def ldap_max_uidnumber():
l = ldap_init()
# We need to iterate the whole user list in
# the default base DN:
l.search(search_base = base,
search_filter = '(uid=*)',
search_scope = scope,
attributes = ALL_ATTRIBUTES)
max_uidnumber = 0
for entry in l.response:
#max_uidnumber = max_uidnumber+" <br><br> "+str(entry['attributes']['uidNumber'])
if entry['attributes']['uidNumber'] > max_uidnumber:
max_uidnumber = entry['attributes']['uidNumber']
ldap_terminate(l)
return max_uidnumber
def ldap_add_entry(form):
return "TODO"

View File

@ -11,7 +11,7 @@ from django.views.generic import View, UpdateView, CreateView
# Add library search path entry?
from passerelle import utils
from .utils import ldap_init, get_form_entry
from .utils import get_form_entry, ldap_max_uidnumber
#TODO
# derive csv connector
@ -24,13 +24,17 @@ def dummy_view(request):
def wcs(request, slug="", wcs_entry_id=0):
json_response = get_form_entry(wcs_entry_id)
# Initiate the LDAP connection:
#res = ldap_init() #OK
#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']))
#dict = json.loads(json_response)
#return HttpResponse(str(dict['fields']))
#return HttpResponse(str("foo"))
#res = ldap_init() #OK
res = ldap_max_uidnumber()
return HttpResponse(str(res))
class LDAPView(View, SingleObjectMixin):
def get(self, request, *args, **kwargs):