mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Introduce IPASimpleLDAPObject.decode method for decoding LDAP values.
This method is intended as a counterpart of IPASimpleLDAPObject.encode and replaces IPASimpleLDAPObject.convert_value_list. https://fedorahosted.org/freeipa/ticket/3521
This commit is contained in:
parent
4f0814d7c0
commit
5aadaa6030
@ -353,8 +353,9 @@ class IPASimpleLDAPObject(object):
|
|||||||
|
|
||||||
|
|
||||||
def encode(self, val):
|
def encode(self, val):
|
||||||
'''
|
"""
|
||||||
'''
|
Encode attribute value to LDAP representation (str).
|
||||||
|
"""
|
||||||
# Booleans are both an instance of bool and int, therefore
|
# Booleans are both an instance of bool and int, therefore
|
||||||
# test for bool before int otherwise the int clause will be
|
# test for bool before int otherwise the int clause will be
|
||||||
# entered for a boolean value instead of the boolean clause.
|
# entered for a boolean value instead of the boolean clause.
|
||||||
@ -379,29 +380,33 @@ class IPASimpleLDAPObject(object):
|
|||||||
else:
|
else:
|
||||||
raise TypeError("attempt to pass unsupported type to ldap, value=%s type=%s" %(val, type(val)))
|
raise TypeError("attempt to pass unsupported type to ldap, value=%s type=%s" %(val, type(val)))
|
||||||
|
|
||||||
def convert_value_list(self, attr, target_type, values):
|
def decode(self, val, attr):
|
||||||
'''
|
"""
|
||||||
'''
|
Decode attribute value from LDAP representation (str).
|
||||||
|
"""
|
||||||
if not self._decode_attrs:
|
if isinstance(val, str):
|
||||||
return values
|
if not self._decode_attrs:
|
||||||
|
return val
|
||||||
ipa_values = []
|
target_type = self._SYNTAX_MAPPING.get(self.get_syntax(attr), unicode_from_utf8)
|
||||||
|
if target_type is str:
|
||||||
for original_value in values:
|
return val
|
||||||
if isinstance(target_type, type) and isinstance(original_value, target_type):
|
try:
|
||||||
ipa_value = original_value
|
return target_type(val)
|
||||||
else:
|
except Exception, e:
|
||||||
try:
|
msg = 'unable to convert the attribute "%s" value "%s" to type %s' % (attr, val, target_type)
|
||||||
ipa_value = target_type(original_value)
|
self.log.error(msg)
|
||||||
except Exception, e:
|
raise ValueError(msg)
|
||||||
msg = 'unable to convert the attribute "%s" value "%s" to type %s' % (attr, original_value, target_type)
|
elif isinstance(val, list):
|
||||||
self.log.error(msg)
|
return [self.decode(m, attr) for m in val]
|
||||||
raise ValueError(msg)
|
elif isinstance(val, tuple):
|
||||||
|
return tuple(self.decode(m, attr) for m in val)
|
||||||
ipa_values.append(ipa_value)
|
elif isinstance(val, dict):
|
||||||
|
dct = dict((unicode_from_utf8(k), self.decode(v, k)) for k, v in val.iteritems())
|
||||||
return ipa_values
|
return dct
|
||||||
|
elif val is None:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
raise TypeError("attempt to pass unsupported type from ldap, value=%s type=%s" %(val, type(val)))
|
||||||
|
|
||||||
def convert_result(self, result):
|
def convert_result(self, result):
|
||||||
'''
|
'''
|
||||||
@ -438,8 +443,7 @@ class IPASimpleLDAPObject(object):
|
|||||||
ipa_entry = LDAPEntry(self, DN(original_dn))
|
ipa_entry = LDAPEntry(self, DN(original_dn))
|
||||||
|
|
||||||
for attr, original_values in original_attrs.items():
|
for attr, original_values in original_attrs.items():
|
||||||
target_type = self._SYNTAX_MAPPING.get(self.get_syntax(attr), unicode_from_utf8)
|
ipa_entry[attr] = self.decode(original_values, attr)
|
||||||
ipa_entry[attr] = self.convert_value_list(attr, target_type, original_values)
|
|
||||||
|
|
||||||
ipa_result.append(ipa_entry)
|
ipa_result.append(ipa_entry)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user