django-ldapdb is an LDAP database backend for Django, allowing to manipulate LDAP entries through Django models.
It supports most of the same APIs as a Django model:
MyModel.objects.create()
MyModel.objects.filter(x=1, y__contains=2)
Full admin support and browsing
django-ldapdb supports every upstream-supported Django version, based on the Django support policy.
For the current version, the following versions are supported:
Django 2.2 (LTS), under Python 3.6 - 3.8 (Python 3.5 has reached its end of life);
Django 3.0, under Python 3.6 - 3.8;
Django 3.1, under Python 3.6 - 3.8.
Installing django-ldapdb
Linux
Use pip: pip install django-ldapdb
You might also need the usual LDAP packages from your distribution, usually named openldap or ldap-utils.
Windows
django-ldapdb depends on the python-ldap <https://pypi.python.org/pypi/python-ldap> project. Either follow its Windows installation guide, or install a pre-built version from https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap (choose the .whl file matching your Python/Windows combination, and install it with pip install python-ldap-3...whl).
You may then install django-ldapdb with
pip install django-ldapdb
Using django-ldapdb
Add the following to your settings.py:
DATABASES = {
'ldap': {
'ENGINE': 'ldapdb.backends.ldap',
'NAME': 'ldap://ldap.nodomain.org/',
'USER': 'cn=admin,dc=nodomain,dc=org',
'PASSWORD': 'some_secret_password',
},
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
}
DATABASE_ROUTERS = ['ldapdb.router.Router']
If you want to access posixGroup entries in your application, you can add something like this to your models.py:
from ldapdb.models.fields import CharField, IntegerField, ListField
import ldapdb.models
class LdapGroup(ldapdb.models.Model):
"""
Class for representing an LDAP group entry.
"""
# LDAP meta-data
base_dn = "ou=groups,dc=nodomain,dc=org"
object_classes = ['posixGroup']
# posixGroup attributes
gid = IntegerField(db_column='gidNumber', unique=True)
name = CharField(db_column='cn', max_length=200, primary_key=True)
members = ListField(db_column='memberUid')
def __str__(self):
return self.name
def __unicode__(self):
return self.name
and add this to your admin.py:
from django.contrib import admin
from . import models
class LDAPGroupAdmin(admin.ModelAdmin):
exclude = ['dn', 'objectClass']
list_display = ['gid', 'name']
admin.site.register(models.LDAPGroup, LDAPGroupAdmin)
- Important note:
You must declare an attribute to be used as the primary key. This attribute will play a special role, as it will be used to build the Relative Distinguished Name of the entry.
For instance in the example above, a group whose cn is foo will have the DN cn=foo,ou=groups,dc=nodomain,dc=org.
Supported fields
djanglo-ldapdb provides the following fields, all imported from ldapdb.models.fields:
Similar to Django:
IntegerField
FloatField
BooleanField
CharField
ImageField
DateTimeField
- Specific to a LDAP server:
ListField (holds a list of text values)
TimestampField (Stores a datetime as a posix timestamp, typically for posixAccount)
- Legacy:
DateField (Stores a date in an arbitrary format. A LDAP server has no notion of Date).
Tuning django-ldapdb
It is possible to adjust django-ldapdb’s behavior by defining a few parameters in the DATABASE section:
- PAGE_SIZE (default: 1000)
Define the maximum size of a results page to be returned by the server
- QUERY_TIMEOUT (default: no limit)
Define the maximum time in seconds we’ll wait to get a reply from the server (on a per-query basis).
Developing with a LDAP server
When developing against a LDAP server, having access to a development LDAP server often proves useful.
django-ldapdb uses the volatildap project for this purpose:
A LDAP server is instantiated for each TestClass;
Its content is reset at the start of each test function;
It can be customized to embark any schemas required by the application;
Starting with volatildap 1.4.0, the volatildap server can be controlled remotely, avoiding the need to install a LDAP server on the host.
Applications using django-ldapdb may use the following code snippet when setting up their tests:
# This snippet is released in the Public Domain
from django.conf import settings
from django.test import TestCase
import volatildap
class LdapEnabledTestCase(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.ldap = volatildap.LdapServer(
# Load some initial data
initial={'ou=people': {
'ou': ['people'],
'objectClass': ['organizationalUnit'],
}},
# Enable more LDAP schemas
schemas=['core.schema', 'cosine.schema', 'inetorgperson.schema', 'nis.schema'],
)
# The volatildap server uses specific defaults, and listens on an arbitrary port.
# Copy the server-side values to Django settings
settings.DATABASES['ldap']['USER'] = cls.ldap.rootdn
settings.DATABASES['ldap']['PASSWORD'] = cls.ldap.rootpw
settings.DATABASES['ldap']['NAME'] = cls.ldap.uri
def setUp(self):
super().setUp()
# Starting an already-started volatildap server performs a data reset
self.ldap.start()
@classmethod
def tearDownClass(cls):
# Free up resources on teardown.
cls.ldap.stop()
super().tearDownClass()
Metadata
Release files for django-ldapdb 1.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-ldapdb-1.5.1.tar.gz | 31.8 kB | Without provenance | Details for django-ldapdb-1.5.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_ldapdb-1.5.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for django_ldapdb-1.5.1-py2.py3-none-any.whl |
Total release size: 54.4 kB
Release files / django-ldapdb-1.5.1.tar.gz
| Download URL | django-ldapdb-1.5.1.tar.gz |
|---|---|
| Size | 31.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5ea333c3130abbb86f8629766370a7f490878706469fce4e5008e41fb566392a
|
|
BLAKE2b-256 checksum How to use checksums |
bd9f8e2f4fa4295ab06533de57bd70a2ceeb5a84c405f8868efc991c82b119f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.8
|
Release files / django_ldapdb-1.5.1-py2.py3-none-any.whl
| Download URL | django_ldapdb-1.5.1-py2.py3-none-any.whl |
|---|---|
| Size | 22.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
2daee828a7eb1ce6ad0634ce5311339d77c08128829e575ff14d62a46771b86a
|
|
BLAKE2b-256 checksum How to use checksums |
a1f248c06dc793e8b15f87df76ebe3cc84738b8a7b1af6573b2e2fb325b84711
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.8
|
Security for django-ldapdb 1.5.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.