Skip to content

Instantly share code, notes, and snippets.

@etianen
Created November 8, 2017 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etianen/b7535ca5c6f45efa3b540bb92ae8b518 to your computer and use it in GitHub Desktop.
Save etianen/b7535ca5c6f45efa3b540bb92ae8b518 to your computer and use it in GitHub Desktop.
from django_python3_ldap import ldap
from django.contrib.auth.decorators import login_required
@login_required
def ldap_seach(request):
# Because of login_required, this view is protected by the Django login screen.
# request.user will be the logged-in user.
if request.method == "POST":
# The user has submitted the search form of the user to look up.
username_to_look_up = request.post["username"]
# We are using an anonymous LDAP connection to search for the user.
# If your LDAP server requires authentication to look up users, then
# you need to hardcode a username and password here, like:
# with ldap.connection(username="foo", password="bar").
with ldap.connection() as connection:
# Look up the user by username.
user = connectino.get_user(username=username_to_look_up)
print(user)
return render(request, "ldap_search.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment