-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathimport_users_to_ldap.py
73 lines (60 loc) · 2.75 KB
/
import_users_to_ldap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from ldap3 import ALL, ALL_ATTRIBUTES, Connection, Server
from employee.models import Employee
User = get_user_model()
class Command(BaseCommand):
help = "Import users from Django to LDAP"
def handle(self, *args, **kwargs):
# LDAP server details
ldap_server = "localhost"
bind_dn = "cn=admin,dc=test,dc=com" # Replace with your bind DN
bind_password = "cool" # Change to your LDAP admin password
# Connect to the LDAP server
server = Server(ldap_server, get_info=ALL)
try:
conn = Connection(server, bind_dn, bind_password, auto_bind=True)
# Fetch all users from Django
users = Employee.objects.all()
for user in users:
# Prepare user data for LDAPclear
dn = f"uid={user.employee_user_id.username},ou=users,dc=test,dc=com"
attributes = {
"objectClass": ["inetOrgPerson"],
"givenName": user.employee_first_name,
"sn": user.employee_last_name,
"cn": f"{user.employee_first_name} {user.employee_last_name}",
"uid": user.email,
"mail": user.email,
"telephoneNumber": user.phone,
"userPassword": user.phone,
}
# Check if the user already exists in LDAP
conn.search(
"ou=users,dc=test,dc=com",
f"(uid={user.employee_user_id.username})",
attributes=ALL_ATTRIBUTES,
)
if conn.entries:
self.stdout.write(
self.style.WARNING(
f"{user.employee_first_name} {user.employee_last_name} already exists in LDAP. Skipping..."
)
)
else:
# Add user to LDAP
if not conn.add(dn, attributes=attributes):
self.stdout.write(
self.style.ERROR(
f"Failed to add {user.employee_first_name} {user.employee_last_name}: {conn.result}"
)
)
else:
self.stdout.write(
self.style.SUCCESS(
f"Successfully added {user.employee_first_name} {user.employee_last_name} to LDAP."
)
)
conn.unbind()
except Exception as e:
self.stdout.write(self.style.ERROR(f"An error occurred: {e}"))