tag lookups

This commit is contained in:
robert dennis
2026-10-12 05:18:59 -04:00
parent cd564977cd
commit cb9e2b4624
16 changed files with 137 additions and 4 deletions
+6 -1
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pterodactyl\Actions\Tags;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Pterodactyl\Contracts\Tags\UpdatesTags;
use Pterodactyl\Enum\EggSpecificTags;
use Pterodactyl\Exceptions\DisplayException;
@@ -18,7 +19,11 @@ final readonly class UpdateTag implements UpdatesTags
// presentation, so it is never renamed or relabelled.
throw_if(EggSpecificTags::isSpecial($tag->slug), DisplayException::class, 'Built-in game tags cannot be edited.');
$validated = Validator::validate($input, CreateTag::rules($tag));
// Nor may a custom tag take a built-in slug and become one.
$rules = CreateTag::rules($tag);
$rules['slug'][] = Rule::notIn(EggSpecificTags::cases());
$validated = Validator::validate($input, $rules, ['slug.not_in' => 'The slug :input belongs to a built-in game tag.']);
// Blank strings are null, as they are in a request, so a blank colour or legacy nest id
// is cleared. The colour and legacy nest id it is not given keep their stored values.
$validated = array_map(fn (mixed $value): mixed => is_string($value) && blank($value) ? null : $value, $validated);
@@ -26,6 +26,7 @@ use Pterodactyl\Http\Requests\Api\Admin\Eggs\UpdateEggRequest;
use Pterodactyl\Http\Resources\Api\Admin\EggResource;
use Pterodactyl\Http\Resources\Api\ApiResourceCollection;
use Pterodactyl\Models\Egg;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
#[Group('Admin API', 'Root administrator endpoints for managing panel configuration and resources.')]
@@ -47,6 +48,7 @@ class EggController extends AdminApiController
*/
#[Endpoint('List eggs', 'Returns a paginated list of egg definitions.')]
#[QueryParam('filter[name]', 'string', 'Filter eggs by name.', required: false, example: 'Paper')]
#[QueryParam('filter[tag]', 'string', 'Filter eggs by tag slug. Separate several slugs with commas to return eggs that carry any of them.', required: false, example: 'minecraft')]
#[QueryParam('sort', 'string', 'Sort eggs by id or name. Prefix with a hyphen for descending order.', required: false, example: 'name')]
#[QueryParam('page', 'integer', 'Page number to return.', required: false, example: 1)]
#[QueryParam('per_page', 'integer', 'Results to return per page.', required: false, example: 50)]
@@ -55,7 +57,7 @@ class EggController extends AdminApiController
public function index(GetEggsRequest $request): ApiResourceCollection
{
$eggs = QueryBuilder::for(Egg::query())
->allowedFilters(['name'])
->allowedFilters(['name', AllowedFilter::exact('tag', 'tags.slug')])
->allowedSorts(['id', 'name'])
->paginate($request->perPage());
@@ -15,6 +15,7 @@ use Pterodactyl\Http\Requests\Api\Application\Tags\GetTagsRequest;
use Pterodactyl\Http\Resources\Api\ApiResourceCollection;
use Pterodactyl\Http\Resources\Api\Application\TagResource;
use Pterodactyl\Models\Tag;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
#[Group('Application API', 'Root administrator endpoints for managing panel resources using application API tokens.')]
@@ -28,13 +29,14 @@ class TagController extends ApplicationApiController
#[QueryParam('per_page', 'integer', 'Number of tags to return per page, up to 100.', required: false, example: 50)]
#[QueryParam('filter[name]', 'string', 'Filter tags by name.', required: false, example: 'Minecraft')]
#[QueryParam('filter[slug]', 'string', 'Filter tags by slug.', required: false, example: 'minecraft')]
#[QueryParam('filter[legacy_nest_id]', 'integer', 'Filter tags by the ID of the nest they were created from.', required: false, example: 1)]
#[QueryParam('sort', 'string', 'Sort tags by id, name, or slug. Prefix with "-" for descending order.', required: false, example: 'name', enum: ['id', '-id', 'name', '-name', 'slug', '-slug'])]
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "eggs" and "nodes" when the key can read those resources.', required: false, example: 'eggs', enum: ['eggs', 'nodes', 'eggs,nodes'])]
#[ResponseFromResource(TagResource::class, Tag::class, collection: true, resourceKey: 'tag', paginate: 50)]
public function index(GetTagsRequest $request): ApiResourceCollection
{
$tags = QueryBuilder::for(Tag::query())
->allowedFilters(['name', 'slug'])
->allowedFilters(['name', 'slug', AllowedFilter::exact('legacy_nest_id')])
->allowedSorts(['id', 'name', 'slug'])
->paginate($request->perPage());
+16
View File
@@ -4061,6 +4061,10 @@ export type AdminSyncEggTagsEggIdPathParameter = number;
* Filter eggs by name.
*/
export type AdminListEggsFilterNameQueryParameter = string;
/**
* Filter eggs by tag slug. Separate several slugs with commas to return eggs that carry any of them.
*/
export type AdminListEggsFilterTagQueryParameter = string;
/**
* Sort eggs by id or name. Prefix with a hyphen for descending order.
*/
@@ -4355,6 +4359,10 @@ export type ApplicationListTagsFilterNameQueryParameter = string;
* Filter tags by slug.
*/
export type ApplicationListTagsFilterSlugQueryParameter = string;
/**
* Filter tags by the ID of the nest they were created from.
*/
export type ApplicationListTagsFilterLegacyNestIdQueryParameter = number;
/**
* Sort tags by id, name, or slug. Prefix with "-" for descending order.
*/
@@ -8127,6 +8135,10 @@ export type AdminListEggsData = {
* Filter eggs by name.
*/
'filter[name]'?: AdminListEggsFilterNameQueryParameter;
/**
* Filter eggs by tag slug. Separate several slugs with commas to return eggs that carry any of them.
*/
'filter[tag]'?: AdminListEggsFilterTagQueryParameter;
/**
* Sort eggs by id or name. Prefix with a hyphen for descending order.
*/
@@ -9970,6 +9982,10 @@ export type ApplicationListTagsData = {
* Filter tags by slug.
*/
'filter[slug]'?: ApplicationListTagsFilterSlugQueryParameter;
/**
* Filter tags by the ID of the nest they were created from.
*/
'filter[legacy_nest_id]'?: ApplicationListTagsFilterLegacyNestIdQueryParameter;
/**
* Sort tags by id, name, or slug. Prefix with "-" for descending order.
*/
File diff suppressed because one or more lines are too long
@@ -4469,6 +4469,11 @@ export type AdminSyncEggTagsEggIdPathParameter = number;
*/
export type AdminListEggsFilterNameQueryParameter = string;
/**
* Filter eggs by tag slug. Separate several slugs with commas to return eggs that carry any of them.
*/
export type AdminListEggsFilterTagQueryParameter = string;
/**
* Sort eggs by id or name. Prefix with a hyphen for descending order.
*/
@@ -4847,6 +4852,11 @@ export type ApplicationListTagsFilterNameQueryParameter = string;
*/
export type ApplicationListTagsFilterSlugQueryParameter = string;
/**
* Filter tags by the ID of the nest they were created from.
*/
export type ApplicationListTagsFilterLegacyNestIdQueryParameter = number;
/**
* Sort tags by id, name, or slug. Prefix with "-" for descending order.
*/
@@ -9220,6 +9230,10 @@ export type AdminListEggsData = {
* Filter eggs by name.
*/
'filter[name]'?: AdminListEggsFilterNameQueryParameter;
/**
* Filter eggs by tag slug. Separate several slugs with commas to return eggs that carry any of them.
*/
'filter[tag]'?: AdminListEggsFilterTagQueryParameter;
/**
* Sort eggs by id or name. Prefix with a hyphen for descending order.
*/
@@ -11313,6 +11327,10 @@ export type ApplicationListTagsData = {
* Filter tags by slug.
*/
'filter[slug]'?: ApplicationListTagsFilterSlugQueryParameter;
/**
* Filter tags by the ID of the nest they were created from.
*/
'filter[legacy_nest_id]'?: ApplicationListTagsFilterLegacyNestIdQueryParameter;
/**
* Sort tags by id, name, or slug. Prefix with "-" for descending order.
*/
@@ -0,0 +1,11 @@
GET /api/admin/eggs?filter[tag]=premium
200 application/json
{"object":"list","data":[{"object":"egg","attributes":{"id":1,"uuid":"e9900001-0000-4000-8000-000000000001","author":"support@example.test","name":"Paper","description":"High performance Minecraft server.","features":["eula","java_version"],"docker_image":"ghcr.io\/pterodactyl\/yolks:java_21","docker_images":{"Java 21":"ghcr.io\/pterodactyl\/yolks:java_21","Java 17":"ghcr.io\/pterodactyl\/yolks:java_17"},"force_outgoing_ip":false,"config":{"files":{"server.properties":{"parser":"properties","find":{"server-ip":"0.0.0.0","server-port":"{{server.build.default.port}}"}}},"startup":{"done":")! For help, type "},"stop":"stop","logs":{},"file_denylist":[],"extends":null},"startup":"java -Xms128M -Xmx{{SERVER_MEMORY}}M -jar {{SERVER_JARFILE}}","script":{"privileged":true,"install":"#!\/bin\/ash\necho \"installing\"","entry":"ash","container":"ghcr.io\/pterodactyl\/installers:alpine","extends":null},"relationships":[],"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}}],"meta":{"pagination":{"total":1,"count":1,"per_page":50,"current_page":1,"total_pages":1,"links":{}}}}
GET /api/admin/eggs?filter[tag]=minecraft,premium&include=tags
200 application/json
{"object":"list","data":[{"object":"egg","attributes":{"id":1,"uuid":"e9900001-0000-4000-8000-000000000001","author":"support@example.test","name":"Paper","description":"High performance Minecraft server.","features":["eula","java_version"],"docker_image":"ghcr.io\/pterodactyl\/yolks:java_21","docker_images":{"Java 21":"ghcr.io\/pterodactyl\/yolks:java_21","Java 17":"ghcr.io\/pterodactyl\/yolks:java_17"},"force_outgoing_ip":false,"config":{"files":{"server.properties":{"parser":"properties","find":{"server-ip":"0.0.0.0","server-port":"{{server.build.default.port}}"}}},"startup":{"done":")! For help, type "},"stop":"stop","logs":{},"file_denylist":[],"extends":null},"startup":"java -Xms128M -Xmx{{SERVER_MEMORY}}M -jar {{SERVER_JARFILE}}","script":{"privileged":true,"install":"#!\/bin\/ash\necho \"installing\"","entry":"ash","container":"ghcr.io\/pterodactyl\/installers:alpine","extends":null},"relationships":{"tags":{"object":"list","data":[{"object":"tag","attributes":{"id":1,"name":"Minecraft","slug":"minecraft","color":"#5B8731","is_predefined":true,"legacy_nest_id":1,"eggs_count":0,"nodes_count":0,"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}},{"object":"tag","attributes":{"id":2,"name":"Premium","slug":"premium","color":"#ffaa00","is_predefined":false,"legacy_nest_id":null,"eggs_count":0,"nodes_count":0,"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}}]}},"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}},{"object":"egg","attributes":{"id":2,"uuid":"e9900002-0000-4000-8000-000000000002","author":"support@example.test","name":"Paper Child","description":null,"features":null,"docker_image":"ghcr.io\/pterodactyl\/yolks:java_21","docker_images":{"Java 21":"ghcr.io\/pterodactyl\/yolks:java_21"},"force_outgoing_ip":true,"config":{"files":null,"startup":null,"stop":null,"logs":null,"file_denylist":["*.secret","config\/private.yml"],"extends":1},"startup":"java -jar {{SERVER_JARFILE}}","script":{"privileged":false,"install":null,"entry":"ash","container":"alpine:3.4","extends":1},"relationships":{"tags":{"object":"list","data":[{"object":"tag","attributes":{"id":1,"name":"Minecraft","slug":"minecraft","color":"#5B8731","is_predefined":true,"legacy_nest_id":1,"eggs_count":0,"nodes_count":0,"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}}]}},"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}}],"meta":{"pagination":{"total":2,"count":2,"per_page":50,"current_page":1,"total_pages":1,"links":{}}}}
GET /api/admin/eggs?filter[tag]=none
200 application/json
{"object":"list","data":[],"meta":{"pagination":{"total":0,"count":0,"per_page":50,"current_page":1,"total_pages":1,"links":{}}}}
@@ -0,0 +1,3 @@
PUT /api/admin/tags/2
422 application/json
{"errors":[{"code":"ValidationException","status":"422","detail":"The slug rust belongs to a built-in game tag.","meta":{"source_field":"slug","rule":"not_in"}}]}
@@ -0,0 +1,11 @@
GET /api/application/tags?filter[legacy_nest_id]=1
200 application/json
{"object":"list","data":[{"object":"tag","attributes":{"id":1,"name":"Minecraft","slug":"minecraft","color":"#5B8731","is_predefined":true,"legacy_nest_id":1,"relationships":[],"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}}],"meta":{"pagination":{"total":1,"count":1,"per_page":50,"current_page":1,"total_pages":1,"links":{}}}}
GET /api/application/tags?filter[legacy_nest_id]=7&include=eggs
200 application/json
{"object":"list","data":[{"object":"tag","attributes":{"id":4,"name":"Unused","slug":"unused","color":"#000000","is_predefined":false,"legacy_nest_id":7,"relationships":{"eggs":{"object":"list","data":[]}},"created_at":"2026-01-02T03:04:05+00:00","updated_at":"2026-02-03T04:05:06+00:00"}}],"meta":{"pagination":{"total":1,"count":1,"per_page":50,"current_page":1,"total_pages":1,"links":{}}}}
GET /api/application/tags?filter[legacy_nest_id]=99
200 application/json
{"object":"list","data":[],"meta":{"pagination":{"total":0,"count":0,"per_page":50,"current_page":1,"total_pages":1,"links":{}}}}
+20
View File
@@ -7484,6 +7484,9 @@ components:
AdminListEggsFilterNameQueryParameter:
type: string
description: 'Filter eggs by name.'
AdminListEggsFilterTagQueryParameter:
type: string
description: 'Filter eggs by tag slug. Separate several slugs with commas to return eggs that carry any of them.'
AdminListEggsSortQueryParameter:
type: string
description: 'Sort eggs by id or name. Prefix with a hyphen for descending order.'
@@ -7816,6 +7819,9 @@ components:
ApplicationListTagsFilterSlugQueryParameter:
type: string
description: 'Filter tags by slug.'
ApplicationListTagsFilterLegacyNestIdQueryParameter:
type: integer
description: 'Filter tags by the ID of the nest they were created from.'
ApplicationListTagsSortQueryParameter:
type: string
description: 'Sort tags by id, name, or slug. Prefix with "-" for descending order.'
@@ -12580,6 +12586,13 @@ paths:
required: false
schema:
$ref: '#/components/schemas/AdminListEggsFilterNameQueryParameter'
-
in: query
name: 'filter[tag]'
description: 'Filter eggs by tag slug. Separate several slugs with commas to return eggs that carry any of them.'
required: false
schema:
$ref: '#/components/schemas/AdminListEggsFilterTagQueryParameter'
-
in: query
name: sort
@@ -14925,6 +14938,13 @@ paths:
required: false
schema:
$ref: '#/components/schemas/ApplicationListTagsFilterSlugQueryParameter'
-
in: query
name: 'filter[legacy_nest_id]'
description: 'Filter tags by the ID of the nest they were created from.'
required: false
schema:
$ref: '#/components/schemas/ApplicationListTagsFilterLegacyNestIdQueryParameter'
-
in: query
name: sort
@@ -56,6 +56,14 @@ test('tags updated without the http api meet the same rules', function (array $i
'a colour that is not hex' => [['color' => '#12345g'], 'color'],
]);
test('a custom tag cannot take a built-in slug', function (): void {
$tag = Tag::factory()->create();
expect(fn () => $this->app->make(UpdatesTags::class)->update($tag, ['name' => 'FiveM', 'slug' => 'fivem']))
->toThrow(ValidationException::class, 'The slug fivem belongs to a built-in game tag.');
expect($tag->fresh()->slug)->not->toBe('fivem');
});
test('built-in game tags cannot be edited, whatever the input', function (array $input): void {
$tag = Tag::factory()->predefined('fivem')->create();
@@ -21,6 +21,17 @@ test('eggs can be filtered by name', function () {
Egg::factory()->create(['name' => 'Other Egg']);
$this->getJson(route('api.admin.eggs', ['filter' => ['name' => 'Filterable Egg']]))->assertOk()->assertJsonCount(1, 'data')->assertJsonPath('data.0.attributes.id', $egg->id);
});
test('eggs can be filtered by tag slug', function () {
[$first, $second] = Egg::factory()->times(2)->create();
Egg::factory()->create();
$first->tags()->attach(Tag::factory()->create(['slug' => 'filter-first']));
$second->tags()->attach(Tag::factory()->create(['slug' => 'filter-second']));
$ids = fn (string $tags): array => collect($this->getJson(route('api.admin.eggs', ['filter' => ['tag' => $tags]]))->assertOk()->json('data'))->pluck('attributes.id')->sort()->values()->all();
expect($ids('filter-first'))->toBe([$first->id])
->and($ids('filter-first,filter-second'))->toBe([$first->id, $second->id])
->and($ids('filter-none'))->toBe([]);
});
test('egg tags can be included', function () {
/** @var Egg $egg */
$egg = Egg::factory()->create();
@@ -45,6 +45,15 @@ test('built in tags cannot be updated', function () {
$tag = Tag::factory()->predefined('fivem')->create();
$this->putJson(route('api.admin.tags.update', ['tag' => $tag->id]), ['name' => 'Changed', 'slug' => 'changed'])->assertStatus(Response::HTTP_BAD_REQUEST)->assertJsonPath('errors.0.code', 'DisplayException');
});
test('a custom tag cannot be renamed onto a built in slug', function () {
$tag = Tag::factory()->create(['slug' => 'custom']);
$this->putJson(route('api.admin.tags.update', ['tag' => $tag->id]), ['name' => 'FiveM', 'slug' => 'fivem'])
->assertUnprocessable()
->assertJsonPath('errors.0.meta.source_field', 'slug')
->assertJsonPath('errors.0.meta.rule', 'not_in')
->assertJsonPath('errors.0.detail', 'The slug fivem belongs to a built-in game tag.');
$this->assertDatabaseHas('tags', ['id' => $tag->id, 'slug' => 'custom']);
});
test('built in tags cannot be deleted', function () {
/** @var Tag $tag */
$tag = Tag::factory()->predefined('fivem')->create();
@@ -37,6 +37,12 @@ test('filter tags by slug', function (): void {
$response = $this->getJson('/api/application/tags?filter[slug]=filter-me')->assertOk();
expect($response->json('data.*.attributes.id'))->toBe([$tag->id]);
});
test('filter tags by legacy nest id', function (): void {
$tag = Tag::factory()->create(['legacy_nest_id' => 4242]);
Tag::factory()->create(['legacy_nest_id' => 42421]);
$response = $this->getJson('/api/application/tags?filter[legacy_nest_id]=4242')->assertOk();
expect($response->json('data.*.attributes.id'))->toBe([$tag->id]);
});
test('return single tag', function (): void {
$tag = Tag::factory()->create(['legacy_nest_id' => 42]);
$this->getJson('/api/application/tags/'.$tag->id)->assertOk()->assertJson(['object' => 'tag', 'attributes' => ['id' => $tag->id, 'name' => $tag->name, 'slug' => $tag->slug, 'color' => $tag->color, 'is_predefined' => false, 'legacy_nest_id' => 42, 'relationships' => [], 'created_at' => $tag->created_at->toAtomString(), 'updated_at' => $tag->updated_at->toAtomString()]], true);
@@ -104,6 +104,11 @@ function adminReads(): array
'database hosts view' => [['requests' => [...adminMatrix('database_host', ADMIN.'/database-hosts/1'), ['GET', ADMIN.'/database-hosts/2?include=node,databases']]]],
'database hosts databases' => [['requests' => [...adminMatrix(null, ADMIN.'/database-hosts/1/databases'), ...World::pages(ADMIN.'/database-hosts/1/databases', ADMIN.'/database-hosts/2/databases')]]],
'eggs index' => [['requests' => [...adminMatrix('egg', ADMIN.'/eggs'), ...World::pages(ADMIN.'/eggs', ADMIN.'/eggs?filter[name]=no-such-egg')]]],
'eggs index tag filter' => [['requests' => [
['GET', ADMIN.'/eggs?filter[tag]=premium'],
['GET', ADMIN.'/eggs?filter[tag]=minecraft,premium&include=tags'],
['GET', ADMIN.'/eggs?filter[tag]=none'],
]]],
'eggs view' => [['requests' => [...adminMatrix('egg', ADMIN.'/eggs/1'), ['GET', ADMIN.'/eggs/2'], ['GET', ADMIN.'/eggs/3?include=variables,tags,servers']]]],
'eggs scripts' => [['requests' => [...adminMatrix('egg', ADMIN.'/eggs/2/scripts'), ['GET', ADMIN.'/eggs/3/scripts']]]],
'eggs variables' => [['requests' => [...adminMatrix(null, ADMIN.'/eggs/1/variables'), ['GET', ADMIN.'/eggs/3/variables']]]],
@@ -205,6 +210,7 @@ function adminWrites(): array
]]],
'write tags store' => [['requests' => [['POST', ADMIN.'/tags', ['name' => 'Created Tag', 'slug' => 'created-tag', 'color' => '#123456']]]]],
'write tags update' => [['requests' => [['PUT', ADMIN.'/tags/2', ['name' => 'Premium Plus', 'slug' => 'premium-plus', 'color' => '#654321']]]]],
'write tags update built in slug' => [['requests' => [['PUT', ADMIN.'/tags/2', ['name' => 'Rust', 'slug' => 'rust', 'color' => '#654321']]]]],
'write users store' => [['requests' => [['POST', ADMIN.'/users', ['username' => 'created', 'email' => 'created@example.test', 'name_first' => 'Cre', 'name_last' => 'Ated', 'external_id' => 'ext-created']]]]],
'write users update' => [['requests' => [['PUT', ADMIN.'/users/2?include=servers', ['username' => 'owner', 'email' => 'owner@example.test', 'name_first' => 'Olivia', 'name_last' => 'Owner']]]]],
];
@@ -132,6 +132,11 @@ function applicationReads(): array
'servers databases index' => [['requests' => [...applicationMatrix('database', APP.'/servers/1/databases'), ['GET', APP.'/servers/2/databases']]]],
'servers databases view' => [['requests' => applicationMatrix('database', APP.'/servers/1/databases/1')]],
'tags index' => [['requests' => [...applicationMatrix('tag', APP.'/tags'), ...World::pages(APP.'/tags', APP.'/tags?filter[slug]=none')]]],
'tags index legacy nest filter' => [['requests' => [
['GET', APP.'/tags?filter[legacy_nest_id]=1'],
['GET', APP.'/tags?filter[legacy_nest_id]=7&include=eggs'],
['GET', APP.'/tags?filter[legacy_nest_id]=99'],
]]],
'tags view' => [['requests' => [...applicationMatrix('tag', APP.'/tags/1'), ['GET', APP.'/tags/4?include=eggs,nodes']]]],
'users index' => [['requests' => [...applicationMatrix('user', APP.'/users'), ...World::pages(APP.'/users', APP.'/users?filter[email]=none@example.test')]]],
'users view' => [['requests' => [...applicationMatrix('user', APP.'/users/2'), ['GET', APP.'/users/5?include=servers']]]],