Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions msldap/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ async def get_object_by_dn(self, dn:str, expected_class = None):
elif 'group' in temp:
yield MSADGroup.from_ldap(entry), None

async def modify(self, dn:str, changes:Dict[str, object], controls:Dict[str, object] = None):
async def modify(self, dn:str, changes:Dict[str, object], controls:Dict[str, object] = None, encode=True):
"""
Performs the modify operation.

Expand All @@ -1164,6 +1164,9 @@ async def modify(self, dn:str, changes:Dict[str, object], controls:Dict[str, obj
:type changes: dict
:param controls: additional controls to be passed in the query
:type controls: dict
:param encode: encode the changes provided before sending them to the server
:type encode: bool

:return: A tuple of (True, None) on success or (False, Exception) on error.
:rtype: (:class:`bool`, :class:`Exception`)
"""
Expand All @@ -1172,7 +1175,7 @@ async def modify(self, dn:str, changes:Dict[str, object], controls:Dict[str, obj
controls_conv = []
for control in controls:
controls_conv.append(Control(control))
return await self._con.modify(dn, changes, controls=controls_conv)
return await self._con.modify(dn, changes, controls=controls_conv, encode=encode)


async def add(self, dn:str, attributes:Dict[str, object]):
Expand Down
7 changes: 5 additions & 2 deletions msldap/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ async def add(self, entry:str, attributes:Dict[str, object]):
except Exception as e:
return False, e

async def modify(self, entry:str, changes:Dict[str, object], controls:List[Control] = None):
async def modify(self, entry:str, changes:Dict[str, object], controls:List[Control] = None, encode=True,):
"""
Performs the modify operation.

Expand All @@ -601,13 +601,16 @@ async def modify(self, entry:str, changes:Dict[str, object], controls:List[Contr
:type changes: dict
:param controls: additional controls to be passed in the query
:type controls: List[class:`Control`]
:param encode: encode the changes provided before sending them to the server
:type encode: bool

:return: A tuple of (True, None) on success or (False, Exception) on error.
:rtype: (:class:`bool`, :class:`Exception`)
"""
try:
req = {
'object' : entry.encode(),
'changes' : encode_changes(changes)
'changes' : encode_changes(changes, encode)
}
br = { 'modifyRequest' : ModifyRequest(req)}
msg = { 'protocolOp' : protocolOp(br)}
Expand Down
19 changes: 17 additions & 2 deletions msldap/protocol/typeconversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ def encode_attributes(x):
lookup_table = MSLDAP_BUILTIN_ATTRIBUTE_TYPES_ENC
elif k in MSLDAP_BUILTIN_ATTRIBUTE_TYPES:
lookup_table = MSLDAP_BUILTIN_ATTRIBUTE_TYPES
elif k in LDAP_WELL_KNOWN_ATTRS:
lookup_table = LDAP_WELL_KNOWN_ATTRS
else:
raise Exception('Unknown conversion type for key "%s"' % k)

Expand Down Expand Up @@ -456,7 +458,7 @@ def convert_result(x):
}


def encode_changes(x):
def encode_changes(x, encode=True):
logger.debug('Encode changes: %s' % x)
res = []
for k in x:
Expand All @@ -465,15 +467,28 @@ def encode_changes(x):
lookup_table = MSLDAP_BUILTIN_ATTRIBUTE_TYPES_ENC
elif k in MSLDAP_BUILTIN_ATTRIBUTE_TYPES:
lookup_table = MSLDAP_BUILTIN_ATTRIBUTE_TYPES
elif k in LDAP_WELL_KNOWN_ATTRS:
lookup_table = LDAP_WELL_KNOWN_ATTRS
else:
raise Exception('Unknown conversion type for key "%s"' % k)

for mod, value in x[k]:
encoder = lookup_table[k]
splitted_name = encoder.__name__.split("_")
if isinstance(value, list) and "single" == splitted_name[0]:
if len(value) > 1:
raise TypeError(f"{k} takes only one value but multiple values have been given.")
value = value[0]
if not encode and splitted_name[1] != ["bytes"]:
if splitted_name[0] == "single":
encoder = single_bytes
else:
encoder = multi_bytes
res.append(Change({
'operation' : mod,
'modification' : PartialAttribute({
'type' : k.encode(),
'attributes' : lookup_table[k](value, True)
'attributes' : encoder(value, True)
})
}))
#print(lookup_table[k](value, True))
Expand Down