json2ldap/json2ldap.py

57 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-01-04 15:57:14 +01:00
import sys
import json
from ldap3 import Server, Connection, ALL
2024-01-04 15:57:14 +01:00
BASEDN = 'dc=nantes,dc=omero-fbi,dc=fr'
USERSDN = 'cn=users,'+BASEDN
2024-01-04 15:57:14 +01:00
serv = Server('10.54.3.60', get_info=ALL)
2024-01-04 14:43:04 +01:00
conn = Connection(serv, user='cn=admin,dc=nantes,dc=omero-fbi,dc=fr', password='Jps55Sk8An9y2nVL')
if not conn.bind():
print('error in bind', conn.result)
2024-01-04 15:57:14 +01:00
def adduser():
with open(DATA_FILE, "r", encoding="utf8") as file:
json_user = json.load(file)
ldap_full_username = json_user["params"]["attributes"]["uid"]
ldap_full_passwd = json_user["params"]["attributes"]["userPassword"]
ldap_givenname = json_user["params"]["attributes"]["givenName"]
ldap_surname = json_user["params"]["attributes"]["sn"]
ldap_cn = json_user["params"]["attributes"]["cn"]
ldap_homedir = json_user["params"]["attributes"]["homeDirectory"]
ldap_mail = json_user["params"]["attributes"]["mail"]
ldap_uid_number = json_user["params"]["attributes"]["uidNumber"]
ldap_gid_number = json_user["params"]["attributes"]["gidNumber"]
ldap_user_dn = 'uid=' + ldap_full_username + ',' + USERSDN
conn.add(
ldap_user_dn,
[
'inetOrgPerson',
'posixAccount',
'person',
'top'
],
{
'sn': ldap_surname,
'gidNumber': ldap_gid_number,
'uidNumber': ldap_uid_number,
'mail': ldap_mail,
'homeDirectory': ldap_homedir,
'userPassword': ldap_full_passwd,
'givenName': ldap_givenname,
'uid': ldap_full_username,
'cn': ldap_cn
}
)
for arg in sys.argv[1:]:
print(arg)
DATA_FILE = arg
adduser()
2024-01-04 14:43:04 +01:00
print(conn.result)
conn.unbind()