models.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. Code used to access Debian's LDAP
  3. """
  4. from django.db import models
  5. from django.conf import settings
  6. try:
  7. import ldap
  8. except ImportError:
  9. import ldap3 as ldap
  10. LDAP_SERVER = getattr(settings, "LDAP_SERVER", "ldap://db.debian.org")
  11. class Entry(object):
  12. def __init__(self):
  13. self.dn = None
  14. self.attrs = None
  15. self.uid = None
  16. def init(self, dn, attrs):
  17. """
  18. Init entry to point at these attributes
  19. """
  20. self.dn = dn
  21. self.attrs = attrs
  22. self.uid = attrs["uid"][0]
  23. def single(self, name):
  24. """
  25. Return a single value for a LDAP attribute
  26. """
  27. if name not in self.attrs:
  28. return None
  29. val = self.attrs[name]
  30. if not val:
  31. return None
  32. return val[0]
  33. @property
  34. def is_dd(self):
  35. return "Debian" in self.attrs["supplementaryGid"]
  36. @property
  37. def is_guest(self):
  38. return "guest" in self.attrs["supplementaryGid"]
  39. def list_people():
  40. search_base = "dc=debian,dc=org"
  41. l = ldap.initialize(LDAP_SERVER)
  42. l.simple_bind_s("","")
  43. # Create the object only once
  44. entry = Entry()
  45. for dn, attrs in l.search_s(search_base, ldap.SCOPE_SUBTREE, "objectclass=inetOrgPerson"):
  46. entry.init(dn, attrs)
  47. yield entry