0

i need some help if someone run into the same problem i am having right now, as i have been asked to add support a login using Active Directory, i have managed to succeed the login process and the models creations of users, and now i want to populate some details from AD into the app database, i want to get the manager of manager of an employee, as i have been searching i found that the team lead N+1 field is named "manager" on ad so i have queried this using this command

AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "Name",
    "email": "mail",
    "employee_manager": "manager",
    "employee_office": "physicalDeliveryOfficeName",
    "employee_cost_center": "extensionAttribute8" 
}

The problem here is i got this in my database

CN=TeamLeadName,OU=Users,OU=T,OU=C,OU=C,DC=domain,DC=net 

so know i don't have any idea how to get only the name of the team lead and not the rest and from there how i can get the team of the team lead or in other word the initial employee manager. Any help will be so much appreciated.

Farhani Walid
  • 927
  • 1
  • 9
  • 20

1 Answers1

0

The manager attribute gives you the distinguishedName of the manager's account. You can use that to look up the manager's account and get the name (you'd probably want the displayName).

The django-auth-ldap package has python-ldap as a dependency, so you can use that to look up the account. I'm not a python developer, so I'm sure this needs work, but putting together the documentation and this answer, it should look something like this:

import ldap

...

l = ldap.initialize('ldap://example.com')
r = l.search_s(manager, ldap.SCOPE_BASE, '(objectClass=*)', ['displayName'])

Where manager is the distinguishedName of the manager that you just found, and "example.com" is your domain name.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84