From f767fb792fd558cfabaa9438a3deff99185cc6a6 Mon Sep 17 00:00:00 2001 From: Charles Sarrazin Date: Mon, 19 Sep 2016 19:54:27 +0200 Subject: [PATCH 1/2] Updated LDAP documentation for Symfony 3.1 --- components/ldap.rst | 83 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/components/ldap.rst b/components/ldap.rst index b1f51043dd4..08caf908fb0 100644 --- a/components/ldap.rst +++ b/components/ldap.rst @@ -20,10 +20,10 @@ You can install the component in 2 different ways: Usage ----- -The :class:`Symfony\\Component\\Ldap\\LdapClient` class provides methods +The :class:`Symfony\\Component\\Ldap\\Ldap` class provides methods to authenticate and query against an LDAP server. -The :class:`Symfony\\Component\\Ldap\\LdapClient` class can be configured +The :class:`Symfony\\Component\\Ldap\\Ldap` class can be configured using the following options: ``host`` @@ -35,11 +35,11 @@ using the following options: ``version`` The version of the LDAP protocol to use -``useSsl`` - Whether or not to secure the connection using SSL +``encryption`` + Your encryption mechanism (``ssl``, ``tls`` or ``none``) -``useStartTls`` - Whether or not to secure the connection using StartTLS +``connection_string`` + You may use this option instead of ``optReferrals`` Specifies whether to automatically follow referrals @@ -47,26 +47,83 @@ using the following options: For example, to connect to a start-TLS secured LDAP server:: - use Symfony\Component\Ldap\LdapClient; + use Symfony\Component\Ldap\Ldap; - $ldap = new LdapClient('my-server', 389, 3, false, true); + $ldap = Ldap::create('ext_ldap', array( + 'host' => 'my-server', + 'encryption' => 'ssl', + )); -The :method:`Symfony\\Component\\Ldap\\LdapClient::bind` method +Or you could directly specify a connection string:: + + use Symfony\Component\Ldap\Ldap; + + $ldap = Ldap::create('ext_ldap', array('connection_string' => 'ldaps://my-server:636')); + +The :method:`Symfony\\Component\\Ldap\\Ldap::bind` method authenticates a previously configured connection using both the distinguished name (DN) and the password of a user:: - use Symfony\Component\Ldap\LdapClient; + use Symfony\Component\Ldap\Ldap; // ... $ldap->bind($dn, $password); Once bound (or if you enabled anonymous authentication on your LDAP server), you may query the LDAP server using the -:method:`Symfony\\Component\\Ldap\\LdapClient::find` method:: +:method:`Symfony\\Component\\Ldap\\Ldap::query` method:: + + use Symfony\Component\Ldap\Ldap; + // ... + + $query = $ldap->query('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))'); + $results = $query->execute(); + + foreach ($results as $entry) { + // Do something with the results + } + +By default, LDAP entries are lazy-loaded. If you wish to fetch +all entries in a single call and do something with the results' +array, you may use the +:method:`Symfony\\Component\\Ldap\\Adapter\\ExtLdap\\Collection::toArray` method:: + + use Symfony\Component\Ldap\Ldap; + // ... + + $query = $ldap->query('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))'); + $results = $query->execute()->toArray(); + + // Do something with the results array + +Creating or updating entries +---------------------------- + +Since version 3.1, The Ldap component provides means to create +new LDAP entries, update or even delete existing ones:: - use Symfony\Component\Ldap\LdapClient; + use Symfony\Component\Ldap\Ldap; + use Symfony\Component\Ldap\Entry; // ... - $ldap->find('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))'); + $entry = new Entry('cn=Fabien Potencier,dc=symfony,dc=com', array( + 'sn' => array('fabpot'), + 'objectClass' => array('inetOrgPerson'), + )); + + $em = $ldap->getEntryManager(); + + // Creating a new entry + $em->add($entry); + + // Finding and updating an existing entry + $query = $ldap->query('dc=symfony,dc=com', '(&(objectclass=person)(ou=Maintainers))'); + $result = $query->execute(); + $entry = $result[0]; + $entry->addAttribute('email', array('fabpot@symfony.com')); + $em->update($entry); + + // Removing an existing entry + $em->remove(new Entry('cn=Test User,dc=symfony,dc=com')); .. _Packagist: https://packagist.org/packages/symfony/ldap From 60bed419d84652ca3771a2990190e15253ff82b5 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 9 Jan 2018 10:49:44 +0100 Subject: [PATCH 2/2] Minor fixes --- components/ldap.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/ldap.rst b/components/ldap.rst index 08caf908fb0..54201ce18fa 100644 --- a/components/ldap.rst +++ b/components/ldap.rst @@ -39,7 +39,8 @@ using the following options: Your encryption mechanism (``ssl``, ``tls`` or ``none``) ``connection_string`` - You may use this option instead of +   You may use this option instead of ``host`` and ``port`` to connect to the + LDAP server ``optReferrals`` Specifies whether to automatically follow referrals @@ -96,11 +97,11 @@ array, you may use the // Do something with the results array -Creating or updating entries +Creating or Updating Entries ---------------------------- -Since version 3.1, The Ldap component provides means to create -new LDAP entries, update or even delete existing ones:: +The Ldap component provides means to create new LDAP entries, update or even +delete existing ones:: use Symfony\Component\Ldap\Ldap; use Symfony\Component\Ldap\Entry;