src/Shared/Infrastructure/Security/LdapNewUserProvider.php line 19

Open in your IDE?
  1. <?php
  2.     namespace App\Shared\Infrastructure\Security;
  3.     use App\Application\Handlers\Empleado\Query\SearchEmpleadoByUsernameQuery;
  4.     use App\Domain\Entity\Empleado;
  5.     use App\Shared\Domain\Bus\Query\QueryBus;
  6.     use Symfony\Component\Ldap\Entry;
  7.     use Symfony\Component\Ldap\LdapInterface;
  8.     use Symfony\Component\Ldap\Security\LdapUserProvider;
  9.     use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  10.     use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  11.     use Symfony\Component\Security\Core\User\UserInterface;
  12.     class LdapNewUserProvider extends LdapUserProvider
  13.     {
  14.         private $queryBus;
  15.         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 = [])
  16.         {
  17.             parent::__construct(
  18.                 $ldap,
  19.                 $baseDn,
  20.                 $searchDn,
  21.                 $searchPassword,
  22.                 $defaultRoles,
  23.                 $uidKey,
  24.                 $filter,
  25.                 $passwordAttribute,
  26.                 $extraFields
  27.             );
  28.             $this->queryBus = $queryBus;
  29.         }
  30.         protected function loadUser(string $identifier, Entry $entry)
  31.         {
  32.             $empleado = $this->queryBus->ask(new SearchEmpleadoByUsernameQuery($identifier))->getEmpleado();
  33.             if(!$empleado) throw new UserNotFoundException(sprintf('User "%s" not found in Application.', $identifier));
  34.             return $empleado;
  35.         }
  36.         public function supportsClass(string $class)
  37.         {
  38.             return Empleado::class === $class;
  39.         }
  40.         public function refreshUser(UserInterface $user)
  41.         {
  42.             if (!$this->supportsClass(get_class($user))) {
  43.                 throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
  44.             }
  45.             $empleado = $this->queryBus->ask(new SearchEmpleadoByUsernameQuery($user->getUsername()))->getEmpleado();
  46.             return $empleado;
  47.         }
  48.     }//class