<?php
namespace App\Shared\Infrastructure\Security;
use App\Application\Handlers\Empleado\Query\SearchEmpleadoByUsernameQuery;
use App\Domain\Entity\Empleado;
use App\Shared\Domain\Bus\Query\QueryBus;
use Symfony\Component\Ldap\Entry;
use Symfony\Component\Ldap\LdapInterface;
use Symfony\Component\Ldap\Security\LdapUserProvider;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
class LdapNewUserProvider extends LdapUserProvider
{
private $queryBus;
public function __construct(LdapInterface $ldap, string $baseDn, string $searchDn = null, string $searchPassword = '', array $defaultRoles = [], string $uidKey = null, string $filter = null, QueryBus $queryBus, string $passwordAttribute = null, array $extraFields = [])
{
parent::__construct(
$ldap,
$baseDn,
$searchDn,
$searchPassword,
$defaultRoles,
$uidKey,
$filter,
$passwordAttribute,
$extraFields
);
$this->queryBus = $queryBus;
}
protected function loadUser(string $identifier, Entry $entry)
{
$empleado = $this->queryBus->ask(new SearchEmpleadoByUsernameQuery($identifier))->getEmpleado();
if(!$empleado) throw new UserNotFoundException(sprintf('User "%s" not found in Application.', $identifier));
return $empleado;
}
public function supportsClass(string $class)
{
return Empleado::class === $class;
}
public function refreshUser(UserInterface $user)
{
if (!$this->supportsClass(get_class($user))) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
}
$empleado = $this->queryBus->ask(new SearchEmpleadoByUsernameQuery($user->getUsername()))->getEmpleado();
return $empleado;
}
}//class