mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
feat: add domain reverse search on registrant name
This commit is contained in:
@@ -5,11 +5,14 @@ namespace App\Entity;
|
|||||||
use ApiPlatform\Metadata\ApiProperty;
|
use ApiPlatform\Metadata\ApiProperty;
|
||||||
use ApiPlatform\Metadata\ApiResource;
|
use ApiPlatform\Metadata\ApiResource;
|
||||||
use ApiPlatform\Metadata\Get;
|
use ApiPlatform\Metadata\Get;
|
||||||
|
use ApiPlatform\Metadata\GetCollection;
|
||||||
|
use ApiPlatform\Metadata\QueryParameter;
|
||||||
use App\Config\EventAction;
|
use App\Config\EventAction;
|
||||||
use App\Exception\MalformedDomainException;
|
use App\Exception\MalformedDomainException;
|
||||||
use App\Repository\DomainRepository;
|
use App\Repository\DomainRepository;
|
||||||
use App\Service\RDAPService;
|
use App\Service\RDAPService;
|
||||||
use App\State\AutoRegisterDomainProvider;
|
use App\State\AutoRegisterDomainProvider;
|
||||||
|
use App\State\FindDomainListFromEntityProvider;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
@@ -29,6 +32,22 @@ use Symfony\Component\Serializer\Attribute\SerializedName;
|
|||||||
]
|
]
|
||||||
),
|
),
|
||||||
*/
|
*/
|
||||||
|
new GetCollection(
|
||||||
|
uriTemplate: '/domains',
|
||||||
|
normalizationContext: [
|
||||||
|
'groups' => [
|
||||||
|
'domain:list',
|
||||||
|
'tld:list',
|
||||||
|
'event:list',
|
||||||
|
'domain:list',
|
||||||
|
'event:list',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
provider: FindDomainListFromEntityProvider::class,
|
||||||
|
parameters: [
|
||||||
|
'registrant' => new QueryParameter(description: 'The exact name of the registrant (case insensitive)', required: true),
|
||||||
|
]
|
||||||
|
),
|
||||||
new Get(
|
new Get(
|
||||||
uriTemplate: '/domains/{ldhName}', // Do not delete this line, otherwise Symfony interprets the TLD of the domain name as a return type
|
uriTemplate: '/domains/{ldhName}', // Do not delete this line, otherwise Symfony interprets the TLD of the domain name as a return type
|
||||||
normalizationContext: [
|
normalizationContext: [
|
||||||
|
|||||||
90
src/State/FindDomainListFromEntityProvider.php
Normal file
90
src/State/FindDomainListFromEntityProvider.php
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\Entity\Domain;
|
||||||
|
use App\Service\RDAPService;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Doctrine\ORM\Query\ResultSetMapping;
|
||||||
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
|
||||||
|
readonly class FindDomainListFromEntityProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private RequestStack $requestStack,
|
||||||
|
private EntityManagerInterface $em,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||||
|
{
|
||||||
|
$request = $this->requestStack->getCurrentRequest();
|
||||||
|
$rsm = (new ResultSetMapping())
|
||||||
|
->addScalarResult('handles', 'handles')
|
||||||
|
->addScalarResult('domain_ids', 'domain_ids')
|
||||||
|
->addScalarResult('registrant', 'registrant');
|
||||||
|
|
||||||
|
$handleBlacklist = join(',', array_map(fn (string $s) => "'$s'", RDAPService::ENTITY_HANDLE_BLACKLIST));
|
||||||
|
|
||||||
|
$sql = <<<SQL
|
||||||
|
SELECT
|
||||||
|
array_agg(DISTINCT sub.handle || '.' || sub.tld_id) AS handles,
|
||||||
|
array_agg(DISTINCT de.domain_id) AS domain_ids,
|
||||||
|
COUNT(de.domain_id) AS cnt,
|
||||||
|
COALESCE(org, fn) AS registrant
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
e.handle AS handle,
|
||||||
|
e.id,
|
||||||
|
e.tld_id,
|
||||||
|
jsonb_path_query_first(
|
||||||
|
e.j_card,
|
||||||
|
'$[1] ? (@[0] == "fn")[3]'
|
||||||
|
) #>> '{}' AS fn,
|
||||||
|
jsonb_path_query_first(
|
||||||
|
e.j_card,
|
||||||
|
'$[1] ? (@[0] == "org")[3]'
|
||||||
|
) #>> '{}' AS org
|
||||||
|
FROM entity e
|
||||||
|
) sub
|
||||||
|
JOIN domain_entity de ON de.entity_uid = sub.id
|
||||||
|
WHERE LOWER(org||fn) NOT LIKE '%redacted%'
|
||||||
|
AND LOWER(org||fn) NOT LIKE '%privacy%'
|
||||||
|
AND LOWER(org||fn) NOT LIKE '%registration private%'
|
||||||
|
AND LOWER(org||fn) NOT LIKE '%domain administrator%'
|
||||||
|
AND LOWER(org||fn) NOT LIKE '%registry super user account%'
|
||||||
|
AND LOWER(org||fn) NOT LIKE '%ano nymous%'
|
||||||
|
AND LOWER(org||fn) NOT LIKE '%by proxy%'
|
||||||
|
AND handle NOT IN ($handleBlacklist)
|
||||||
|
AND de.roles @> '["registrant"]'
|
||||||
|
AND sub.tld_id IS NOT NULL
|
||||||
|
AND (LOWER(org) = LOWER(:var) OR LOWER(fn) = LOWER(:var))
|
||||||
|
GROUP BY COALESCE(org, fn)
|
||||||
|
HAVING COALESCE(org, fn) != '' AND COALESCE(org, fn) != '' IS NOT NULL
|
||||||
|
ORDER BY cnt DESC;
|
||||||
|
SQL;
|
||||||
|
$query = $this->em->createNativeQuery($sql, $rsm);
|
||||||
|
$query->setParameter('var', trim($request->get('registrant')));
|
||||||
|
|
||||||
|
$result = $query->getOneOrNullResult();
|
||||||
|
|
||||||
|
if (!$result) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$domainList = array_filter(explode(',', trim($result['domain_ids'], '{}')));
|
||||||
|
|
||||||
|
if (empty($domainList)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->em->getRepository(Domain::class)
|
||||||
|
->createQueryBuilder('d')
|
||||||
|
->where('d.ldhName IN (:list)')
|
||||||
|
->setParameter('list', $domainList)
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user