Skip to content

[Ldap] UpdateOperation toArray() function throws exception when using LDAP_MODIFY_BATCH_REMOVE_ALL operation type #54829

Closed
@phasdev

Description

@phasdev

Symfony version(s) affected

7.0.7

Description

Performing a batch operation using the LDAP_MODIFY_BATCH_REMOVE_ALL operation type produces an exception: ldap_modify_batch(): If option modtype" is LDAP_MODIFY_BATCH_REMOVE_ALL, option "values" cannot be provided

How to reproduce

Apply a batch update operation with an LDAP_MODIFY_BATCH_REMOVE_ALL operation type.
e.g. using the example code from https://symfony.com/doc/current/components/ldap.html ...

use Symfony\Component\Ldap\Adapter\ExtLdap\UpdateOperation;
use Symfony\Component\Ldap\Ldap;

$ldap = Ldap::create('ext_ldap', [
    'host' => 'my-server',
    'encryption' => 'ssl',
]);

$entryManager = $ldap->getEntryManager();
$entryManager->applyOperations('cn=Fabien Potencier,dc=symfony,dc=com', [
    new UpdateOperation(LDAP_MODIFY_BATCH_REMOVE_ALL , 'mail', NULL),
]);

Possible Solution

For LDAP_MODIFY_BATCH_REMOVE_ALL operations, omit the values element from the toArray() return value, i.e. update toArray function:

public function toArray(): array
{
  $op = [
      'attrib' => $this->attribute,
      'modtype' => $this->operationType,
  ];
  if (\LDAP_MODIFY_BATCH_REMOVE_ALL !== $this->operationType) {
      $op['values'] = $this->values;
  }
  return $op;
}

or (more concise/fewer changes):

public function toArray(): array
{
  return [
    'attrib' => $this->attribute,
    'modtype' => $this->operationType,
    ...(\LDAP_MODIFY_BATCH_REMOVE_ALL !== $this->operationType ? ['values' => $this->values] : [])
  ];
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions