Adding the user I created in Django to the server with ldap
I want to add the user created in Django to my server via LDAP. Even if I get the message that the connection is successful, it does not continue and gives this warning: LDAP connection successfully. Error occurred during LDAP connection: ('unable to open socket', [(LDAPSocketOpenError('socket connection error while opening: [WinError 10060] I can attract users to my database with the connection I made.
def create_user_in_ldap(request, django_user):
"""
Adds the user created in Django to the LDAP server.
"""
try:
ldap_config = get_object_or_404(LdapConfig,company=request.user.company)
ldap_server = ldap_config.ldap_server
ldap_username = ldap_config.bind_username
ldap_password = ldap_config.bind_password
ldap_domain = ldap_config.bind_dn
server = Server(ldap_server, get_info=ALL)
connection = Connection(
server,
user=f"{ldap_domain}\\{ldap_username}",
password=ldap_password,
authentication=NTLM,
auto_bind=True
)
if not connection.bind():
print(f"LDAP connection unsuccessful: {connection.result}")
return False
print("LDAP connection succesfully")
user_dn = f"cn={django_user.username},ou=Users,dc=example,dc=com"
attributes = {
"objectClass": ["top", "person", "organizationalPerson", "user"],
"cn": django_user.username,
"sn": django_user.last_name or "NoLastName",
"givenName": django_user.first_name or "NoFirstName",
"displayName": f"{django_user.first_name} {django_user.last_name}",
"mail": django_user.email,
"userPassword": "default_password",
}
connection.add(user_dn, attributes=attributes)
print("User add result:", connection.result)
if connection.result["description"] == "success":
print(f"User successfully added to LDAP server {django_user.username}")
return True
else:
print(f"Error adding user: {connection.result['description']}")
return False
except Exception as e:
print(f"Error occurred during LDAP connection: {str(e)}")
return False