Created
February 7, 2019 14:17
-
-
Save Nek-/f00872db7a5d061de33ed7c48390d788 to your computer and use it in GitHub Desktop.
LDAP Fixtures with Symfony and DoctrineFixtures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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