Skip to content

Commit fb1b240

Browse files
committed
Add API endpoint to get a server by external ID
1 parent a1e704d commit fb1b240

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1414
* Adds ability to include egg variables on an API request.
1515
* Added `external_id` column to servers that allows for easier linking with external services such as WHMCS.
1616
* Added back the sidebar when viewing servers that allows for quick-switching to a different server.
17+
* Added API endpoint to get a server by external ID.
1718

1819
## v0.7.1 (Derelict Dermodactylus)
1920
### Fixed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
4+
5+
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
6+
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
7+
use Pterodactyl\Http\Requests\Api\Application\Servers\GetExternalServerRequest;
8+
9+
class ExternalServerController extends ApplicationApiController
10+
{
11+
/**
12+
* Retrieve a specific server from the database using its external ID.
13+
*
14+
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\GetExternalServerRequest $request
15+
* @return array
16+
*/
17+
public function index(GetExternalServerRequest $request): array
18+
{
19+
return $this->fractal->item($request->getServerModel())
20+
->transformWith($this->getTransformer(ServerTransformer::class))
21+
->toArray();
22+
}
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
4+
5+
use Pterodactyl\Models\Server;
6+
use Pterodactyl\Services\Acl\Api\AdminAcl;
7+
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
8+
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
9+
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
10+
11+
class GetExternalServerRequest extends ApplicationApiRequest
12+
{
13+
/**
14+
* @var \Pterodactyl\Models\Server
15+
*/
16+
private $serverModel;
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $resource = AdminAcl::RESOURCE_SERVERS;
22+
23+
/**
24+
* @var int
25+
*/
26+
protected $permission = AdminAcl::READ;
27+
28+
/**
29+
* Determine if the requested external user exists.
30+
*
31+
* @return bool
32+
*/
33+
public function resourceExists(): bool
34+
{
35+
$repository = $this->container->make(ServerRepositoryInterface::class);
36+
37+
try {
38+
$this->serverModel = $repository->findFirstWhere([
39+
['external_id', '=', $this->route()->parameter('external_id')],
40+
]);
41+
} catch (RecordNotFoundException $exception) {
42+
return false;
43+
}
44+
45+
return true;
46+
}
47+
48+
/**
49+
* Return the server model for the requested external server.
50+
*
51+
* @return \Pterodactyl\Models\Server
52+
*/
53+
public function getServerModel(): Server
54+
{
55+
return $this->serverModel;
56+
}
57+
}

routes/api-application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
Route::group(['prefix' => '/servers'], function () {
7575
Route::get('/', 'Servers\ServerController@index')->name('api.application.servers');
7676
Route::get('/{server}', 'Servers\ServerController@view')->name('api.application.servers.view');
77+
Route::get('/external/{external_id}', 'Servers\ExternalServerController@index')->name('api.application.servers.external');
7778

7879
Route::patch('/{server}/details', 'Servers\ServerDetailsController@details')->name('api.application.servers.details');
7980
Route::patch('/{server}/build', 'Servers\ServerDetailsController@build')->name('api.application.servers.build');

0 commit comments

Comments
 (0)