src/EventListener/UserResolveListener.php line 21

  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Admin\CustomerUser;
  4. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  5. use Symfony\Component\Security\Core\User\UserProviderInterface;
  6. use League\Bundle\OAuth2ServerBundle\Event\UserResolveEvent;
  7. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. final class UserResolveListener
  10. {   
  11.     public function __construct(
  12.            private EntityManagerInterface $adminEntityManager
  13.            private UserProviderInterface $userProvider
  14.            private UserPasswordHasherInterface $userPasswordEncoder)
  15.     {
  16.     }
  17.         
  18.     public function onUserResolve(UserResolveEvent $event): void
  19.     {   
  20. error_log(utf8_decode($event->getPassword()));    
  21.         $user $this->userProvider->loadUserByIdentifier($event->getUsername());
  22.      error_log($event->getPassword());
  23.  
  24.         if (null === $user) {
  25.             throw new UnauthorizedHttpException("username","Invalid credentials!");
  26.         }
  27. //nicht in Lost        
  28.         if ($user->getMapping()->getStatus() === "lost"){
  29.             throw new AccessDeniedHttpException("username","Invalid access right!");
  30.         }
  31.         
  32. //login nur mit aktiven Arbeitsvertrag
  33.         $work_contract $this->adminEntityManager->getRepository(CustomerUser::class)->activWorkContract($user);
  34.         if ($work_contract == null) {
  35.             throw new AccessDeniedHttpException("username","Invalid access right!");
  36.         }
  37.     error_log($event->getPassword());    
  38.         if (!$this->userPasswordEncoder->isPasswordValid($user$event->getPassword())) {
  39.             throw new UnauthorizedHttpException("password","Invalid credentials!");
  40.         }
  41.         $event->setUser($user);
  42.     }
  43. }