src/EventListener/UserResolveListener.php line 21
<?php
namespace App\EventListener;
use App\Entity\Admin\CustomerUser;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use League\Bundle\OAuth2ServerBundle\Event\UserResolveEvent;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Doctrine\ORM\EntityManagerInterface;
final class UserResolveListener
{
public function __construct(
private EntityManagerInterface $adminEntityManager,
private UserProviderInterface $userProvider,
private UserPasswordHasherInterface $userPasswordEncoder)
{
}
public function onUserResolve(UserResolveEvent $event): void
{
error_log(utf8_decode($event->getPassword()));
$user = $this->userProvider->loadUserByIdentifier($event->getUsername());
error_log($event->getPassword());
if (null === $user) {
throw new UnauthorizedHttpException("username","Invalid credentials!");
}
//nicht in Lost
if ($user->getMapping()->getStatus() === "lost"){
throw new AccessDeniedHttpException("username","Invalid access right!");
}
//login nur mit aktiven Arbeitsvertrag
$work_contract = $this->adminEntityManager->getRepository(CustomerUser::class)->activWorkContract($user);
if ($work_contract == null) {
throw new AccessDeniedHttpException("username","Invalid access right!");
}
error_log($event->getPassword());
if (!$this->userPasswordEncoder->isPasswordValid($user, $event->getPassword())) {
throw new UnauthorizedHttpException("password","Invalid credentials!");
}
$event->setUser($user);
}
}