vendor/nen/kennisbank-platform/src/Nen/Security/IpLoginAuthenticator.php line 19

Open in your IDE?
  1. <?php
  2. namespace Nen\Bundle\KennisbankPlatformBundle\Security;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Nen\Bundle\KennisbankPlatformBundle\Entity\Ip;
  5. use App\Entity\User;
  6. use Nen\Bundle\KennisbankPlatformBundle\Support\IpPacker;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  16. class IpLoginAuthenticator extends AbstractAuthenticator
  17. {
  18.     private Security $security;
  19.     private EntityManagerInterface $em;
  20.     public function __construct(Security $securityEntityManagerInterface $em)
  21.     {
  22.         $this->security $security;
  23.         $this->em $em;
  24.     }
  25.     public function loadUserByIp($ip): ?User
  26.     {
  27.         $packedIp IpPacker::pack($ip);
  28.         $qb $this->em->createQueryBuilder();
  29.         $result $qb
  30.             ->select('i''u''c')
  31.             ->from(Ip::class, 'i')
  32.             ->innerJoin('i.user''u')
  33.             ->innerJoin('u.company''c')
  34.             ->where(
  35.                 $qb->expr()->lte('i.from'$packedIp),
  36.                 $qb->expr()->gte('i.to'$packedIp)
  37.             )
  38.             ->setMaxResults(1)
  39.             ->getQuery()
  40.             ->execute();
  41.         if (empty($result)) {
  42.             return null;
  43.         }
  44.         /** @var Ip $ip */
  45.         $ip $result[0];
  46.         if (!$ip->getUser()->hasValidLicense()) {
  47.             return null;
  48.         }
  49.         return $ip->getUser();
  50.     }
  51.     public function supports(Request $request): bool
  52.     {
  53.         return $this->security->getUser() === null;
  54.     }
  55.     public function authenticate(Request $request): PassportInterface
  56.     {
  57.         $ip $request->getClientIp();
  58.         return new SelfValidatingPassport(
  59.             new UserBadge($ip, [$this'loadUserByIp'])
  60.         );
  61.     }
  62.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  63.     {
  64.         return null;
  65.     }
  66.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  67.     {
  68.         return null;
  69.     }
  70. }