Add code to create user's unix profile also
This commit is contained in:
parent
f3d90a2b7d
commit
c72f97de42
2 changed files with 30 additions and 5 deletions
|
@ -220,7 +220,7 @@ LDAP_MAX_UID_PATH = os.path.join(
|
|||
'ldap_max_uid_file'
|
||||
)
|
||||
|
||||
IPV6_WORK_USER_GROUP = config('IPV6_WORK_USER_GROUP', cast=int)
|
||||
LDAP_IPV6_WORK_USER_GROUP = config('LDAP_IPV6_WORK_USER_GROUP', cast=int)
|
||||
|
||||
|
||||
def set_max_uid(max_uid):
|
||||
|
|
|
@ -1,17 +1,38 @@
|
|||
from django.conf import settings
|
||||
from ldap3 import Server, ServerPool, Connection, ObjectDef, AttrDef, Reader, Writer
|
||||
from ldap3 import Server, Connection, ObjectDef, Writer, SUBTREE
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
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():
|
||||
logger.error("conn.bind() returned False. Could not connect.")
|
||||
raise Exception('Could not connect to LDAP Server')
|
||||
obj_new_user = ObjectDef(
|
||||
['inetOrgPerson'], conn)
|
||||
obj_new_user = ObjectDef(['inetOrgPerson', 'posixAccount'], conn)
|
||||
uid = settings.get_max_uid() + 1
|
||||
results = True
|
||||
while results:
|
||||
results = conn.search(
|
||||
search_base=settings.LDAP_SEARCH_BASE,
|
||||
search_filter=(
|
||||
'(&(objectClass=inetOrgPerson)(objectClass=posixAccount)'
|
||||
'(objectClass=top)(uidNumber={uidNumber}))'.format(
|
||||
uidNumber=uid
|
||||
)
|
||||
),
|
||||
search_scope=SUBTREE,
|
||||
attributes=['uidNumber'],
|
||||
)
|
||||
if results:
|
||||
logger.debug("{uid} exists. Trying next.".format(uid=uid))
|
||||
uid += 1
|
||||
else:
|
||||
logger.debug("{uid} does not exist. Using it".format(uid=uid))
|
||||
|
||||
w = Writer(conn, obj_new_user)
|
||||
dn = 'uid=%s,ou=users,dc=ungleich,dc=ch' % user
|
||||
w.new(dn)
|
||||
|
@ -20,9 +41,13 @@ def create_user(user, password, firstname, lastname, email):
|
|||
w[0].cn = firstname + " " + lastname
|
||||
w[0].mail = email
|
||||
w[0].userPassword = password
|
||||
w[0].gidNumber = settings.IPV6_WORK_USER_GROUP
|
||||
w[0].uidNumber = uid
|
||||
w[0].homeDirectory = "/home/" + user
|
||||
|
||||
if not w.commit():
|
||||
conn.unbind()
|
||||
logger.error("w.commit() returned False. Could not write user.")
|
||||
raise Exception("Couldn't write user")
|
||||
conn.unbind()
|
||||
return True
|
||||
|
|
Loading…
Reference in a new issue