feat: add domain reverse search on registrant name

This commit is contained in:
Maël Gangloff
2025-10-27 14:06:25 +01:00
parent 3dda89b49e
commit 1f5d386b0d
2 changed files with 109 additions and 0 deletions

View File

@@ -5,11 +5,14 @@ namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use App\Config\EventAction;
use App\Exception\MalformedDomainException;
use App\Repository\DomainRepository;
use App\Service\RDAPService;
use App\State\AutoRegisterDomainProvider;
use App\State\FindDomainListFromEntityProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
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(
uriTemplate: '/domains/{ldhName}', // Do not delete this line, otherwise Symfony interprets the TLD of the domain name as a return type
normalizationContext: [

View 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();
}
}