ipv6-dot-work/users/ldap_funcs.py

29 lines
869 B
Python
Raw Normal View History

from django.conf import settings
from ldap3 import Server, ServerPool, Connection, ObjectDef, AttrDef, Reader, Writer
server = Server(settings.AUTH_LDAP_SERVER_URI)
def create_user(user, password, firstname, lastname, email):
conn = Connection(server, settings.AUTH_LDAP_BIND_DN,
settings.AUTH_LDAP_BIND_PASSWORD)
if not conn.bind():
raise Exception('Could not connect to LDAP Server')
obj_new_user = ObjectDef(
['inetOrgPerson'], conn)
w = Writer(conn, obj_new_user)
2018-11-16 14:53:16 +00:00
dn = 'uid=%s,ou=users,dc=ungleich,dc=ch' % user
w.new(dn)
w[0].givenName = firstname
w[0].sn = lastname
w[0].cn = firstname + " " + lastname
w[0].mail = email
w[0].userPassword = password
if not w.commit():
conn.unbind()
raise Exception("Couldn't write user")
conn.unbind()
2018-11-16 14:53:16 +00:00
return True