I have a project in django where i try to connect with ldap server. All seems works well, because when i login in django, the user is added to the django database.
The problem is that I made a custom user with the department field in django and I want to get a concret attribute from ldap and assign with the field department, but when I login i see in django logs this line:
search_s ('ou=xxx,o=xxx, 2 '(uid=%user)s)') returned 1 objects cn=usern_name,ou=xxx,o=xxx
Creating Django user user_name
Populating Django user user_name
cn=user_name,ou=xxx,o=xxx does not have a value for the attribute aaeAppAdd
Then I see in department field in Django user database that is empty.
How I can see or get the user_object attributes (print, etc) that the LDAP offer when i login correct?
These are my config files.
settings.py
# Config to authenticate with LDAP
import ldap
from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion, GroupOfNamesType
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
basedn = "ou=xxx,o=xxx"
AUTH_LDAP_SERVER_URI = 'ldap://ldapserver:389'
AUTH_LDAP_BIND_DN = ""
AUTH_LDAP_BIND_PASSWORD = ""
AUTH_LDAP_USER_SEARCH = LDAPSearch(basedn, ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail",
"department": "aaeAppAdd"
}
AUTH_LDAP_ALWAYS_UPDATE_USER = True
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {"console": {"class": "logging.StreamHandler"}},
"loggers": {"django_auth_ldap": {"level": "DEBUG", "handlers": ["console"]}},
}
############################## django-auth-ldap debug ##############################
if DEBUG:
import logging, logging.handlers
logfile = BASE_DIR / 'django-ldap-debug.log'
my_logger = logging.getLogger('django_auth_ldap')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
logfile, maxBytes=1024 * 500, backupCount=5)
my_logger.addHandler(handler)
model.py of the account app
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
department = models.CharField(blank=True, max_length=15)
views.py of account app
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from accounts.models import CustomUser
# Create your views here.
@login_required
def userinfo(request):
try:
# ldapuserprofile = CustomUser.objects.get(uid=request.user.username)
ldapuserprofile = CustomUser.objects.get(request.user.username)
except CustomUser.DoesNotExist:
return HttpResponseRedirect('/login/')
for attr in ldapuserprofile:
print(attr)
context = {'request': request, 'ldapuser': ldapuserprofile,}
return render(request, 'userinfo.html', context)
urls.py
from django.urls import path
from accounts import views
urlpatterns = [
path('userinfo/', views.userinfo),
]
When I try to call the url appears me en arror with
"too many values to unpack (expected2)"
Using python ldap script i can get this attribute aaeAppAdd with others attributes about user info.
Thanks