Skip to content

Instantly share code, notes, and snippets.

@Nek-
Created February 7, 2019 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nek-/f00872db7a5d061de33ed7c48390d788 to your computer and use it in GitHub Desktop.
Save Nek-/f00872db7a5d061de33ed7c48390d788 to your computer and use it in GitHub Desktop.
LDAP Fixtures with Symfony and DoctrineFixtures
<?php
namespace App\DataFixtures;
use App\Application\Security\LdapEncoder;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\Exception\LdapException;
use Symfony\Component\Ldap\Ldap;
class UserFixtures extends Fixture
{
private $encoder;
public function __construct(LdapEncoder $encoder)
{
$this->encoder = $encoder;
}
public function load(ObjectManager $manager)
{
$ldap = Ldap::create('ext_ldap', ['connection_string' => 'ldap://ldap:389']);
$ldap->bind('cn=admin,dc=mycorp,dc=com', 's3cr3tpassw0rd');
$entryManager = $ldap->getEntryManager();
try {
$entryManager->remove(new Entry('uid=maxime,ou=People,dc=mycorp,dc=com'));
$entryManager->remove(new Entry('ou=People,dc=mycorp,dc=com'));
} catch (LdapException $e) {}
$ou = new Entry('ou=People,dc=mycorp,dc=com', [
'objectClass' => ['organizationalUnit'],
'ou' => ['People']
]);
$plainPassword = 'password';
$entry = new Entry('uid=maxime,ou=People,dc=mycorp,dc=com', [
'objectClass' => [
'inetOrgPerson',
],
'sn' => ['maxime'],
'uid' => ['maxime'],
'cn' => ['Maxime Veber'],
'mail' => ['maxime.veber@biig.fr'],
'userPassword' => [$this->encoder->encodePassword($plainPassword, null)]
]);
$entryManager->add($ou);
$entryManager->add($entry);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment