108 lines
3.5 KiB
PHP
108 lines
3.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\DataFixtures;
|
||
|
|
||
|
use App\Service\LdapService;
|
||
|
use App\Entity\User;
|
||
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||
|
use Doctrine\Persistence\ObjectManager;
|
||
|
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
|
||
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||
|
use Symfony\Component\Console\Input\ArrayInput;
|
||
|
use Symfony\Component\Console\Output\BufferedOutput;
|
||
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
||
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||
|
|
||
|
class AppFixtures extends Fixture
|
||
|
{
|
||
|
private $kernel;
|
||
|
private $output;
|
||
|
private $minio;
|
||
|
private $colorbg;
|
||
|
private $colorft;
|
||
|
private $ldap;
|
||
|
private $manager;
|
||
|
|
||
|
public function __construct(KernelInterface $kernel, LdapService $ldapservice)
|
||
|
{
|
||
|
$this->kernel = $kernel;
|
||
|
$this->ldap = $ldapservice;
|
||
|
$this->output = new ConsoleOutput();
|
||
|
}
|
||
|
|
||
|
public function load(ObjectManager $manager): void
|
||
|
{
|
||
|
$this->manager = $manager;
|
||
|
|
||
|
// Reset autoincrement
|
||
|
|
||
|
// app:Init
|
||
|
$this->writeln('app:Init');
|
||
|
$application = new Application($this->kernel);
|
||
|
$application->setAutoExit(false);
|
||
|
$input = new ArrayInput(['command' => 'app:Init']);
|
||
|
$boutput = new BufferedOutput();
|
||
|
$application->run($input, $boutput);
|
||
|
$manager->flush();
|
||
|
|
||
|
$this->writeln('LDAP');
|
||
|
$baseorganisation = $this->ldap->getParameter('basedn');
|
||
|
|
||
|
// Purge de la strucutre annuaire
|
||
|
$this->ldap->deleteByDN('ou=crous01,'.$baseorganisation, true);
|
||
|
$this->ldap->deleteByDN('ou=crous02,'.$baseorganisation, true);
|
||
|
|
||
|
// Création de la structure
|
||
|
$this->ldap->addOrganisation('ou=crous01,'.$baseorganisation);
|
||
|
$this->ldap->addOrganisation('ou=users,ou=crous01,'.$baseorganisation);
|
||
|
$this->ldap->addOrganisation('ou=crous02,'.$baseorganisation);
|
||
|
$this->ldap->addOrganisation('ou=users,ou=crous02,'.$baseorganisation);
|
||
|
|
||
|
// Création user
|
||
|
$this->submitUser('admin','NUO SSO','Administrateur','uid=admin,ou=users,ou=crous01,'.$baseorganisation);
|
||
|
$this->submitUser('user001','001','User','uid=user001,ou=users,ou=crous01,'.$baseorganisation);
|
||
|
$this->submitUser('user002','002','User','uid=user002,ou=users,ou=crous02,'.$baseorganisation);
|
||
|
}
|
||
|
|
||
|
|
||
|
private function submituser($username,$firstname,$lastname,$dn)
|
||
|
{
|
||
|
$user = new User();
|
||
|
$user->setUsername($username);
|
||
|
$user->setPassword($username);
|
||
|
$user->setRoles(['ROLE_USER']);
|
||
|
$user->setFirstname($firstname);
|
||
|
$user->setLastname($lastname);
|
||
|
$user->setEmail($username.'@noreply.fr');
|
||
|
|
||
|
$this->ldap->fixtureUser($user,$dn);
|
||
|
|
||
|
}
|
||
|
|
||
|
private function writeln($string)
|
||
|
{
|
||
|
$this->output->writeln(' <fg=yellow>></> <info>'.$string.'</info>');
|
||
|
}
|
||
|
|
||
|
private function csv_to_array($csv, $delimiter = ';', $enclosure = '', $escape = '\\', $terminator = "\n")
|
||
|
{
|
||
|
$r = [];
|
||
|
$rows = explode($terminator, trim($csv));
|
||
|
|
||
|
$names = array_shift($rows);
|
||
|
$names = str_getcsv($names, $delimiter, $enclosure, $escape);
|
||
|
$nc = count($names);
|
||
|
foreach ($rows as $row) {
|
||
|
if (trim($row)) {
|
||
|
$values = str_getcsv($row, $delimiter, $enclosure, $escape);
|
||
|
if (!$values) {
|
||
|
$values = array_fill(0, $nc, null);
|
||
|
}
|
||
|
@$r[] = array_combine($names, $values);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $r;
|
||
|
}
|
||
|
}
|