Move DN handling methods to LDAPConnection

ldap2 has "DN normalization" functionality, which silently adds the base
DN to DNs that don't already end with it.
This functionality is left in the ldap2 class only.

Part of the work for: https://fedorahosted.org/freeipa/ticket/2660
This commit is contained in:
Petr Viktorin
2013-01-18 04:51:41 -05:00
committed by Martin Kosek
parent 44e15206d0
commit 6fb115751c
2 changed files with 40 additions and 36 deletions

View File

@@ -902,6 +902,44 @@ class LDAPConnection(object):
obj = self.schema.get_obj(_ldap.schema.AttributeType, attr) obj = self.schema.get_obj(_ldap.schema.AttributeType, attr)
return obj and obj.single_value return obj and obj.single_value
def normalize_dn(self, dn):
"""Override to normalize all DNs passed to LDAPConnection methods"""
assert isinstance(dn, DN)
return dn
def make_dn_from_attr(self, attr, value, parent_dn=None):
"""
Make distinguished name from attribute.
Keyword arguments:
parent_dn -- DN of the parent entry (default '')
"""
if parent_dn is None:
parent_dn = DN()
parent_dn = self.normalize_dn(parent_dn)
if isinstance(value, (list, tuple)):
value = value[0]
return DN((attr, value), parent_dn)
def make_dn(self, entry_attrs, primary_key='cn', parent_dn=None):
"""
Make distinguished name from entry attributes.
Keyword arguments:
primary_key -- attribute from which to make RDN (default 'cn')
parent_dn -- DN of the parent entry (default '')
"""
assert primary_key in entry_attrs
if parent_dn is None:
parent_dn = DN()
parent_dn = self.normalize_dn(parent_dn)
return DN((primary_key, entry_attrs[primary_key]), parent_dn)
class IPAdmin(LDAPConnection): class IPAdmin(LDAPConnection):

View File

@@ -204,8 +204,8 @@ class ldap2(LDAPConnection, CrudBackend):
Normalize distinguished name by assuring it ends with Normalize distinguished name by assuring it ends with
the base_dn. the base_dn.
Note: You don't have to normalize DN's before passing them to Note: ldap2 methods normalize DNs internally, but relying on this is
ldap2 methods. It's done internally for you. not recommended.
""" """
assert isinstance(dn, DN) assert isinstance(dn, DN)
@@ -218,40 +218,6 @@ class ldap2(LDAPConnection, CrudBackend):
return dn return dn
def make_dn_from_attr(self, attr, value, parent_dn=None):
"""
Make distinguished name from attribute.
Keyword arguments:
parent_dn -- DN of the parent entry (default '')
"""
if parent_dn is None:
parent_dn = DN()
assert isinstance(parent_dn, DN)
parent_dn = self.normalize_dn(parent_dn)
if isinstance(value, (list, tuple)):
value = value[0]
return DN((attr, value), parent_dn)
def make_dn(self, entry_attrs, primary_key='cn', parent_dn=None):
"""
Make distinguished name from entry attributes.
Keyword arguments:
primary_key -- attribute from which to make RDN (default 'cn')
parent_dn -- DN of the parent entry (default '')
"""
assert primary_key in entry_attrs
if parent_dn is None:
parent_dn = DN()
parent_dn = self.normalize_dn(parent_dn)
return DN((primary_key, entry_attrs[primary_key]), parent_dn)
def add_entry(self, dn, entry_attrs, normalize=True): def add_entry(self, dn, entry_attrs, normalize=True):
"""Create a new entry.""" """Create a new entry."""