[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/ldap/Adapter/ExtLdap/ -> EntryManager.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Ldap\Adapter\ExtLdap;
  13  
  14  use Symfony\Component\Ldap\Adapter\EntryManagerInterface;
  15  use Symfony\Component\Ldap\Entry;
  16  use Symfony\Component\Ldap\Exception\LdapException;
  17  use Symfony\Component\Ldap\Exception\NotBoundException;
  18  use Symfony\Component\Ldap\Exception\UpdateOperationException;
  19  
  20  /**
  21   * @author Charles Sarrazin <[email protected]>
  22   * @author Bob van de Vijver <[email protected]>
  23   */
  24  class EntryManager implements EntryManagerInterface
  25  {
  26      private $connection;
  27  
  28      public function __construct(Connection $connection)
  29      {
  30          $this->connection = $connection;
  31      }
  32  
  33      /**
  34       * {@inheritdoc}
  35       */
  36      public function add(Entry $entry)
  37      {
  38          $con = $this->getConnectionResource();
  39  
  40          if (!@ldap_add($con, $entry->getDn(), $entry->getAttributes())) {
  41              throw new LdapException(sprintf('Could not add entry "%s": ', $entry->getDn()).ldap_error($con), ldap_errno($con));
  42          }
  43  
  44          return $this;
  45      }
  46  
  47      /**
  48       * {@inheritdoc}
  49       */
  50      public function update(Entry $entry)
  51      {
  52          $con = $this->getConnectionResource();
  53  
  54          if (!@ldap_modify($con, $entry->getDn(), $entry->getAttributes())) {
  55              throw new LdapException(sprintf('Could not update entry "%s": ', $entry->getDn()).ldap_error($con), ldap_errno($con));
  56          }
  57      }
  58  
  59      /**
  60       * {@inheritdoc}
  61       */
  62      public function remove(Entry $entry)
  63      {
  64          $con = $this->getConnectionResource();
  65  
  66          if (!@ldap_delete($con, $entry->getDn())) {
  67              throw new LdapException(sprintf('Could not remove entry "%s": ', $entry->getDn()).ldap_error($con), ldap_errno($con));
  68          }
  69      }
  70  
  71      /**
  72       * Adds values to an entry's multi-valued attribute from the LDAP server.
  73       *
  74       * @throws NotBoundException
  75       * @throws LdapException
  76       */
  77      public function addAttributeValues(Entry $entry, string $attribute, array $values)
  78      {
  79          $con = $this->getConnectionResource();
  80  
  81          if (!@ldap_mod_add($con, $entry->getDn(), [$attribute => $values])) {
  82              throw new LdapException(sprintf('Could not add values to entry "%s", attribute "%s": ', $entry->getDn(), $attribute).ldap_error($con), ldap_errno($con));
  83          }
  84      }
  85  
  86      /**
  87       * Removes values from an entry's multi-valued attribute from the LDAP server.
  88       *
  89       * @throws NotBoundException
  90       * @throws LdapException
  91       */
  92      public function removeAttributeValues(Entry $entry, string $attribute, array $values)
  93      {
  94          $con = $this->getConnectionResource();
  95  
  96          if (!@ldap_mod_del($con, $entry->getDn(), [$attribute => $values])) {
  97              throw new LdapException(sprintf('Could not remove values from entry "%s", attribute "%s": ', $entry->getDn(), $attribute).ldap_error($con), ldap_errno($con));
  98          }
  99      }
 100  
 101      /**
 102       * {@inheritdoc}
 103       */
 104      public function rename(Entry $entry, string $newRdn, bool $removeOldRdn = true)
 105      {
 106          $con = $this->getConnectionResource();
 107  
 108          if (!@ldap_rename($con, $entry->getDn(), $newRdn, '', $removeOldRdn)) {
 109              throw new LdapException(sprintf('Could not rename entry "%s" to "%s": ', $entry->getDn(), $newRdn).ldap_error($con), ldap_errno($con));
 110          }
 111      }
 112  
 113      /**
 114       * Moves an entry on the Ldap server.
 115       *
 116       * @throws NotBoundException if the connection has not been previously bound
 117       * @throws LdapException     if an error is thrown during the rename operation
 118       */
 119      public function move(Entry $entry, string $newParent)
 120      {
 121          $con = $this->getConnectionResource();
 122          $rdn = $this->parseRdnFromEntry($entry);
 123          // deleteOldRdn does not matter here, since the Rdn will not be changing in the move.
 124          if (!@ldap_rename($con, $entry->getDn(), $rdn, $newParent, true)) {
 125              throw new LdapException(sprintf('Could not move entry "%s" to "%s": ', $entry->getDn(), $newParent).ldap_error($con), ldap_errno($con));
 126          }
 127      }
 128  
 129      /**
 130       * Get the connection resource, but first check if the connection is bound.
 131       */
 132      private function getConnectionResource()
 133      {
 134          // If the connection is not bound, throw an exception. Users should use an explicit bind call first.
 135          if (!$this->connection->isBound()) {
 136              throw new NotBoundException('Query execution is not possible without binding the connection first.');
 137          }
 138  
 139          return $this->connection->getResource();
 140      }
 141  
 142      /**
 143       * @param iterable<int, UpdateOperation> $operations An array or iterable of UpdateOperation instances
 144       *
 145       * @throws UpdateOperationException in case of an error
 146       */
 147      public function applyOperations(string $dn, iterable $operations): void
 148      {
 149          $operationsMapped = [];
 150          foreach ($operations as $modification) {
 151              $operationsMapped[] = $modification->toArray();
 152          }
 153  
 154          $con = $this->getConnectionResource();
 155          if (!@ldap_modify_batch($con, $dn, $operationsMapped)) {
 156              throw new UpdateOperationException(sprintf('Error executing UpdateOperation on "%s": ', $dn).ldap_error($con), ldap_errno($con));
 157          }
 158      }
 159  
 160      private function parseRdnFromEntry(Entry $entry): string
 161      {
 162          if (!preg_match('/(^[^,\\\\]*(?:\\\\.[^,\\\\]*)*),/', $entry->getDn(), $matches)) {
 163              throw new LdapException(sprintf('Entry "%s" malformed, could not parse RDN.', $entry->getDn()));
 164          }
 165  
 166          return $matches[1];
 167      }
 168  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer