Skip to main content

An LDAP database backend for Django

Project description

https://secure.travis-ci.org/django-ldapdb/django-ldapdb.png?branch=master Latest Version Supported Python versions Wheel status License

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 Django versions 1.11 / 2.0 / 2.1, and Python 2.7 / 3.4 / 3.5 / 3.6 / 3.7, as far as the Django and Python versions are compatible.

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-hypernode.com/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).

and then you can also 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

  • 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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ldapdb-1.3.0.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

django_ldapdb-1.3.0-py2.py3-none-any.whl (22.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-ldapdb-1.3.0.tar.gz.

File metadata

  • Download URL: django-ldapdb-1.3.0.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6

File hashes

Hashes for django-ldapdb-1.3.0.tar.gz
Algorithm Hash digest
SHA256 8bc4e49b2aa467c0cede33ea6c674c0ef162c46e292396afac28b263d4f5177a
MD5 64fe9f06fa8f9c058bbd5100779c8e3d
BLAKE2b-256 6481ccccbf7fdfb135467a425b61ae53d1f5b7142d874531132a338575b15d07

See more details on using hashes here.

File details

Details for the file django_ldapdb-1.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_ldapdb-1.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6

File hashes

Hashes for django_ldapdb-1.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e94daabf431b3d156f3c94f68d42e719d396cba8caf54b1f00e2f5b43daaeafd
MD5 cbf58d1be1c28644ca97b3b6bcc0f597
BLAKE2b-256 e0328df574a3286c88e0ab3731e602ac06e2f0a25cf49cc5287fc30b3eabc318

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page