def manager_country_attribute_populate(sender, user, ldap_user, **kwargs): try: profile_map = settings.AUTH_LDAP_USER_ATTR_MAP except AttributeError: profile_map = {} if 'manager' in profile_map: if profile_map['manager'] in ldap_user.attrs: manager_ref = force_text( ldap_user.attrs[profile_map['manager']][0]) # CN=John Smith,OU=TOR,OU=Corp-Users,DC=mydomain,DC=internal cn = manager_ref.split(',')[0][3:] user.manager = cn # raw value from LDAP is in profile.country for this reason we assign # some correct value user.country = None if 'country' in profile_map: if profile_map['country'] in ldap_user.attrs: country = force_text(ldap_user.attrs[profile_map['country']][0]) # assign None if `country` doesn't exist in Country try: user.country = Country.id_from_name(country.lower()) except ValueError: user.country = None