src/Platform/SecurityBundle/Doctrine/Login/AttemptRepository.php line 39

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Doctrine\Login;
  3. use Cms\CoreBundle\Model\Search\SearchableTrait;
  4. use Cms\CoreBundle\Util\DateTimeUtils;
  5. use Cms\CoreBundle\Util\Doctrine\EntityRepository;
  6. use Cms\TenantBundle\Entity\Tenant;
  7. use DateTime;
  8. use Platform\SecurityBundle\Entity\Login\Attempt;
  9. /**
  10.  * Class AttemptRepository
  11.  *
  12.  * @package Platform\SecurityBundle\Doctrine\Login
  13.  *
  14.  * @method Attempt findExact($id);
  15.  */
  16. class AttemptRepository extends EntityRepository
  17. {
  18.     use SearchableTrait;
  19.     /**
  20.      * @param string $uid
  21.      * @return Attempt|null
  22.      */
  23.     public function findOneByUid(string $uid): ?Attempt
  24.     {
  25.         return $this->findOneBy([
  26.             'uid' => $uid,
  27.         ]);
  28.     }
  29.     /**
  30.      * @param Tenant $tenant
  31.      * @param string|null $ip
  32.      * @return int
  33.      */
  34.     public function countLastHourFailed(Tenant $tenant, ?string $ip null)
  35.     {
  36.         // create the builder
  37.         $qb $this->_em->createQueryBuilder()
  38.             ->select('COUNT(attempts)')
  39.             ->from($this->_entityName'attempts')
  40.             ->where('attempts.status = false')
  41.             ->andWhere('attempts.tenant = :tenant')
  42.             ->setParameter('tenant'$tenant)
  43.             ->andWhere('attempts.createdAt >= :date')
  44.             ->setParameter('date'DateTimeUtils::beforeCurrent('PT1H'));
  45.         if ( ! empty($ip)) {
  46.             $qb->andWhere('attempts.ip = :ip')
  47.                 ->setParameter('ip'$ip);
  48.         }
  49.         // run it
  50.         return intval($this->querySingleScalar($qb)[0]);
  51.     }
  52.     /**
  53.      * @param DateTime $startDate
  54.      * @param DateTime $endDate
  55.      * @return int
  56.      */
  57.     public function countLoginsByDateInterval(DateTime $startDateDateTime $endDate)
  58.     {
  59.         // need to get login attempts for all tenants
  60.         $tenantFilter $this->_em->getFilters()->getFilter('tenant_filter');
  61.         $this->_em->getFilters()->disable('tenant_filter');
  62.         // create the builder
  63.         $qb $this->createQueryBuilder('attempts')
  64.             ->select('COUNT(attempts)')
  65.             ->where('attempts.status = :status')
  66.             ->andWhere('attempts.createdAt >= :startDate')
  67.             ->andWhere('attempts.createdAt < :endDate')
  68.             ->setParameters(array(
  69.                 'status' => true,
  70.                 'startDate' => $startDate,
  71.                 'endDate' => $endDate,
  72.             ))
  73.         ;
  74.         $result $this->querySingleScalar($qb);
  75.         // I'm not sure that this is necessary, but theoretically this can affect further queries with tenant params
  76.         $this->_em->getFilters()
  77.             ->enable('tenant_filter')
  78.             ->setParameter('id'$tenantFilter->getParameter('id'))
  79.         ;
  80.         return intval($result[0]);
  81.     }
  82. }