Skip to content

[Ldap] Updated Ldap component documentation for 3.1 #6982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Updated LDAP documentation for Symfony 3.1
  • Loading branch information
csarrazi committed Sep 19, 2016
commit f767fb792fd558cfabaa9438a3deff99185cc6a6
83 changes: 70 additions & 13 deletions components/ldap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand All @@ -35,38 +35,95 @@ 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``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but this option name looks weird. Is there a better alternative to connection_string ? For example, like dsn in the database world.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the term "DSN" is specific to the database world. "connection_string" is standard for all software, and not only databases. :)

You may use this option instead of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This phrase looks unfinished: You may use this option instead of

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! Good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change that soon (this week or the next, depending on whether I can find time to change this), and also add some information which came from my previous github comments (like @mipapo mentioned).

I'll


``optReferrals``
Specifies whether to automatically follow referrals
returned by the LDAP server

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